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.



15
16
17
18
19
# File 'lib/active_record_host_pool/pool_proxy.rb', line 15

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

Instance Method Details

#__getobj__Object



21
22
23
# File 'lib/active_record_host_pool/pool_proxy.rb', line 21

def __getobj__
  _connection_pool
end

#__setobj__(spec) ⇒ Object



25
26
27
28
29
# File 'lib/active_record_host_pool/pool_proxy.rb', line 25

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

#automatic_reconnect=(value) ⇒ Object



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

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



80
81
82
83
# File 'lib/active_record_host_pool/pool_proxy.rb', line 80

def clear_reloadable_connections!
  _connection_pool.clear_reloadable_connections!
  _clear_connection_proxy_cache
end

#connection(*args) ⇒ Object



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

def connection(*args)
  begin
    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
end

#disconnect!Object



66
67
68
69
70
71
72
# 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

#specObject



31
32
33
# File 'lib/active_record_host_pool/pool_proxy.rb', line 31

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