Class: ActiveRecordHostPool::PoolProxy
- Inherits:
-
Delegator
- Object
- Delegator
- ActiveRecordHostPool::PoolProxy
- 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
-
#pool_config ⇒ Object
readonly
Returns the value of attribute pool_config.
Instance Method Summary collapse
- #__getobj__ ⇒ Object
- #__setobj__(pool_config) ⇒ Object
- #_unproxied_connection(*args) ⇒ Object
- #active_connection? ⇒ Boolean (also: #active_connection)
- #automatic_reconnect=(value) ⇒ Object
- #checkin(cx) ⇒ Object
-
#checkout(*args, &block) ⇒ Object
by the time we are patched into ActiveRecord, the current thread has already established a connection.
- #clear_reloadable_connections! ⇒ Object
- #discard! ⇒ Object
- #disconnect! ⇒ Object
- #flush! ⇒ Object
-
#initialize(pool_config) ⇒ PoolProxy
constructor
A new instance of PoolProxy.
- #lease_connection(*args) ⇒ Object (also: #connection)
- #release_connection(*args) ⇒ Object
- #schema_cache ⇒ Object
-
#with_connection(prevent_permanent_checkout: false) ⇒ Object
rubocop:disable Lint/DuplicateMethods.
Constructor Details
#initialize(pool_config) ⇒ 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_config ⇒ Object (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 |
#_unproxied_connection(*args) ⇒ Object
63 64 65 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 63 def _unproxied_connection(*args) _connection_pool.lease_connection(*args) end |
#active_connection? ⇒ Boolean Also known as: active_connection
101 102 103 104 105 106 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 101 def active_connection? real_connection_lease = _connection_pool.send(:connection_lease) if real_connection_lease.connection _connection_proxy_for(real_connection_lease.connection, @config[:database]) end end |
#automatic_reconnect=(value) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 124 def automatic_reconnect=(value) p = _connection_pool(false) return unless p p.automatic_reconnect = value end |
#checkin(cx) ⇒ Object
74 75 76 77 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 74 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
69 70 71 72 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 69 def checkout(*args, &block) cx = _connection_pool.checkout(*args, &block) _connection_proxy_for(cx, @config[:database]) end |
#clear_reloadable_connections! ⇒ Object
131 132 133 134 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 131 def clear_reloadable_connections! _connection_pool.clear_reloadable_connections! _clear_connection_proxy_cache end |
#discard! ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 150 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
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 113 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
143 144 145 146 147 148 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 143 def flush! p = _connection_pool(false) return unless p p.flush! end |
#lease_connection(*args) ⇒ Object Also known as: connection
54 55 56 57 58 59 60 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 54 def lease_connection(*args) real_connection = _unproxied_connection(*args) _connection_proxy_for(real_connection, @config[:database]) rescue *RESCUABLE_DB_ERROR, ActiveRecord::NoDatabaseError, ActiveRecord::StatementInvalid _connection_pools.delete(_pool_key) Kernel.raise end |
#release_connection(*args) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 136 def release_connection(*args) p = _connection_pool(false) return unless p p.release_connection(*args) end |
#schema_cache ⇒ Object
109 110 111 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 109 def schema_cache @schema_cache ||= ActiveRecord::ConnectionAdapters::BoundSchemaReflection.new(_connection_pool.schema_reflection, self) end |
#with_connection(prevent_permanent_checkout: false) ⇒ Object
rubocop:disable Lint/DuplicateMethods
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_record_host_pool/pool_proxy.rb', line 79 def with_connection(prevent_permanent_checkout: false) # rubocop:disable Lint/DuplicateMethods real_connection_lease = _connection_pool.send(:connection_lease) sticky_was = real_connection_lease.sticky real_connection_lease.sticky = false if prevent_permanent_checkout if real_connection_lease.connection begin yield _connection_proxy_for(real_connection_lease.connection, @config[:database]) ensure real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was end else begin real_connection_lease.connection = _unproxied_connection yield _connection_proxy_for(real_connection_lease.connection, @config[:database]) ensure real_connection_lease.sticky = sticky_was if prevent_permanent_checkout && !sticky_was _connection_pool.release_connection(real_connection_lease) unless real_connection_lease.sticky end end end |