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
28
29
30
31
32
33
34
# 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 = default_pool.get_schema_cache(nil) if ::Rails.version >= '6'
  @schema_cache = SchemaCache.new(self) unless @schema_cache.is_a?(SchemaCache)
  if ::Rails.version >= '6'
    @default_pool.set_schema_cache(@schema_cache)
    @connection_pools.each_value do |pool|
      pool.set_schema_cache(@schema_cache)
    end
  end
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_guard_rail_environmentObject



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

def active_guard_rail_environment
  ::Rails.env.test? ? :primary : active_shard.database_server.guard_rail_environment
end

#active_shardObject



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

def active_shard
  Shard.current(@category)
end

#automatic_reconnect=(value) ⇒ Object



104
105
106
# File 'lib/switchman/connection_pool_proxy.rb', line 104

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

#clear_idle_connections!(since_when) ⇒ Object



108
109
110
# File 'lib/switchman/connection_pool_proxy.rb', line 108

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

#connection(switch_shard: true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/switchman/connection_pool_proxy.rb', line 56

def connection(switch_shard: true)
  pool = current_pool
  begin
    connection = pool.connection(switch_shard: switch_shard)
    connection.instance_variable_set(:@schema_cache, @schema_cache) unless ::Rails.version >= '6'
    connection
  rescue ConnectionError
    raise if active_shard.database_server == Shard.default.database_server && active_guard_rail_environment == :primary
    configs = active_shard.database_server.config(active_guard_rail_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) unless ::Rails.version >= '6'
      rescue ConnectionError
        raise if idx == configs.length - 1
        next
      end
      @connection_pools[pool_key] = pool
      break connection
    end
  end
end

#connectionsObject



52
53
54
# File 'lib/switchman/connection_pool_proxy.rb', line 52

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

#current_poolObject



44
45
46
47
48
49
50
# File 'lib/switchman/connection_pool_proxy.rb', line 44

def current_pool
  current_active_shard = active_shard
  pool = self.default_pool if current_active_shard.database_server == Shard.default.database_server && active_guard_rail_environment == :primary && (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



100
101
102
# File 'lib/switchman/connection_pool_proxy.rb', line 100

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

#get_schema_cache(_connection) ⇒ Object



81
82
83
# File 'lib/switchman/connection_pool_proxy.rb', line 81

def get_schema_cache(_connection)
  @schema_cache
end

#remove_shard!(shard) ⇒ Object



112
113
114
# File 'lib/switchman/connection_pool_proxy.rb', line 112

def remove_shard!(shard)
  connection_pools.each { |pool| pool.remove_shard!(shard) }
end