class Knockoff::ReplicaConnectionPool
Attributes
pool[R]
Public Class Methods
new(config_keys)
click to toggle source
# File lib/knockoff/replica_connection_pool.rb, line 5 def initialize(config_keys) @pool = Concurrent::Hash.new config_keys.each do |config_key| @pool[config_key] = connection_class(config_key) end end
Public Instance Methods
clear_all_active_connections!()
click to toggle source
# File lib/knockoff/replica_connection_pool.rb, line 13 def clear_all_active_connections! @pool.each do |_name, klass| klass.clear_active_connections! end end
connection_class(config_key)
click to toggle source
Based off of code from replica_pools gem generates a unique ActiveRecord::Base
subclass for a single replica
# File lib/knockoff/replica_connection_pool.rb, line 39 def connection_class(config_key) # Config key is of schema 'knockoff_replica_n' class_name = "KnockoffReplica#{config_key.split('_').last}" # TODO: Hardcoding the uri string feels meh. Either set the database config # or reference ENV instead Knockoff.module_eval %Q{ class #{class_name} < ActiveRecord::Base self.abstract_class = true establish_connection :#{config_key} def self.connection_config configurations[#{config_key.to_s.inspect}] end end }, __FILE__, __LINE__ Knockoff.const_get(class_name) end
disconnect_all_replicas!()
click to toggle source
# File lib/knockoff/replica_connection_pool.rb, line 19 def disconnect_all_replicas! @pool.each do |_name, klass| klass.connection.disconnect! end end
random_replica_connection()
click to toggle source
# File lib/knockoff/replica_connection_pool.rb, line 33 def random_replica_connection @pool[@pool.keys.sample] end
reconnect_all_replicas!()
click to toggle source
Assumes that the config has been updated to something new, and simply reconnects with the config.
# File lib/knockoff/replica_connection_pool.rb, line 27 def reconnect_all_replicas! @pool.each do |database_key, klass| klass.establish_connection database_key.to_sym end end