class ActiveReplica::ConnectionHandler
This class works the same as the default ActiveRecord ConnectionHandler
with each method carefully copied and delegated appropriately
for single pool operations it delegates to the current active handler for operations over the whole connecton pool list, it delegates to each handler
Attributes
easy failover to the default
Public Class Methods
# File lib/active_replica/connection_handler.rb, line 13 def initialize(default_connection_handler) @shard_to_connection_handler = Concurrent::Map.new(initial_capacity: 2) @default_connection_handler = default_connection_handler end
Public Instance Methods
the active connection method should just delegate
# File lib/active_replica/connection_handler.rb, line 93 def active_connections? connection_handler_list.any?(&:active_connections) end
add a shard with connection handler
# File lib/active_replica/connection_handler.rb, line 34 def add_shard(shard, connection_handler) @shard_to_connection_handler[shard] = connection_handler end
the clear connection methods can be delegated to each connection handler
# File lib/active_replica/connection_handler.rb, line 99 def clear_active_connections! connection_handler_list.each(&:clear_active_connections!) end
# File lib/active_replica/connection_handler.rb, line 107 def clear_all_connections! connection_handler_list.each(&:clear_all_connections!) end
# File lib/active_replica/connection_handler.rb, line 103 def clear_reloadable_connections! connection_handler_list.each(&:clear_reloadable_connections!) end
the connection pool list is used for various cleaning tasks it should be modified to return the full list mapped over all children
# File lib/active_replica/connection_handler.rb, line 81 def connection_pool_list connection_handler_list.flat_map(&:connection_pool_list) end
get the shard with the connection handler
# File lib/active_replica/connection_handler.rb, line 46 def get_shard(shard) @shard_to_connection_handler[shard] or fail "no handler for shard #{shard.inspect}" end
get the list of shard names
# File lib/active_replica/connection_handler.rb, line 40 def shards @shard_to_connection_handler.keys end
switch the handler for the current thread and ensure its put back at the end
# File lib/active_replica/connection_handler.rb, line 62 def with_connection_handler(connection_handler) before = active_connection_handler self.active_connection_handler = connection_handler yield ensure self.active_connection_handler = before end
fetch the shard and use it
# File lib/active_replica/connection_handler.rb, line 52 def with_shard(shard) connection_handler = get_shard(shard) with_connection_handler(connection_handler) do yield end end
Private Instance Methods
the key to the whole thing let us switch the active connection handler
# File lib/active_replica/connection_handler.rb, line 24 def active_connection_handler ActiveReplica::RuntimeRegistry.connection_handler || default_connection_handler end
# File lib/active_replica/connection_handler.rb, line 28 def active_connection_handler=(connection_handler) ActiveReplica::RuntimeRegistry.connection_handler = connection_handler end
grab the full list of connection handlers to enable clean up methods
# File lib/active_replica/connection_handler.rb, line 73 def connection_handler_list @shard_to_connection_handler.values + [default_connection_handler].compact end