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

default_connection_handler[R]

easy failover to the default

Public Class Methods

new(default_connection_handler) click to toggle source
# 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

active_connections?() click to toggle source

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_shard(shard, connection_handler) click to toggle source

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
clear_active_connections!() click to toggle source

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
clear_all_connections!() click to toggle source
# File lib/active_replica/connection_handler.rb, line 107
def clear_all_connections!
  connection_handler_list.each(&:clear_all_connections!)
end
clear_reloadable_connections!() click to toggle source
# File lib/active_replica/connection_handler.rb, line 103
def clear_reloadable_connections!
  connection_handler_list.each(&:clear_reloadable_connections!)
end
connection_pool_list() click to toggle source

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
Also aliased as: connection_pools
connection_pools()
get_shard(shard) click to toggle source

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
shards() click to toggle source

get the list of shard names

# File lib/active_replica/connection_handler.rb, line 40
def shards
  @shard_to_connection_handler.keys
end
with_connection_handler(connection_handler) { || ... } click to toggle source

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
with_shard(shard) { || ... } click to toggle source

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

active_connection_handler() click to toggle source

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
active_connection_handler=(connection_handler) click to toggle source
# File lib/active_replica/connection_handler.rb, line 28
        def active_connection_handler=(connection_handler)
  ActiveReplica::RuntimeRegistry.connection_handler = connection_handler
end
connection_handler_list() click to toggle source

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