Class: ActiveRecordHostPool::PoolProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ PoolProxy

Returns a new instance of PoolProxy.



17
18
19
20
21
# File 'lib/active_record_host_pool/pool_proxy.rb', line 17

def initialize(spec)
  super(spec)
  @spec = spec
  @config = spec.config
end

Instance Method Details

#__getobj__Object



23
24
25
# File 'lib/active_record_host_pool/pool_proxy.rb', line 23

def __getobj__
  _connection_pool
end

#__setobj__(spec) ⇒ Object



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

def __setobj__(spec)
  @spec = spec
  @config = spec.config
  @_pool_key = nil
end

#automatic_reconnect=(value) ⇒ Object



75
76
77
78
79
80
# File 'lib/active_record_host_pool/pool_proxy.rb', line 75

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

  p.automatic_reconnect = value if p.respond_to?(:automatic_reconnect=)
end

#checkin(cx) ⇒ Object



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

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



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

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

#clear_reloadable_connections!Object



82
83
84
85
# File 'lib/active_record_host_pool/pool_proxy.rb', line 82

def clear_reloadable_connections!
  _connection_pool.clear_reloadable_connections!
  _clear_connection_proxy_cache
end

#connection(*args) ⇒ Object



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

def connection(*args)
  real_connection = _connection_pool.connection(*args)
  _connection_proxy_for(real_connection, @config[:database])
rescue Exception => e
  if rescuable_errors.any? { |r| e.is_a?(r) }
    _connection_pools.delete(_pool_key)
  end
  Kernel.raise(e)
end

#discard!Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/active_record_host_pool/pool_proxy.rb', line 101

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 #spec 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



66
67
68
69
70
71
72
73
# File 'lib/active_record_host_pool/pool_proxy.rb', line 66

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

  p.disconnect!
  p.automatic_reconnect = true if p.respond_to?(:automatic_reconnect=)
  _clear_connection_proxy_cache
end

#flush!Object



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

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

  p.flush!
end

#release_connection(*args) ⇒ Object



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

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

  p.release_connection(*args)
end

#specObject



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

def spec
  @spec
end

#with_connectionObject



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

def with_connection
  cx = checkout
  yield cx
ensure
  checkin cx
end