Class: SolidRedis::ClusterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_redis/cluster_config.rb

Overview

Immutable, Ractor-shareable description of a Redis Cluster: seed nodes and per-node client options. Slot tables, node clients and sockets live in a per-Ractor ClusterState, exactly like SentinelConfig/SentinelState.

Constant Summary collapse

LOCAL_STATE_KEY =
:solid_redis_cluster_states
DEFAULT_PORT =
6379
DEFAULT_MAX_REDIRECTIONS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes:, max_redirections: DEFAULT_MAX_REDIRECTIONS, **redis_options) ⇒ ClusterConfig

Returns a new instance of ClusterConfig.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/solid_redis/cluster_config.rb', line 14

def initialize(nodes:, max_redirections: DEFAULT_MAX_REDIRECTIONS, **redis_options)
  endpoints = Array(nodes).map { |endpoint| normalize_endpoint(endpoint) }
  @node_endpoints = Shareable.copy(endpoints, label: "nodes")
  raise ArgumentError, "At least one cluster node is required" if @node_endpoints.empty?

  @max_redirections = Integer(max_redirections)
  raise ArgumentError, "max_redirections must not be negative" if @max_redirections.negative?

  @reconnect_attempts = Integer(redis_options.fetch(:reconnect_attempts, 1))
  raise ArgumentError, "reconnect_attempts must not be negative" if @reconnect_attempts.negative?
  raise ArgumentError, "Redis Cluster only supports database 0" if redis_options.fetch(:db, 0) != 0

  @redis_client_options = Shareable.copy(redis_options, label: "Redis options")
  Ractor.make_shareable(self)
end

Instance Attribute Details

#max_redirections ⇒ Object (readonly)

Returns the value of attribute max_redirections.



12
13
14
# File 'lib/solid_redis/cluster_config.rb', line 12

def max_redirections
  @max_redirections
end

#node_endpoints ⇒ Object (readonly)

Returns the value of attribute node_endpoints.



12
13
14
# File 'lib/solid_redis/cluster_config.rb', line 12

def node_endpoints
  @node_endpoints
end

#reconnect_attempts ⇒ Object (readonly)

Returns the value of attribute reconnect_attempts.



12
13
14
# File 'lib/solid_redis/cluster_config.rb', line 12

def reconnect_attempts
  @reconnect_attempts
end

#redis_client_options ⇒ Object (readonly)

Returns the value of attribute redis_client_options.



12
13
14
# File 'lib/solid_redis/cluster_config.rb', line 12

def redis_client_options
  @redis_client_options
end

Instance Method Details

#cluster? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/solid_redis/cluster_config.rb', line 34

def cluster?
  true
end

#discovered? ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/solid_redis/cluster_config.rb', line 38

def discovered?
  state.discovered?
end

#inspect ⇒ Object



71
72
73
# File 'lib/solid_redis/cluster_config.rb', line 71

def inspect
  "#<#{self.class.name} nodes=#{node_endpoints.length}>"
end

#new_client(**options) ⇒ Object



56
57
58
# File 'lib/solid_redis/cluster_config.rb', line 56

def new_client(**options)
  ClusterClient.new(self, **options)
end

#new_pool(**options) ⇒ Object



60
61
62
# File 'lib/solid_redis/cluster_config.rb', line 60

def new_pool(**options)
  Pool.new(self, **options)
end

#nodes ⇒ Object



42
43
44
# File 'lib/solid_redis/cluster_config.rb', line 42

def nodes
  state.nodes
end

#notify(event, *arguments) ⇒ Object



64
65
66
67
68
69
# File 'lib/solid_redis/cluster_config.rb', line 64

def notify(event, *arguments)
  callbacks = redis_client_options[:callbacks]
  return unless callbacks&.respond_to?(event)

  callbacks.respond_with(event, *arguments)
end

#refresh ⇒ Object



51
52
53
54
# File 'lib/solid_redis/cluster_config.rb', line 51

def refresh
  state.refresh
  self
end

#reset ⇒ Object



46
47
48
49
# File 'lib/solid_redis/cluster_config.rb', line 46

def reset
  state.reset
  self
end

#sentinel? ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/solid_redis/cluster_config.rb', line 30

def sentinel?
  false
end

#state ⇒ Object

Per-Ractor runtime state, created on first use in each Ractor.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/solid_redis/cluster_config.rb', line 76

def state
  registry = Ractor.current[LOCAL_STATE_KEY]
  unless registry
    registry = { mutex: Mutex.new, states: {} }
    Ractor.current[LOCAL_STATE_KEY] = registry
  end

  registry[:mutex].synchronize do
    registry[:states][self] ||= ClusterState.new(self)
  end
end