Class: RedisClient::ClusterConfig

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

Constant Summary collapse

DEFAULT_HOST =
'127.0.0.1'
DEFAULT_PORT =
6379
DEFAULT_SCHEME =
'redis'
SECURE_SCHEME =
'rediss'
DEFAULT_NODES =
["#{DEFAULT_SCHEME}://#{DEFAULT_HOST}:#{DEFAULT_PORT}"].freeze
VALID_SCHEMES =
[DEFAULT_SCHEME, SECURE_SCHEME].freeze
VALID_NODES_KEYS =
i[ssl username password host port db].freeze
MERGE_CONFIG_KEYS =
i[ssl username password].freeze
IGNORE_GENERIC_CONFIG_KEYS =
i[url host port path].freeze
MAX_WORKERS =
Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
InvalidClientConfigError =
Class.new(::RedisClient::Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes: DEFAULT_NODES, replica: false, replica_affinity: :random, fixed_hostname: '', concurrency: nil, client_implementation: ::RedisClient::Cluster, **client_config) ⇒ ClusterConfig

Returns a new instance of ClusterConfig.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redis_client/cluster_config.rb', line 26

def initialize(
  nodes: DEFAULT_NODES,
  replica: false,
  replica_affinity: :random,
  fixed_hostname: '',
  concurrency: nil,
  client_implementation: ::RedisClient::Cluster, # for redis gem
  **client_config
)

  @replica = true & replica
  @replica_affinity = replica_affinity.to_s.to_sym
  @fixed_hostname = fixed_hostname.to_s
  @node_configs = build_node_configs(nodes.dup)
  client_config = client_config.reject { |k, _| IGNORE_GENERIC_CONFIG_KEYS.include?(k) }
  @command_builder = client_config.fetch(:command_builder, ::RedisClient::CommandBuilder)
  @client_config = merge_generic_config(client_config, @node_configs)
  @concurrency = merge_concurrency_option(concurrency)
  @client_implementation = client_implementation
  @mutex = Mutex.new
end

Instance Attribute Details

#client_configObject (readonly)

Returns the value of attribute client_config.



24
25
26
# File 'lib/redis_client/cluster_config.rb', line 24

def client_config
  @client_config
end

#command_builderObject (readonly)

Returns the value of attribute command_builder.



24
25
26
# File 'lib/redis_client/cluster_config.rb', line 24

def command_builder
  @command_builder
end

#replica_affinityObject (readonly)

Returns the value of attribute replica_affinity.



24
25
26
# File 'lib/redis_client/cluster_config.rb', line 24

def replica_affinity
  @replica_affinity
end

Instance Method Details

#add_node(host, port) ⇒ Object



100
101
102
103
104
# File 'lib/redis_client/cluster_config.rb', line 100

def add_node(host, port)
  return if @mutex.locked?

  @mutex.synchronize { @node_configs << { host: host, port: port } }
end

#dupObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/redis_client/cluster_config.rb', line 48

def dup
  self.class.new(
    nodes: @node_configs,
    replica: @replica,
    replica_affinity: @replica_affinity,
    fixed_hostname: @fixed_hostname,
    concurrency: @concurrency,
    client_implementation: @client_implementation,
    **@client_config
  )
end

#inspectObject



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

def inspect
  "#<#{self.class.name} #{per_node_key.values}>"
end

#new_client(**kwargs) ⇒ Object



77
78
79
# File 'lib/redis_client/cluster_config.rb', line 77

def new_client(**kwargs)
  @client_implementation.new(self, concurrency: @concurrency, **kwargs)
end

#new_pool(size: 5, timeout: 5, **kwargs) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/redis_client/cluster_config.rb', line 68

def new_pool(size: 5, timeout: 5, **kwargs)
  @client_implementation.new(
    self,
    pool: { size: size, timeout: timeout },
    concurrency: @concurrency,
    **kwargs
  )
end

#per_node_keyObject



81
82
83
84
85
86
87
88
# File 'lib/redis_client/cluster_config.rb', line 81

def per_node_key
  @node_configs.to_h do |config|
    node_key = ::RedisClient::Cluster::NodeKey.build_from_host_port(config[:host], config[:port])
    config = @client_config.merge(config)
    config = config.merge(host: @fixed_hostname) unless @fixed_hostname.empty?
    [node_key, config]
  end
end

#read_timeoutObject



64
65
66
# File 'lib/redis_client/cluster_config.rb', line 64

def read_timeout
  @client_config[:read_timeout] || @client_config[:timeout] || ::RedisClient::Config::DEFAULT_TIMEOUT
end

#update_node(addrs) ⇒ Object



94
95
96
97
98
# File 'lib/redis_client/cluster_config.rb', line 94

def update_node(addrs)
  return if @mutex.locked?

  @mutex.synchronize { @node_configs = build_node_configs(addrs) }
end

#use_replica?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/redis_client/cluster_config.rb', line 90

def use_replica?
  @replica
end