Class: Monga::ConnectionPool
- Inherits:
-
Object
- Object
- Monga::ConnectionPool
- Defined in:
- lib/monga/connection_pool.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
Instance Method Summary collapse
-
#aquire_connection ⇒ Object
Aquires random connection with min waiting responses among connected.
-
#initialize(opts) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
Constructor Details
#initialize(opts) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
5 6 7 8 9 10 11 12 |
# File 'lib/monga/connection_pool.rb', line 5 def initialize(opts) pool_size = opts.delete :pool_size @connections = [] pool_size.times do @connections << Monga::Connection.new(opts) end end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
3 4 5 |
# File 'lib/monga/connection_pool.rb', line 3 def connections @connections end |
Instance Method Details
#aquire_connection ⇒ Object
Aquires random connection with min waiting responses among connected. Otherwise return random disconnected one.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/monga/connection_pool.rb', line 16 def aquire_connection connected = @connections.select(&:connected?) if connected.any? min = connected.min_by{ |c| c.responses.size }.responses.size conns = connected.select{ |c| c.responses.size == min } conns.sample else @connections.sample end end |