Class: Switchman::ConnectionPoolProxy
- Inherits:
-
Object
- Object
- Switchman::ConnectionPoolProxy
- Defined in:
- lib/switchman/connection_pool_proxy.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#schema_cache ⇒ Object
readonly
Returns the value of attribute schema_cache.
Instance Method Summary collapse
- #active_guard_rail_environment ⇒ Object
- #active_shard ⇒ Object
- #automatic_reconnect=(value) ⇒ Object
- #clear_idle_connections!(since_when) ⇒ Object
- #connection(switch_shard: true) ⇒ Object
- #connections ⇒ Object
- #current_pool ⇒ Object
- #default_pool ⇒ Object
- #discard! ⇒ Object
- #get_schema_cache(_connection) ⇒ Object
-
#initialize(category, default_pool, shard_connection_pools) ⇒ ConnectionPoolProxy
constructor
A new instance of ConnectionPoolProxy.
- #remove_shard!(shard) ⇒ Object
- #set_schema_cache(cache) ⇒ Object
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
#category ⇒ Object (readonly)
Returns the value of attribute category.
18 19 20 |
# File 'lib/switchman/connection_pool_proxy.rb', line 18 def category @category end |
#schema_cache ⇒ Object (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_environment ⇒ Object
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_shard ⇒ Object
38 39 40 |
# File 'lib/switchman/connection_pool_proxy.rb', line 38 def active_shard Shard.current(@category) end |
#automatic_reconnect=(value) ⇒ Object
110 111 112 |
# File 'lib/switchman/connection_pool_proxy.rb', line 110 def automatic_reconnect=(value) connection_pools.each { |pool| pool.automatic_reconnect = value } end |
#clear_idle_connections!(since_when) ⇒ Object
114 115 116 |
# File 'lib/switchman/connection_pool_proxy.rb', line 114 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 |
#connections ⇒ Object
54 55 56 |
# File 'lib/switchman/connection_pool_proxy.rb', line 54 def connections connection_pools.map(&:connections).inject([], &:+) end |
#current_pool ⇒ Object
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_pool ⇒ Object
20 21 22 |
# File 'lib/switchman/connection_pool_proxy.rb', line 20 def default_pool @default_pool end |
#discard! ⇒ Object
106 107 108 |
# File 'lib/switchman/connection_pool_proxy.rb', line 106 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
118 119 120 |
# File 'lib/switchman/connection_pool_proxy.rb', line 118 def remove_shard!(shard) connection_pools.each { |pool| pool.remove_shard!(shard) } end |
#set_schema_cache(cache) ⇒ Object
87 88 89 |
# File 'lib/switchman/connection_pool_proxy.rb', line 87 def set_schema_cache(cache) @schema_cache.copy_values(cache) end |