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))
SLOW_COMMAND_TIMEOUT =

It’s used with slow queries of fetching meta data like CLUSTER NODES, COMMAND and so on.

Float(ENV.fetch('REDIS_CLIENT_SLOW_COMMAND_TIMEOUT', -1))
MAX_STARTUP_SAMPLE =

It affects to strike a balance between load and stability in initialization or changed states.

Integer(ENV.fetch('REDIS_CLIENT_MAX_STARTUP_SAMPLE', 3))
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, connect_with_original_config: false, client_implementation: ::RedisClient::Cluster, slow_command_timeout: SLOW_COMMAND_TIMEOUT, command_builder: ::RedisClient::CommandBuilder, max_startup_sample: MAX_STARTUP_SAMPLE, **client_config) ⇒ ClusterConfig

rubocop:disable Metrics/ParameterLists



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/redis_client/cluster_config.rb', line 31

def initialize( # rubocop:disable Metrics/ParameterLists
  nodes: DEFAULT_NODES,
  replica: false,
  replica_affinity: :random,
  fixed_hostname: '',
  concurrency: nil,
  connect_with_original_config: false,
  client_implementation: ::RedisClient::Cluster, # for redis gem
  slow_command_timeout: SLOW_COMMAND_TIMEOUT,
  command_builder: ::RedisClient::CommandBuilder,
  max_startup_sample: MAX_STARTUP_SAMPLE,
  **client_config
)

  @replica = true & replica
  @replica_affinity = replica_affinity.to_s.to_sym
  @fixed_hostname = fixed_hostname.to_s
  @command_builder = command_builder
  node_configs = build_node_configs(nodes.dup)
  @client_config = merge_generic_config(client_config, node_configs)
  # Keep tabs on the original startup nodes we were constructed with
  @startup_nodes = build_startup_nodes(node_configs)
  @concurrency = merge_concurrency_option(concurrency)
  @connect_with_original_config = connect_with_original_config
  @client_implementation = client_implementation
  @slow_command_timeout = slow_command_timeout
  @max_startup_sample = max_startup_sample
end

Instance Attribute Details

#client_configObject (readonly)

Returns the value of attribute client_config.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def client_config
  @client_config
end

#command_builderObject (readonly)

Returns the value of attribute command_builder.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def command_builder
  @command_builder
end

#connect_with_original_configObject (readonly)

Returns the value of attribute connect_with_original_config.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def connect_with_original_config
  @connect_with_original_config
end

#max_startup_sampleObject (readonly)

Returns the value of attribute max_startup_sample.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def max_startup_sample
  @max_startup_sample
end

#replica_affinityObject (readonly)

Returns the value of attribute replica_affinity.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def replica_affinity
  @replica_affinity
end

#slow_command_timeoutObject (readonly)

Returns the value of attribute slow_command_timeout.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def slow_command_timeout
  @slow_command_timeout
end

#startup_nodesObject (readonly)

Returns the value of attribute startup_nodes.



28
29
30
# File 'lib/redis_client/cluster_config.rb', line 28

def startup_nodes
  @startup_nodes
end

Instance Method Details

#client_config_for_node(node_key) ⇒ Object



85
86
87
88
89
# File 'lib/redis_client/cluster_config.rb', line 85

def client_config_for_node(node_key)
  config = ::RedisClient::Cluster::NodeKey.hashify(node_key)
  config[:port] = ensure_integer(config[:port])
  augment_client_config(config)
end

#inspectObject



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

def inspect
  "#<#{self.class.name} #{startup_nodes.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

#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

#use_replica?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/redis_client/cluster_config.rb', line 81

def use_replica?
  @replica
end