Class: Fwd::Pool

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fwd/pool.rb

Overview

“Clone” of normal ConnectionPool but tweaked for round-robin

Defined Under Namespace

Classes: IdleStack

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Pool

Returns a new instance of Pool.



14
15
16
17
18
19
20
21
22
# File 'lib/fwd/pool.rb', line 14

def initialize(items)
  @idle = IdleStack.new(0) {}
  @size = items.size
  @key  = :"io-proxy-pool-#{@idle.object_id}"

  items.each do |item|
    @idle.push(item)
  end
end

Instance Method Details

#checkoutObject



28
29
30
31
32
33
# File 'lib/fwd/pool.rb', line 28

def checkout
  conn = @idle.pop(30)
  yield conn
ensure
  @idle.unshift(conn) if conn
end

#each(&block) ⇒ Object



24
25
26
# File 'lib/fwd/pool.rb', line 24

def each(&block)
  @size.times { checkout(&block) }
end