Class: RedisClient::Cluster::Node::BaseTopology

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_client/cluster/node/base_topology.rb

Constant Summary collapse

IGNORE_GENERIC_CONFIG_KEYS =
%i[url host port path].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, concurrent_worker, **kwargs) ⇒ BaseTopology

Returns a new instance of BaseTopology.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/redis_client/cluster/node/base_topology.rb', line 10

def initialize(pool, concurrent_worker, **kwargs)
  @pool = pool
  @clients = {}
  @client_options = kwargs.reject { |k, _| IGNORE_GENERIC_CONFIG_KEYS.include?(k) }
  @concurrent_worker = concurrent_worker
  @replications = EMPTY_HASH
  @primary_node_keys = EMPTY_ARRAY
  @replica_node_keys = EMPTY_ARRAY
  @primary_clients = EMPTY_ARRAY
  @replica_clients = EMPTY_ARRAY
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



8
9
10
# File 'lib/redis_client/cluster/node/base_topology.rb', line 8

def clients
  @clients
end

#primary_clientsObject (readonly)

Returns the value of attribute primary_clients.



8
9
10
# File 'lib/redis_client/cluster/node/base_topology.rb', line 8

def primary_clients
  @primary_clients
end

#replica_clientsObject (readonly)

Returns the value of attribute replica_clients.



8
9
10
# File 'lib/redis_client/cluster/node/base_topology.rb', line 8

def replica_clients
  @replica_clients
end

Instance Method Details

#any_primary_node_key(seed: nil) ⇒ Object



22
23
24
25
# File 'lib/redis_client/cluster/node/base_topology.rb', line 22

def any_primary_node_key(seed: nil)
  random = seed.nil? ? Random : Random.new(seed)
  @primary_node_keys.sample(random: random)
end

#process_topology_update!(replications, options) ⇒ Object

rubocop:disable Metrics/AbcSize



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/redis_client/cluster/node/base_topology.rb', line 27

def process_topology_update!(replications, options) # rubocop:disable Metrics/AbcSize
  @replications = replications.freeze
  @primary_node_keys = @replications.keys.sort.select { |k| options.key?(k) }.freeze
  @replica_node_keys = @replications.values.flatten.sort.select { |k| options.key?(k) }.freeze

  # Disconnect from nodes that we no longer want, and connect to nodes we're not connected to yet
  disconnect_from_unwanted_nodes(options)
  connect_to_new_nodes(options)

  @primary_clients, @replica_clients = @clients.partition { |k, _| @primary_node_keys.include?(k) }.map(&:to_h)
  @primary_clients.freeze
  @replica_clients.freeze
end