Module: SlavePools

Defined in:
lib/slave_pools.rb,
lib/slave_pools/pool.rb,
lib/slave_pools/pools.rb,
lib/slave_pools/config.rb,
lib/slave_pools/engine.rb,
lib/slave_pools/hijack.rb,
lib/slave_pools/version.rb,
lib/slave_pools/query_cache.rb,
lib/slave_pools/connection_proxy.rb,
lib/slave_pools/active_record_extensions.rb

Defined Under Namespace

Modules: ActiveRecordExtensions, Hijack, QueryCache Classes: Config, ConnectionProxy, Engine, Pool, Pools

Constant Summary collapse

VERSION =
"1.2.0"

Class Method Summary collapse

Class Method Details

.configObject



16
17
18
# File 'lib/slave_pools.rb', line 16

def config
  @config ||= SlavePools::Config.new
end

.currentObject



35
36
37
# File 'lib/slave_pools.rb', line 35

def current
  proxy.current
end

.log(level, message) ⇒ Object



55
56
57
# File 'lib/slave_pools.rb', line 55

def log(level, message)
  logger.send(level, "[SlavePools] #{message}")
end

.loggerObject



59
60
61
# File 'lib/slave_pools.rb', line 59

def logger
  ActiveRecord::Base.logger
end

.next_slave!Object



39
40
41
# File 'lib/slave_pools.rb', line 39

def next_slave!
  proxy.next_slave!
end

.poolsObject



51
52
53
# File 'lib/slave_pools.rb', line 51

def pools
  Thread.current[:slave_pools] ||= SlavePools::Pools.new
end

.proxyObject



28
29
30
31
32
33
# File 'lib/slave_pools.rb', line 28

def proxy
  Thread.current[:slave_pools_proxy] ||= SlavePools::ConnectionProxy.new(
    ActiveRecord::Base,
    SlavePools.pools
  )
end

.setup!Object



20
21
22
23
24
25
26
# File 'lib/slave_pools.rb', line 20

def setup!
  ConnectionProxy.generate_safe_delegations

  ActiveRecord::Base.send(:extend, SlavePools::Hijack)

  log :info, "Proxy loaded with: #{pools.keys.join(', ')}"
end

.with_masterObject



47
48
49
# File 'lib/slave_pools.rb', line 47

def with_master
  proxy.with_master{ yield }
end

.with_pool(*a) ⇒ Object



43
44
45
# File 'lib/slave_pools.rb', line 43

def with_pool(*a)
  proxy.with_pool(*a){ yield }
end