Class: EM::PG::Sequel::ConnectionPool
- Inherits:
-
Sequel::ConnectionPool
- Object
- Sequel::ConnectionPool
- EM::PG::Sequel::ConnectionPool
- Defined in:
- lib/em-pg-sequel/connection_pool.rb
Constant Summary collapse
- DEFAULT_SIZE =
4
Instance Attribute Summary collapse
-
#allocated ⇒ Object
readonly
Returns the value of attribute allocated.
-
#available ⇒ Object
readonly
Returns the value of attribute available.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #disconnect(server = nil) ⇒ Object
- #hold(server = nil) ⇒ Object
-
#initialize(db, opts = {}) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #size ⇒ Object
Constructor Details
#initialize(db, opts = {}) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
9 10 11 12 13 14 15 16 17 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 9 def initialize(db, opts = {}) super @available = [] @allocated = {} @pending = [] @max_size = opts[:max_connections] || DEFAULT_SIZE hold {} end |
Instance Attribute Details
#allocated ⇒ Object (readonly)
Returns the value of attribute allocated.
7 8 9 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 7 def allocated @allocated end |
#available ⇒ Object (readonly)
Returns the value of attribute available.
7 8 9 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 7 def available @available end |
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
7 8 9 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 7 def max_size @max_size end |
Instance Method Details
#disconnect(server = nil) ⇒ Object
47 48 49 50 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 47 def disconnect(server = nil) @available.each{ |conn| db.disconnect_connection(conn) } @available.clear end |
#hold(server = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 23 def hold(server = nil) fiber = Fiber.current fiber_id = fiber.object_id if conn = @allocated[fiber_id] skip_release = true else conn = acquire(fiber) until conn end begin yield conn rescue ::Sequel::DatabaseDisconnectError => e db.disconnect_connection(conn) drop_failed(fiber_id) skip_release = true raise ensure release(fiber_id) unless skip_release end end |
#size ⇒ Object
19 20 21 |
# File 'lib/em-pg-sequel/connection_pool.rb', line 19 def size @available.length + @allocated.length end |