Class: Cql::Client::ConnectionManager
- Inherits:
-
Object
- Object
- Cql::Client::ConnectionManager
- Includes:
- Enumerable
- Defined in:
- lib/cql/client/connection_manager.rb
Instance Method Summary collapse
- #add_connections(connections) ⇒ Object
- #connected? ⇒ Boolean
- #each_connection(&callback) ⇒ Object (also: #each)
-
#initialize ⇒ ConnectionManager
constructor
A new instance of ConnectionManager.
- #random_connection ⇒ Object
- #snapshot ⇒ Object
Constructor Details
#initialize ⇒ ConnectionManager
Returns a new instance of ConnectionManager.
9 10 11 12 |
# File 'lib/cql/client/connection_manager.rb', line 9 def initialize @connections = [] @lock = Mutex.new end |
Instance Method Details
#add_connections(connections) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cql/client/connection_manager.rb', line 14 def add_connections(connections) @lock.synchronize do @connections.concat(connections) connections.each do |connection| connection.on_closed do @lock.synchronize do @connections.delete(connection) end end end end end |
#connected? ⇒ Boolean
27 28 29 30 31 |
# File 'lib/cql/client/connection_manager.rb', line 27 def connected? @lock.synchronize do @connections.any? end end |
#each_connection(&callback) ⇒ Object Also known as: each
46 47 48 49 50 51 52 |
# File 'lib/cql/client/connection_manager.rb', line 46 def each_connection(&callback) return self unless block_given? raise NotConnectedError unless connected? @lock.synchronize do @connections.each(&callback) end end |
#random_connection ⇒ Object
39 40 41 42 43 44 |
# File 'lib/cql/client/connection_manager.rb', line 39 def random_connection raise NotConnectedError unless connected? @lock.synchronize do @connections.sample end end |
#snapshot ⇒ Object
33 34 35 36 37 |
# File 'lib/cql/client/connection_manager.rb', line 33 def snapshot @lock.synchronize do @connections.dup end end |