Class: FiberConnectionPool
- Inherits:
-
Object
- Object
- FiberConnectionPool
- Defined in:
- lib/fiber_connection_pool.rb
Constant Summary collapse
- VERSION =
'0.1.2'
Instance Attribute Summary collapse
-
#saved_data ⇒ Object
Returns the value of attribute saved_data.
Instance Method Summary collapse
-
#initialize(opts) ⇒ FiberConnectionPool
constructor
Initializes the pool with ‘size’ instances running the given block to get each one.
-
#query(sql) ⇒ Object
avoid method_missing for most common methods.
- #recreate_connection(new_conn) ⇒ Object
- #save_data_for_fiber ⇒ Object
- #stop_saving_data_for_fiber ⇒ Object
Constructor Details
#initialize(opts) ⇒ FiberConnectionPool
Initializes the pool with ‘size’ instances running the given block to get each one. Ex:
pool = FiberConnectionPool.new(:size => 5) { MyConnection.new }
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fiber_connection_pool.rb', line 13 def initialize(opts) raise ArgumentError.new('size > 0 is mandatory') if opts[:size].to_i <= 0 @saved_data = {} # placeholder for requested save data @reserved = {} # map of in-progress connections @reserved_backup = {} # backup map of in-progress connections, to catch failures @available = [] # pool of free connections @pending = [] # pending reservations (FIFO) @available = Array.new(opts[:size].to_i) { yield } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object (private)
Allow the pool to behave as the underlying connection
If the requesting method begins with “a” prefix, then hijack the callbacks and errbacks to fire a connection pool release whenever the request is complete. Otherwise yield the connection within execute method and release once it is complete (assumption: fiber will yield until data is available, or request is complete)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/fiber_connection_pool.rb', line 117 def method_missing(method, *args, &blk) async = (method[0,1] == "a") execute(async,method) do |conn| df = conn.send(method, *args, &blk) if async fiber = Fiber.current df.callback do release(fiber) release_backup(fiber) end df.errback { release(fiber) } end df end end |
Instance Attribute Details
#saved_data ⇒ Object
Returns the value of attribute saved_data.
6 7 8 |
# File 'lib/fiber_connection_pool.rb', line 6 def saved_data @saved_data end |
Instance Method Details
#query(sql) ⇒ Object
avoid method_missing for most common methods
36 37 38 39 40 |
# File 'lib/fiber_connection_pool.rb', line 36 def query(sql) execute(false,'query') do |conn| conn.query sql end end |
#recreate_connection(new_conn) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fiber_connection_pool.rb', line 43 def recreate_connection(new_conn) bad_conn = @reserved_backup[Fiber.current.object_id] release_backup Fiber.current @available.reject!{ |v| v == bad_conn } @reserved.reject!{ |k,v| v == bad_conn } @available.push new_conn # try to cleanup begin bad_conn.close rescue end end |
#save_data_for_fiber ⇒ Object
25 26 27 |
# File 'lib/fiber_connection_pool.rb', line 25 def save_data_for_fiber @saved_data[Fiber.current.object_id] ||= {} end |
#stop_saving_data_for_fiber ⇒ Object
29 30 31 |
# File 'lib/fiber_connection_pool.rb', line 29 def stop_saving_data_for_fiber @saved_data.delete Fiber.current.object_id end |