Class: EbisuConnection::ConnectionManager

Inherits:
FreshConnection::AbstractConnectionManager
  • Object
show all
Defined in:
lib/ebisu_connection/connection_manager.rb

Defined Under Namespace

Classes: AllReplicaHasGoneError

Instance Method Summary collapse

Constructor Details

#initialize(spec_name = nil) ⇒ ConnectionManager

Returns a new instance of ConnectionManager.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ebisu_connection/connection_manager.rb', line 9

def initialize(spec_name = nil)
  super

  @replicas = Concurrent::Array.new

  replica_conf.each do |conf|
    @replicas << Replica.new(conf, spec_name)
  end

  recalc_roulette
end

Instance Method Details

#clear_all_connections!Object



32
33
34
35
36
# File 'lib/ebisu_connection/connection_manager.rb', line 32

def clear_all_connections!
  @replicas.each do |pool|
    pool.disconnect!
  end
end

#put_aside!Object



26
27
28
29
30
# File 'lib/ebisu_connection/connection_manager.rb', line 26

def put_aside!
  @replicas.each do |pool|
    pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
  end
end

#recovery?Boolean

Returns:

  • (Boolean)

Raises:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ebisu_connection/connection_manager.rb', line 38

def recovery?
  dead_replicas = @replicas.select do |pool|
    c = pool.connection rescue nil
    !c || !c.active?
  end
  return false if dead_replicas.empty?

  dead_replicas.each do |pool|
    pool.disconnect!
    @replicas.delete(pool)
  end

  raise AllReplicaHasGoneError if @replicas.empty?

  recalc_roulette
  true
end

#replica_connectionObject



21
22
23
24
# File 'lib/ebisu_connection/connection_manager.rb', line 21

def replica_connection
  raise AllReplicaHasGoneError if @replicas.empty?
  @replicas[@roulette.sample].connection
end