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

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

#clear_idle_connections!(since_when) ⇒ Object



77
78
79
# File 'lib/switchman/connection_pool_proxy.rb', line 77

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

#connectionObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/switchman/connection_pool_proxy.rb', line 44

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

#current_poolObject



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

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



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

def default_pool
  @default_pool
end