Class: NeverBlock::DB::Pool
- Inherits:
-
Object
- Object
- NeverBlock::DB::Pool
- Defined in:
- lib/neverblock/io/db/pool.rb
Overview
Example:
pool = NeverBlock::Pool.new(:size=>16)do
# connection creation code goes here
end 32.times do
Fiber.new do
# acquire a connection from the pool
pool.hold do |conn|
conn.execute('something') # you can use the connection normally now
end
end.resume
end
It is the responsibility of client code to release the connection using pool.release(conn)
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #all_connections ⇒ Object
-
#hold ⇒ Object
If a connection is available, pass it to the block, otherwise pass the fiber to the queue till a connection is available.
-
#initialize(options = {}, &block) ⇒ Pool
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) Available options are :size => the maximum number of connections to be created in the pool :eager => (true|false) indicates whether connections should be created initially or when need.
-
#release(conn) ⇒ Object
Give the fiber’s connection back to the pool.
- #replace_acquired_connection ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Pool
initialize the connection pool using the supplied proc to create the connections You can choose to start them eagerly or lazily (lazy by default) Available options are
:size => the maximum number of connections to be created in the pool
:eager => (true|false) indicates whether connections should be
created initially or when need
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/neverblock/io/db/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 Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
32 33 34 |
# File 'lib/neverblock/io/db/pool.rb', line 32 def size @size end |
Instance Method Details
#all_connections ⇒ Object
79 80 81 |
# File 'lib/neverblock/io/db/pool.rb', line 79 def all_connections (@connections + @busy_connections.values).each {|conn| yield(conn)} end |
#hold ⇒ Object
If a connection is available, pass it to the block, otherwise pass the fiber to the queue till a connection is available
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/neverblock/io/db/pool.rb', line 61 def hold fiber = NB::Fiber.current conn = acquire(fiber) if block_given? yield conn release(conn) else conn end end |
#release(conn) ⇒ Object
Give the fiber’s connection back to the pool
73 74 75 76 77 |
# File 'lib/neverblock/io/db/pool.rb', line 73 def release(conn) @busy_connections.delete(conn.object_id) @connections << conn unless @connections.include? conn process_queue end |
#replace_acquired_connection ⇒ Object
52 53 54 55 56 57 |
# File 'lib/neverblock/io/db/pool.rb', line 52 def replace_acquired_connection fiber = NB::Fiber.current conn = @connection_proc.call @busy_connections[fiber] = conn fiber[connection_pool_key] = conn end |