Class: NeverBlock::Pool::FiberedConnectionPool
- Inherits:
-
Object
- Object
- NeverBlock::Pool::FiberedConnectionPool
- Defined in:
- lib/never_block/pool/fibered_connection_pool.rb
Overview
Example:
pool = NeverBlock::Pool::FiberedConnectionPool.new(:size=>16)do
# connection creation code goes here
end 32.times do
Fiber.new do
conn = pool.hold # hold will pause the fiber until a connection is available
conn.execute('something') # you can use the connection normally now
end.resume
end
The pool has support for transactions, just pass true to the pool#hold method and the connection will not be released after the block is finished It is the responsibility of client code to release the connection
Instance Method Summary collapse
- #all_connections ⇒ Object
-
#hold(transactional = false) ⇒ Object
If a connection is available, pass it to the block, otherwise pass the fiber to the queue till a connection is available when done with a connection try to porcess other fibers in the queue before releasing the connection if inside a transaction, don’t release the fiber.
-
#initialize(options = {}, &block) ⇒ FiberedConnectionPool
constructor
initialize the connection pool using the supplied proc to create the connections you can choose to start them eagerly or lazily (lazy by default).
-
#release(fiber, conn) ⇒ Object
Give the fiber back to the pool you have to call this explicitly if you held a connection for a transaction.
Constructor Details
#initialize(options = {}, &block) ⇒ FiberedConnectionPool
initialize the connection pool using the supplied proc to create the connections you can choose to start them eagerly or lazily (lazy by default)
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/never_block/pool/fibered_connection_pool.rb', line 41 def initialize( = {}, &block) @connections, @busy_connections, @queue = [], {},[] @connection_proc = block @size = [:size] || 8 if [:eager] @size.times do @connections << @connection_proc.call end end end |
Instance Method Details
#all_connections ⇒ Object
82 83 84 |
# File 'lib/never_block/pool/fibered_connection_pool.rb', line 82 def all_connections (@connections + @busy_connections.values).each {|conn| yield(conn)} end |
#hold(transactional = false) ⇒ Object
If a connection is available, pass it to the block, otherwise pass the fiber to the queue till a connection is available when done with a connection try to porcess other fibers in the queue before releasing the connection if inside a transaction, don’t release the fiber
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/never_block/pool/fibered_connection_pool.rb', line 60 def hold(transactional = false) fiber = Fiber.current if conn = @busy_connections[fiber] return yield(conn) end conn = acquire(fiber) begin yield conn ensure release(fiber, conn) unless transactional process_queue end end |
#release(fiber, conn) ⇒ Object
Give the fiber back to the pool you have to call this explicitly if you held a connection for a transaction
77 78 79 80 |
# File 'lib/never_block/pool/fibered_connection_pool.rb', line 77 def release(fiber, conn) @busy_connections.delete(fiber) @connections << conn end |