redis-cluster-client 0.4.3 → 0.4.5

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: 4e29792dd4eb3b7609baa8943b25a385c87dd36767ba679e9c9209be765a670b
4
- data.tar.gz: 367d7cb722ef026b76c7f200237c1c64a0dced84026c78a43feb55a72eb7accb
3
+ metadata.gz: be67f7c4a1a9e75573c957233bc9381745fc143b483583a4abb02b6b794bf49a
4
+ data.tar.gz: b12b518ba293af7e8f84e9a8c4770c5d5e8d776e213af30db94bcba753612802
5
5
  SHA512:
6
- metadata.gz: c3b7eb12f99b928072eeb00f4465afc5be3a3cb07aeabfc821369151f94ebbf12029409317c21b57cb628710e786e1e423b84f543b7430c23d89f71e9fe9339b
7
- data.tar.gz: c317f99c5bb51be184f4aab415017260237e3614e535565649bb90d79a2b47c7b9edfb07c83b3a96ead85849c7bd97639ce6d60eb1f957df351908ae098f6795
6
+ metadata.gz: fa19e009a6af98f69b797a88fd0ce3e09c9cd74d0bed99fdcabf4c15e78435179736e66d5d18e0e2c66c44d6343ab75e443b3d8577b37beb91ed13065f00bf4c
7
+ data.tar.gz: e055226106abe328b92d84da1f63e2d3e3ef22168cf3716837e96a7491249862894d98fa49edd92681083d6617787701411e51dab391322f1259af86a730149f
@@ -10,7 +10,7 @@ class RedisClient
10
10
 
11
11
  attr_reader :replica_clients
12
12
 
13
- DUMMY_LATENCY_NSEC = 100 * 1000 * 1000 * 1000
13
+ DUMMY_LATENCY_MSEC = 100 * 1000 * 1000
14
14
  MEASURE_ATTEMPT_COUNT = 10
15
15
 
16
16
  def initialize(replications, options, pool, **kwargs)
@@ -45,7 +45,7 @@ class RedisClient
45
45
  Thread.new(k, v) do |node_key, client|
46
46
  Thread.current[:node_key] = node_key
47
47
 
48
- min = DUMMY_LATENCY_NSEC
48
+ min = DUMMY_LATENCY_MSEC
49
49
  MEASURE_ATTEMPT_COUNT.times do
50
50
  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
51
51
  client.call_once('PING')
@@ -55,7 +55,7 @@ class RedisClient
55
55
 
56
56
  Thread.current[:latency] = min
57
57
  rescue StandardError
58
- Thread.current[:latency] = DUMMY_LATENCY_NSEC
58
+ Thread.current[:latency] = DUMMY_LATENCY_MSEC
59
59
  end
60
60
  end
61
61
 
@@ -3,33 +3,63 @@
3
3
  class RedisClient
4
4
  class Cluster
5
5
  class PubSub
6
+ MAX_THREADS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
7
+
6
8
  def initialize(router, command_builder)
7
9
  @router = router
8
10
  @command_builder = command_builder
9
- @pubsub = nil
11
+ @pubsub_states = {}
10
12
  end
11
13
 
12
14
  def call(*args, **kwargs)
13
- close
14
- command = @command_builder.generate(args, kwargs)
15
- @pubsub = @router.assign_node(command).pubsub
16
- @pubsub.call_v(command)
15
+ _call(@command_builder.generate(args, kwargs))
17
16
  end
18
17
 
19
18
  def call_v(command)
20
- close
21
- command = @command_builder.generate(command)
22
- @pubsub = @router.assign_node(command).pubsub
23
- @pubsub.call_v(command)
19
+ _call(@command_builder.generate(command))
24
20
  end
25
21
 
26
22
  def close
27
- @pubsub&.close
28
- @pubsub = nil
23
+ @pubsub_states.each_value(&:close)
24
+ @pubsub_states.clear
29
25
  end
30
26
 
31
27
  def next_event(timeout = nil)
32
- @pubsub&.next_event(timeout)
28
+ return if @pubsub_states.empty?
29
+
30
+ msgs = collect_messages(timeout).compact
31
+ return msgs.first if msgs.size < 2
32
+
33
+ msgs
34
+ end
35
+
36
+ private
37
+
38
+ def _call(command)
39
+ node_key = @router.find_node_key(command)
40
+ pubsub = if @pubsub_states.key?(node_key)
41
+ @pubsub_states[node_key]
42
+ else
43
+ @pubsub_states[node_key] = @router.find_node(node_key).pubsub
44
+ end
45
+ pubsub.call_v(command)
46
+ end
47
+
48
+ def collect_messages(timeout) # rubocop:disable Metrics/AbcSize
49
+ @pubsub_states.each_slice(MAX_THREADS).each_with_object([]) do |chuncked_pubsub_states, acc|
50
+ threads = chuncked_pubsub_states.map do |_, v|
51
+ Thread.new(v) do |pubsub|
52
+ Thread.current[:reply] = pubsub.next_event(timeout)
53
+ rescue StandardError => e
54
+ Thread.current[:reply] = e
55
+ end
56
+ end
57
+
58
+ threads.each do |t|
59
+ t.join
60
+ acc << t[:reply] unless t[:reply].nil?
61
+ end
62
+ end
33
63
  end
34
64
  end
35
65
  end
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.4.3
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taishi Kasuga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-08 00:00:00.000000000 Z
11
+ date: 2023-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  requirements: []
72
- rubygems_version: 3.4.6
72
+ rubygems_version: 3.4.13
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: A Redis cluster client for Ruby