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.



22
23
24
25
26
27
# File 'lib/switchman/connection_pool_proxy.rb', line 22

def initialize(category, default_pool, shard_connection_pools)
  @category = category
  @default_pool = default_pool
  @connection_pools = shard_connection_pools
  @schema_cache = SchemaCache.new(self)
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



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

def category
  @category
end

#schema_cacheObject (readonly)

Returns the value of attribute schema_cache.



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

def schema_cache
  @schema_cache
end

Instance Method Details

#active_shackles_environmentObject



33
34
35
# File 'lib/switchman/connection_pool_proxy.rb', line 33

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

#active_shardObject



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

def active_shard
  Shard.current(@category)
end

#automatic_reconnect=(value) ⇒ Object



93
94
95
# File 'lib/switchman/connection_pool_proxy.rb', line 93

def automatic_reconnect=(value)
  connection_pools.each { |pool| pool.automatic_reconnect = value }
end

#clear_idle_connections!(since_when) ⇒ Object



97
98
99
# File 'lib/switchman/connection_pool_proxy.rb', line 97

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

#connectionObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/switchman/connection_pool_proxy.rb', line 49

def connection
  pool = current_pool
  begin
    connection = pool.connection
    connection.instance_variable_set(:@schema_cache, @schema_cache)
    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
        connection.instance_variable_set(:@schema_cache, @schema_cache)
      rescue ConnectionError
        raise if idx == configs.length - 1
        next
      end
      @connection_pools[pool_key] = pool
      break connection
    end
  end
end

#connectionsObject



45
46
47
# File 'lib/switchman/connection_pool_proxy.rb', line 45

def connections
  connection_pools.map(&:connections).inject([], &:+)
end

#current_poolObject



37
38
39
40
41
42
43
# File 'lib/switchman/connection_pool_proxy.rb', line 37

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

#default_poolObject



18
19
20
# File 'lib/switchman/connection_pool_proxy.rb', line 18

def default_pool
  @default_pool
end

#discard!Object



89
90
91
# File 'lib/switchman/connection_pool_proxy.rb', line 89

def discard!
  # this breaks everything if i try to pass it onto the pools and i'm not sure why
end