redis-cluster-client 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 619becbe41f6cb5c829de9df021a399ac966b1976284e06e7792c0b570061e32
4
- data.tar.gz: 5b6ddc1c8410bcf8ab03538bf5dd77d0425d4bf34f170ea026ff4d1c1be2ce8e
3
+ metadata.gz: 1eaec04db89e1afa8a1d0a27688194d5941a94edb00aef979b1f368d01b4dd41
4
+ data.tar.gz: 36571e86def197fc3633d4c026958fbe635576d5f9aec3ab19b32759957a8f14
5
5
  SHA512:
6
- metadata.gz: 658a78bdd61f7e902b2385c73f23f3fa692a73b77a073afc0cb3d0616726a99cb65884ec4ab7cbae2bdd4574444f1de2f639105eaa4f40e1c07c186d7589dff5
7
- data.tar.gz: 33b9d88cc33fec6f5a556ae3beb494c9875796088b93a47b647efe02db6f41540d49f3120d5d7114c8757aa78e29b173abd20a8b2a2341861e953a0ab4b0f67a
6
+ metadata.gz: 2da9f21f30cc49f9df64cf11ed5ee3c0385bc6310bbb7e8cf3478552272f716ab68928aee05bb90b24d39253040f170fd38df78687afb78ea612f310f6a96b2e
7
+ data.tar.gz: 4f7828b8072f01aaa5aa56264d7fda6f8d4ff17bce7a88a5adb2c05bd5eab8a74fe137b3c59ce1bba086a9793a05d490252d0df94228e35c6c10a2b0d869d797
@@ -72,14 +72,14 @@ class RedisClient
72
72
  end
73
73
 
74
74
  def dig_details(command, key)
75
- name = command&.flatten&.first.to_s.downcase
75
+ name = command&.flatten&.first.to_s.downcase # OPTIMIZE: prevent allocation for string
76
76
  return if name.empty? || !@details.key?(name)
77
77
 
78
78
  @details.fetch(name).fetch(key)
79
79
  end
80
80
 
81
81
  def determine_first_key_position(command) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
82
- case command&.flatten&.first.to_s.downcase
82
+ case command&.flatten&.first.to_s.downcase # OPTIMIZE: prevent allocation for string
83
83
  when 'eval', 'evalsha', 'zinterstore', 'zunionstore' then 3
84
84
  when 'object' then 2
85
85
  when 'memory'
@@ -21,6 +21,7 @@ class RedisClient
21
21
  @replications.each_value { |keys| keys.sort_by! { |k| latencies.fetch(k) } }
22
22
  @replica_clients = select_replica_clients(@replications, @clients)
23
23
  @clients_for_scanning = select_clients_for_scanning(@replications, @clients)
24
+ @existed_replicas = @replications.reject { |_, v| v.empty? }.values
24
25
  end
25
26
 
26
27
  def clients_for_scanning(seed: nil) # rubocop:disable Lint/UnusedMethodArgument
@@ -33,7 +34,7 @@ class RedisClient
33
34
 
34
35
  def any_replica_node_key(seed: nil)
35
36
  random = seed.nil? ? Random : Random.new(seed)
36
- @replications.reject { |_, v| v.empty? }.values.sample(random: random).first
37
+ @existed_replicas.sample(random: random).first
37
38
  end
38
39
 
39
40
  private
@@ -181,6 +181,8 @@ class RedisClient
181
181
  end
182
182
 
183
183
  def update_slot(slot, node_key)
184
+ return if @mutex.locked?
185
+
184
186
  @mutex.synchronize { @slots[slot] = node_key }
185
187
  end
186
188
 
@@ -9,54 +9,48 @@ class RedisClient
9
9
  ReplySizeError = Class.new(::RedisClient::Error)
10
10
  MAX_THREADS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
11
11
 
12
- def initialize(router, command_builder)
12
+ def initialize(router, command_builder, seed: Random.new_seed)
13
13
  @router = router
14
14
  @command_builder = command_builder
15
- @grouped = Hash.new([].freeze)
15
+ @grouped = {}
16
16
  @size = 0
17
- @seed = Random.new_seed
17
+ @seed = seed
18
18
  end
19
19
 
20
20
  def call(*args, **kwargs, &block)
21
21
  command = @command_builder.generate(args, kwargs)
22
22
  node_key = @router.find_node_key(command, seed: @seed)
23
- @grouped[node_key] += [[@size, :call_v, command, block]]
24
- @size += 1
23
+ add_line(node_key, [@size, :call_v, command, block])
25
24
  end
26
25
 
27
26
  def call_v(args, &block)
28
27
  command = @command_builder.generate(args)
29
28
  node_key = @router.find_node_key(command, seed: @seed)
30
- @grouped[node_key] += [[@size, :call_v, command, block]]
31
- @size += 1
29
+ add_line(node_key, [@size, :call_v, command, block])
32
30
  end
33
31
 
34
32
  def call_once(*args, **kwargs, &block)
35
33
  command = @command_builder.generate(args, kwargs)
36
34
  node_key = @router.find_node_key(command, seed: @seed)
37
- @grouped[node_key] += [[@size, :call_once_v, command, block]]
38
- @size += 1
35
+ add_line(node_key, [@size, :call_once_v, command, block])
39
36
  end
40
37
 
41
38
  def call_once_v(args, &block)
42
39
  command = @command_builder.generate(args)
43
40
  node_key = @router.find_node_key(command, seed: @seed)
44
- @grouped[node_key] += [[@size, :call_once_v, command, block]]
45
- @size += 1
41
+ add_line(node_key, [@size, :call_once_v, command, block])
46
42
  end
47
43
 
48
44
  def blocking_call(timeout, *args, **kwargs, &block)
49
45
  command = @command_builder.generate(args, kwargs)
50
46
  node_key = @router.find_node_key(command, seed: @seed)
51
- @grouped[node_key] += [[@size, :blocking_call_v, timeout, command, block]]
52
- @size += 1
47
+ add_line(node_key, [@size, :blocking_call_v, timeout, command, block])
53
48
  end
54
49
 
55
50
  def blocking_call_v(timeout, args, &block)
56
51
  command = @command_builder.generate(args)
57
52
  node_key = @router.find_node_key(command, seed: @seed)
58
- @grouped[node_key] += [[@size, :blocking_call_v, timeout, command, block]]
59
- @size += 1
53
+ add_line(node_key, [@size, :blocking_call_v, timeout, command, block])
60
54
  end
61
55
 
62
56
  def empty?
@@ -92,6 +86,14 @@ class RedisClient
92
86
 
93
87
  raise ::RedisClient::Cluster::ErrorCollection, errors
94
88
  end
89
+
90
+ private
91
+
92
+ def add_line(node_key, line)
93
+ @grouped[node_key] = [] unless @grouped.key?(node_key)
94
+ @grouped[node_key] << line
95
+ @size += 1
96
+ end
95
97
  end
96
98
  end
97
99
  end
@@ -281,6 +281,8 @@ class RedisClient
281
281
  end
282
282
 
283
283
  def update_cluster_info!
284
+ return if @mutex.locked?
285
+
284
286
  @mutex.synchronize do
285
287
  begin
286
288
  @node.each(&:close)
@@ -78,7 +78,8 @@ class RedisClient
78
78
  end
79
79
 
80
80
  def pipelined
81
- pipeline = ::RedisClient::Cluster::Pipeline.new(@router, @command_builder)
81
+ seed = @config.use_replica? && @config.replica_affinity == :random ? nil : Random.new_seed
82
+ pipeline = ::RedisClient::Cluster::Pipeline.new(@router, @command_builder, seed: seed)
82
83
  yield pipeline
83
84
  return [] if pipeline.empty? == 0
84
85
 
@@ -42,6 +42,17 @@ class RedisClient
42
42
  @mutex = Mutex.new
43
43
  end
44
44
 
45
+ def dup
46
+ self.class.new(
47
+ nodes: @node_configs,
48
+ replica: @replica,
49
+ replica_affinity: @replica_affinity,
50
+ fixed_hostname: @fixed_hostname,
51
+ client_implementation: @client_implementation,
52
+ **@client_config
53
+ )
54
+ end
55
+
45
56
  def inspect
46
57
  "#<#{self.class.name} #{per_node_key.values}>"
47
58
  end
@@ -72,15 +83,15 @@ class RedisClient
72
83
  end
73
84
 
74
85
  def update_node(addrs)
86
+ return if @mutex.locked?
87
+
75
88
  @mutex.synchronize { @node_configs = build_node_configs(addrs) }
76
89
  end
77
90
 
78
91
  def add_node(host, port)
79
- @mutex.synchronize { @node_configs << { host: host, port: port } }
80
- end
92
+ return if @mutex.locked?
81
93
 
82
- def dup
83
- self.class.new(nodes: @node_configs, replica: @replica, fixed_hostname: @fixed_hostname, **@client_config)
94
+ @mutex.synchronize { @node_configs << { host: host, port: port } }
84
95
  end
85
96
 
86
97
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-cluster-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2022-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client