Class: ActiveReplica::ConnectionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/active_replica/connection_handler.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_connection_handler) ⇒ ConnectionHandler

Returns a new instance of ConnectionHandler.



13
14
15
16
# 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

Instance Attribute Details

#default_connection_handlerObject (readonly)

easy failover to the default



19
20
21
# File 'lib/active_replica/connection_handler.rb', line 19

def default_connection_handler
  @default_connection_handler
end

Instance Method Details

#active_connections?Boolean

the active connection method should just delegate

Returns:

  • (Boolean)


93
94
95
# File 'lib/active_replica/connection_handler.rb', line 93

def active_connections?
  connection_handler_list.any?(&:active_connections)
end

#add_shard(shard, connection_handler) ⇒ Object

add a shard with connection handler



34
35
36
# 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!Object

the clear connection methods can be delegated to each connection handler



99
100
101
# File 'lib/active_replica/connection_handler.rb', line 99

def clear_active_connections!
  connection_handler_list.each(&:clear_active_connections!)
end

#clear_all_connections!Object



107
108
109
# File 'lib/active_replica/connection_handler.rb', line 107

def clear_all_connections!
  connection_handler_list.each(&:clear_all_connections!)
end

#clear_reloadable_connections!Object



103
104
105
# File 'lib/active_replica/connection_handler.rb', line 103

def clear_reloadable_connections!
  connection_handler_list.each(&:clear_reloadable_connections!)
end

#connection_pool_listObject Also known as: connection_pools

the connection pool list is used for various cleaning tasks it should be modified to return the full list mapped over all children



81
82
83
# File 'lib/active_replica/connection_handler.rb', line 81

def connection_pool_list
  connection_handler_list.flat_map(&:connection_pool_list)
end

#get_shard(shard) ⇒ Object

get the shard with the connection handler



46
47
48
# 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

#shardsObject

get the list of shard names



40
41
42
# File 'lib/active_replica/connection_handler.rb', line 40

def shards
  @shard_to_connection_handler.keys
end

#with_connection_handler(connection_handler) ⇒ Object

switch the handler for the current thread and ensure its put back at the end



62
63
64
65
66
67
68
# 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) ⇒ Object

fetch the shard and use it



52
53
54
55
56
57
# 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