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.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/switchman/connection_pool_proxy.rb', line 24

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.



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

def category
  @category
end

#schema_cacheObject (readonly)

Returns the value of attribute schema_cache.



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

def schema_cache
  @schema_cache
end

Instance Method Details

#active_guard_rail_environmentObject



42
43
44
# File 'lib/switchman/connection_pool_proxy.rb', line 42

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

#active_shardObject



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

def active_shard
  Shard.current(@category)
end

#automatic_reconnect=(value) ⇒ Object



106
107
108
# File 'lib/switchman/connection_pool_proxy.rb', line 106

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

#clear_idle_connections!(since_when) ⇒ Object



110
111
112
# File 'lib/switchman/connection_pool_proxy.rb', line 110

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

#connection(switch_shard: true) ⇒ Object



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

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



54
55
56
# File 'lib/switchman/connection_pool_proxy.rb', line 54

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

#current_poolObject



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

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



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

def default_pool
  @default_pool
end

#discard!Object



102
103
104
# File 'lib/switchman/connection_pool_proxy.rb', line 102

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



83
84
85
# File 'lib/switchman/connection_pool_proxy.rb', line 83

def get_schema_cache(_connection)
  @schema_cache
end

#remove_shard!(shard) ⇒ Object



114
115
116
# File 'lib/switchman/connection_pool_proxy.rb', line 114

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