Class: Cql::Client::ConnectionManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/cql/client/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

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

Returns:

  • (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

Raises:



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_connectionObject

Raises:



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

#snapshotObject



33
34
35
36
37
# File 'lib/cql/client/connection_manager.rb', line 33

def snapshot
  @lock.synchronize do
    @connections.dup
  end
end