class Replidog::Proxy

Constants

REPLICABLE_METHOD_NAMES
REPLICABLE_METHOD_NAMES_REGEXP

Attributes

configuration[R]
index[W]

Public Class Methods

new(handler, configuration) click to toggle source
# File lib/replidog/proxy.rb, line 14
def initialize(handler, configuration)
  @handler = handler
  @configuration = configuration
  @lock = Mutex.new
end

Public Instance Methods

clear_active_slave_connections!() click to toggle source
# File lib/replidog/proxy.rb, line 56
def clear_active_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.release_connection
  end
end
clear_all_slave_connections!() click to toggle source
# File lib/replidog/proxy.rb, line 68
def clear_all_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.automatic_reconnect = false
    pool.disconnect!
  end
end
clear_query_cache() click to toggle source
# File lib/replidog/proxy.rb, line 95
def clear_query_cache
  @handler.clear_query_cache
end
clear_query_cache_for_slaves() click to toggle source
# File lib/replidog/proxy.rb, line 99
def clear_query_cache_for_slaves
  slave_connection_pool_table.values.each do |pool|
    pool.connection.clear_query_cache
  end
end
clear_reloadable_slave_connections!() click to toggle source
# File lib/replidog/proxy.rb, line 62
def clear_reloadable_slave_connections!
  slave_connection_pool_table.each_value do |pool|
    pool.clear_reloadable_connections!
  end
end
connected?() click to toggle source
# File lib/replidog/proxy.rb, line 52
def connected?
  current_model.connection_handler.connected?(current_model) && slave_connection_pool_table.values.any?(&:connected?)
end
current_connection_name() click to toggle source
# File lib/replidog/proxy.rb, line 28
def current_connection_name
  Thread.current['replidog.current_connection_name']
end
current_connection_name=(connection_name) click to toggle source
# File lib/replidog/proxy.rb, line 32
def current_connection_name=(connection_name)
  Thread.current['replidog.current_connection_name'] = connection_name
end
current_model() click to toggle source
# File lib/replidog/proxy.rb, line 20
def current_model
  Thread.current['replidog.current_model']
end
current_model=(model) click to toggle source
# File lib/replidog/proxy.rb, line 24
def current_model=(model)
  Thread.current['replidog.current_model'] = model.is_a?(ActiveRecord::Base) ? model.class : model
end
disable_query_cache!() click to toggle source
# File lib/replidog/proxy.rb, line 85
def disable_query_cache!
  @handler.disable_query_cache!
end
disable_query_cache_for_slaves!() click to toggle source
# File lib/replidog/proxy.rb, line 89
def disable_query_cache_for_slaves!
  slave_connection_pool_table.values.each do |pool|
    pool.connection.disable_query_cache!
  end
end
enable_query_cache!() click to toggle source
# File lib/replidog/proxy.rb, line 75
def enable_query_cache!
  @handler.enable_query_cache!
end
enable_query_cache_for_slaves!() click to toggle source
# File lib/replidog/proxy.rb, line 79
def enable_query_cache_for_slaves!
  slave_connection_pool_table.each_value do |pool|
    pool.connection.enable_query_cache!
  end
end
lock(locks = true) click to toggle source
# File lib/replidog/proxy.rb, line 44
def lock(locks = true)
  old_connection_name = current_connection_name
  self.current_connection_name ||= :master
  current_connection.lock(locks)
ensure
  self.current_connection_name = old_connection_name
end
transaction(options = {}, &block) click to toggle source
# File lib/replidog/proxy.rb, line 36
def transaction(options = {}, &block)
  old_connection_name = current_connection_name
  self.current_connection_name ||= :master
  current_connection.transaction(options, &block)
ensure
  self.current_connection_name = old_connection_name
end

Private Instance Methods

connection_by_method_name(method_name) click to toggle source
# File lib/replidog/proxy.rb, line 119
def connection_by_method_name(method_name)
  REPLICABLE_METHOD_NAMES_REGEXP === method_name ? slave_connection : master_connection
end
current_connection() click to toggle source
# File lib/replidog/proxy.rb, line 123
def current_connection
  if current_connection_name.to_s == "master"
    master_connection
  else
    slave_connection_pool_table[current_connection_name.to_s].try(:connection) or raise_connection_not_found
  end
end
increment_slave_connection_index() click to toggle source
# File lib/replidog/proxy.rb, line 167
def increment_slave_connection_index
  self.index = (index + 1) % slave_connection_pool_table.size
end
index() click to toggle source
# File lib/replidog/proxy.rb, line 171
def index
  @index ||= rand(slave_connection_pool_table.size)
end
master_connection() click to toggle source
# File lib/replidog/proxy.rb, line 131
def master_connection
  current_model.retrieve_connection
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/replidog/proxy.rb, line 107
def method_missing(method_name, *args, &block)
  if current_connection_name
    current_connection.send(method_name, *args, &block)
  else
    connection_by_method_name(method_name).send(method_name, *args, &block)
  end
end
raise_connection_not_found() click to toggle source
# File lib/replidog/proxy.rb, line 157
def raise_connection_not_found
  raise StandardError, "connection #{current_connection_name} is not found"
end
replicated?() click to toggle source
# File lib/replidog/proxy.rb, line 143
def replicated?
  replications
end
replications() click to toggle source
# File lib/replidog/proxy.rb, line 147
def replications
  @configuration.config[:replications] || []
end
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/replidog/proxy.rb, line 115
def respond_to_missing?(method, *args)
  master_connection.respond_to?(method, *args) || super
end
slave_connection() click to toggle source
# File lib/replidog/proxy.rb, line 135
def slave_connection
  slave_connection_pool.connection
end
slave_connection_index() click to toggle source
# File lib/replidog/proxy.rb, line 161
def slave_connection_index
  @lock.synchronize do
    index.tap { increment_slave_connection_index }
  end
end
slave_connection_pool() click to toggle source
# File lib/replidog/proxy.rb, line 139
def slave_connection_pool
  slave_connection_pool_table.values[slave_connection_index]
end
slave_connection_pool_table() click to toggle source
# File lib/replidog/proxy.rb, line 151
def slave_connection_pool_table
  @slave_connection_pools ||= replications.inject({}) do |table, (name, configuration)|
    table.merge(name => ConnectionPoolCreater.create(configuration))
  end
end