Class: ActiveRecordHostPool::PoolProxy

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_config) ⇒ PoolProxy

Returns a new instance of PoolProxy.



28
29
30
31
32
# File 'lib/active_record_host_pool/pool_proxy.rb', line 28

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

Instance Attribute Details

#pool_configObject (readonly)

Returns the value of attribute pool_config.



44
45
46
# File 'lib/active_record_host_pool/pool_proxy.rb', line 44

def pool_config
  @pool_config
end

Instance Method Details

#__getobj__Object



34
35
36
# File 'lib/active_record_host_pool/pool_proxy.rb', line 34

def __getobj__
  _connection_pool
end

#__setobj__(pool_config) ⇒ Object



38
39
40
41
42
# File 'lib/active_record_host_pool/pool_proxy.rb', line 38

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

#_unproxied_connection(*args) ⇒ Object



54
55
56
# File 'lib/active_record_host_pool/pool_proxy.rb', line 54

def _unproxied_connection(*args)
  _connection_pool.connection(*args)
end

#automatic_reconnect=(value) ⇒ Object



88
89
90
91
92
93
# File 'lib/active_record_host_pool/pool_proxy.rb', line 88

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

  p.automatic_reconnect = value
end

#checkin(cx) ⇒ Object



65
66
67
68
# File 'lib/active_record_host_pool/pool_proxy.rb', line 65

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



60
61
62
63
# File 'lib/active_record_host_pool/pool_proxy.rb', line 60

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

#clear_reloadable_connections!Object



95
96
97
98
# File 'lib/active_record_host_pool/pool_proxy.rb', line 95

def clear_reloadable_connections!
  _connection_pool.clear_reloadable_connections!
  _clear_connection_proxy_cache
end

#connection(*args) ⇒ Object



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

def 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

#discard!Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_record_host_pool/pool_proxy.rb', line 114

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_record_host_pool/pool_proxy.rb', line 77

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

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

#flush!Object



107
108
109
110
111
112
# File 'lib/active_record_host_pool/pool_proxy.rb', line 107

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

  p.flush!
end

#release_connection(*args) ⇒ Object



100
101
102
103
104
105
# File 'lib/active_record_host_pool/pool_proxy.rb', line 100

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

  p.release_connection(*args)
end

#with_connectionObject



70
71
72
73
74
75
# File 'lib/active_record_host_pool/pool_proxy.rb', line 70

def with_connection
  cx = checkout
  yield cx
ensure
  checkin cx
end