Class: ActiveRecordHostPool::PoolProxy

Inherits:
Delegator
  • Object
show all
Defined in:
lib/active_record_host_pool/pool_proxy.rb

Overview

Sits between ConnectionHandler and a bunch of different ConnectionPools (one per host).

Constant Summary collapse

RESCUABLE_DB_ERROR =
rescuable_db_error.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_config) ⇒ PoolProxy

Returns a new instance of PoolProxy.



35
36
37
38
39
40
# File 'lib/active_record_host_pool/pool_proxy.rb', line 35

def initialize(pool_config)
  super
  @pool_config = pool_config
  @config = pool_config.db_config.configuration_hash
  @mutex = Mutex.new
end

Instance Attribute Details

#pool_configObject (readonly)

Returns the value of attribute pool_config.



52
53
54
# File 'lib/active_record_host_pool/pool_proxy.rb', line 52

def pool_config
  @pool_config
end

Instance Method Details

#__getobj__Object



42
43
44
# File 'lib/active_record_host_pool/pool_proxy.rb', line 42

def __getobj__
  _connection_pool
end

#__setobj__(pool_config) ⇒ Object



46
47
48
49
50
# File 'lib/active_record_host_pool/pool_proxy.rb', line 46

def __setobj__(pool_config)
  @pool_config = pool_config
  @config = pool_config.db_config.configuration_hash
  @_pool_key = nil
end

#automatic_reconnect=(value) ⇒ Object



151
152
153
154
155
156
# File 'lib/active_record_host_pool/pool_proxy.rb', line 151

def automatic_reconnect=(value)
  p = _connection_pool(false)
  return unless p

  p.automatic_reconnect = value
end

#checkin(cx) ⇒ Object



91
92
93
94
# File 'lib/active_record_host_pool/pool_proxy.rb', line 91

def checkin(cx)
  cx = cx.unproxied
  _connection_pool.checkin(cx)
end

#checkout(*args, &block) ⇒ Object

by the time we are patched into ActiveRecord, the current thread has already established a connection. thus we need to patch both connection and checkout/checkin



86
87
88
89
# File 'lib/active_record_host_pool/pool_proxy.rb', line 86

def checkout(*args, &block)
  cx = _connection_pool.checkout(*args, &block)
  _connection_proxy_for(cx, @config[:database])
end

#clear_reloadable_connections!Object



158
159
160
161
# File 'lib/active_record_host_pool/pool_proxy.rb', line 158

def clear_reloadable_connections!
  _connection_pool.clear_reloadable_connections!
  _clear_connection_proxy_cache
end

#discard!Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/active_record_host_pool/pool_proxy.rb', line 177

def discard!
  p = _connection_pool(false)
  return unless p

  p.discard!

  # All connections in the pool (even if they're currently
  # leased!) have just been discarded, along with the pool itself.
  # Any further interaction with the pool (except #pool_config and #schema_cache)
  # is undefined.
  # Remove the connection for the given key so a new one can be created in its place
  _connection_pools.delete(_pool_key)
end

#disconnect!Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/active_record_host_pool/pool_proxy.rb', line 140

def disconnect!
  p = _connection_pool(false)
  return unless p

  @mutex.synchronize do
    p.disconnect!
    p.automatic_reconnect = true
    _clear_connection_proxy_cache
  end
end

#flush!Object



170
171
172
173
174
175
# File 'lib/active_record_host_pool/pool_proxy.rb', line 170

def flush!
  p = _connection_pool(false)
  return unless p

  p.flush!
end

#release_connection(*args) ⇒ Object



163
164
165
166
167
168
# File 'lib/active_record_host_pool/pool_proxy.rb', line 163

def release_connection(*args)
  p = _connection_pool(false)
  return unless p

  p.release_connection(*args)
end