Class: SlavePools::ConnectionProxy

Inherits:
Object
  • Object
show all
Includes:
QueryCache
Defined in:
lib/slave_pools/connection_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryCache

#select_all

Constructor Details

#initialize(master, pools) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/slave_pools/connection_proxy.rb', line 29

def initialize(master, pools)
  @master       = master
  @slave_pools  = pools
  @master_depth = 0
  @current_pool = default_pool

  if SlavePools.config.defaults_to_master
    @current = master
  else
    @current = current_slave
  end

  # this ivar is for ConnectionAdapter compatibility
  # some gems (e.g. newrelic_rpm) will actually use
  # instance_variable_get(:@config) to find it.
  @config = current.connection_config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)

Proxies any unknown methods to master. Safe methods have been generated during ‘setup!`. Creates a method to speed up subsequent calls.



89
90
91
92
# File 'lib/slave_pools/connection_proxy.rb', line 89

def method_missing(method, *args, &block)
  generate_unsafe_delegation(method)
  send(method, *args, &block)
end

Instance Attribute Details

#currentObject

Returns the value of attribute current.



9
10
11
# File 'lib/slave_pools/connection_proxy.rb', line 9

def current
  @current
end

#current_poolObject

Returns the value of attribute current_pool.



9
10
11
# File 'lib/slave_pools/connection_proxy.rb', line 9

def current_pool
  @current_pool
end

#masterObject

Returns the value of attribute master.



8
9
10
# File 'lib/slave_pools/connection_proxy.rb', line 8

def master
  @master
end

#master_depthObject

Returns the value of attribute master_depth.



9
10
11
# File 'lib/slave_pools/connection_proxy.rb', line 9

def master_depth
  @master_depth
end

#slave_poolsObject

Returns the value of attribute slave_pools.



9
10
11
# File 'lib/slave_pools/connection_proxy.rb', line 9

def slave_pools
  @slave_pools
end

Class Method Details

.generate_safe_delegationsObject



12
13
14
15
16
# File 'lib/slave_pools/connection_proxy.rb', line 12

def generate_safe_delegations
  SlavePools.config.safe_methods.each do |method|
    generate_safe_delegation(method) unless instance_methods.include?(method)
  end
end

Instance Method Details

#current_slaveObject



76
77
78
# File 'lib/slave_pools/connection_proxy.rb', line 76

def current_slave
  current_pool.current
end

#next_slave!Object



71
72
73
74
# File 'lib/slave_pools/connection_proxy.rb', line 71

def next_slave!
  return if within_master_block?
  self.current = current_pool.next
end

#transaction(*args, &block) ⇒ Object



67
68
69
# File 'lib/slave_pools/connection_proxy.rb', line 67

def transaction(*args, &block)
  with_master { master.transaction(*args, &block) }
end

#with_masterObject



57
58
59
60
61
62
63
64
65
# File 'lib/slave_pools/connection_proxy.rb', line 57

def with_master
  last_conn = self.current
  self.current = master
  self.master_depth += 1
  yield
ensure
  self.master_depth = [master_depth - 1, 0].max
  self.current = last_conn
end

#with_pool(pool_name = 'default') ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/slave_pools/connection_proxy.rb', line 47

def with_pool(pool_name = 'default')
  last_conn, last_pool = self.current, self.current_pool
  self.current_pool = slave_pools[pool_name.to_sym] || default_pool
  self.current = current_slave unless within_master_block?
  yield
ensure
  self.current_pool = last_pool
  self.current      = last_conn
end