redis-cluster-client 0.4.6 → 0.4.7

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: 75b13bfebd7ddbbb49d37626af8bae2b0a0c21ddc7ad198856c60c502700909e
4
- data.tar.gz: 9f8deb6e34399711ba7e432a85747272c51aa5525e3efc8d637577baab831e9e
3
+ metadata.gz: b894248e71e984932e9d2f177640c86a7f7d08cb57d3bf43bb91603ffe737003
4
+ data.tar.gz: e1ca295dc2a19a4c16ecb2b69ef3ad28cd353c5f5b17db90749d29c8b9eb3f79
5
5
  SHA512:
6
- metadata.gz: 764b1ff360c46af8afebe9d937a2ac2ba3442701a4b5e9626af6157557bb29f9e35f0d0dcaff473cc77aa47b9930c6e179e42ceffa501d2105ab478e2d4f6fc8
7
- data.tar.gz: 2eec106a7a6d0e38253ffca1f115d2a5368f3dd3c5bcfaafe25bb51bba35be67ba765dd5f6d93916d3eb23193bf4e588fe33833df8602475b58867ef8801585f
6
+ metadata.gz: 88f12ba8750400ac78ae399d0b1f1301a7d12626286d780a04268af16bc1fab58f2da168b8802766f631fb09327518ca0f6927e2b57f6c03a79b3ec4439b9033
7
+ data.tar.gz: f721594b1cf537dc05f7d999f99626814f88f3488bf8f2c3c67fa197214f24a1b5513a25e616666c2c8a840cd3b33c616517e8eb35eee9c169a21a3e63ad86ea
@@ -3,13 +3,45 @@
3
3
  class RedisClient
4
4
  class Cluster
5
5
  class PubSub
6
- MAX_THREADS = Integer(ENV.fetch('REDIS_CLIENT_MAX_THREADS', 5))
6
+ class State
7
+ def initialize(client)
8
+ @client = client
9
+ @worker = nil
10
+ end
11
+
12
+ def call(command)
13
+ @client.call_v(command)
14
+ end
15
+
16
+ def close
17
+ @worker.exit if @worker&.alive?
18
+ @client.close
19
+ end
20
+
21
+ def take_message(timeout)
22
+ @worker = subscribe(@client, timeout) if @worker.nil?
23
+ return if @worker.join(0.01).nil?
24
+
25
+ message = @worker[:reply]
26
+ @worker = nil
27
+ message
28
+ end
29
+
30
+ private
31
+
32
+ def subscribe(client, timeout)
33
+ Thread.new(client, timeout) do |pubsub, to|
34
+ Thread.current[:reply] = pubsub.next_event(to)
35
+ rescue StandardError => e
36
+ Thread.current[:reply] = e
37
+ end
38
+ end
39
+ end
7
40
 
8
41
  def initialize(router, command_builder)
9
42
  @router = router
10
43
  @command_builder = command_builder
11
- @pubsub_states = {}
12
- @messages = []
44
+ @states = {}
13
45
  end
14
46
 
15
47
  def call(*args, **kwargs)
@@ -21,46 +53,39 @@ class RedisClient
21
53
  end
22
54
 
23
55
  def close
24
- @pubsub_states.each_value(&:close)
25
- @pubsub_states.clear
26
- @messages.clear
56
+ @states.each_value(&:close)
57
+ @states.clear
27
58
  end
28
59
 
29
60
  def next_event(timeout = nil)
30
- return if @pubsub_states.empty?
31
- return @messages.shift unless @messages.empty?
61
+ return if @states.empty?
32
62
 
33
- collect_messages(timeout)
34
- @messages.shift
63
+ max_duration = calc_max_duration(timeout)
64
+ starting = obtain_current_time
65
+ loop do
66
+ break if max_duration > 0 && obtain_current_time - starting > max_duration
67
+
68
+ @states.each_value do |pubsub|
69
+ message = pubsub.take_message(timeout)
70
+ return message if message
71
+ end
72
+ end
35
73
  end
36
74
 
37
75
  private
38
76
 
39
77
  def _call(command)
40
78
  node_key = @router.find_node_key(command)
41
- pubsub = if @pubsub_states.key?(node_key)
42
- @pubsub_states[node_key]
43
- else
44
- @pubsub_states[node_key] = @router.find_node(node_key).pubsub
45
- end
46
- pubsub.call_v(command)
79
+ @states[node_key] = State.new(@router.find_node(node_key).pubsub) unless @states.key?(node_key)
80
+ @states[node_key].call(command)
47
81
  end
48
82
 
49
- def collect_messages(timeout)
50
- @pubsub_states.each_slice(MAX_THREADS) do |chuncked_pubsub_states|
51
- threads = chuncked_pubsub_states.map do |_, v|
52
- Thread.new(v) do |pubsub|
53
- Thread.current[:reply] = pubsub.next_event(timeout)
54
- rescue StandardError => e
55
- Thread.current[:reply] = e
56
- end
57
- end
83
+ def obtain_current_time
84
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
85
+ end
58
86
 
59
- threads.each do |t|
60
- t.join
61
- @messages << t[:reply] unless t[:reply].nil?
62
- end
63
- end
87
+ def calc_max_duration(timeout)
88
+ timeout.nil? || timeout < 0 ? 0 : timeout * 1_000_000
64
89
  end
65
90
  end
66
91
  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.6
4
+ version: 0.4.7
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-08-09 00:00:00.000000000 Z
11
+ date: 2023-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-client