Class: Switchman::ConnectionPoolProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/switchman/connection_pool_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category, default_pool, shard_connection_pools) ⇒ ConnectionPoolProxy

Returns a new instance of ConnectionPoolProxy.



20
21
22
23
24
# File 'lib/switchman/connection_pool_proxy.rb', line 20

def initialize(category, default_pool, shard_connection_pools)
  @category = category
  @default_pool = default_pool
  @connection_pools = shard_connection_pools
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



14
15
16
# File 'lib/switchman/connection_pool_proxy.rb', line 14

def category
  @category
end

Instance Method Details

#active_shackles_environmentObject



30
31
32
# File 'lib/switchman/connection_pool_proxy.rb', line 30

def active_shackles_environment
  ::Rails.env.test? ? :master : active_shard.database_server.shackles_environment
end

#active_shardObject



26
27
28
# File 'lib/switchman/connection_pool_proxy.rb', line 26

def active_shard
  Shard.current(@category)
end

#clear_idle_connections!(since_when) ⇒ Object



71
72
73
# File 'lib/switchman/connection_pool_proxy.rb', line 71

def clear_idle_connections!(since_when)
  @connection_pools.values.each { |pool| pool.clear_idle_connections!(since_when) }
end

#connectionObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/switchman/connection_pool_proxy.rb', line 41

def connection
  pool = current_pool
  begin
    pool.connection
  rescue ConnectionError
    raise if active_shard.database_server == Shard.default.database_server && active_shackles_environment == :master
    configs = active_shard.database_server.config(active_shackles_environment)
    raise unless configs.is_a?(Array)
    configs.each_with_index do |config, idx|
      pool = create_pool(config.dup)
      begin
        connection = pool.connection
      rescue ConnectionError
        raise if idx == configs.length - 1
        next
      end
      @connection_pools[pool_key] = pool
      break connection
    end
  end
end

#current_poolObject



34
35
36
37
38
39
# File 'lib/switchman/connection_pool_proxy.rb', line 34

def current_pool
  pool = self.default_pool if active_shard.database_server == Shard.default.database_server && active_shackles_environment == :master && (active_shard == Shard.default || active_shard.database_server.shareable?)
  pool = @connection_pools[pool_key] ||= create_pool unless pool
  pool.shard = active_shard
  pool
end

#default_poolObject



16
17
18
# File 'lib/switchman/connection_pool_proxy.rb', line 16

def default_pool
  @default_pool
end