redis-cluster-client 0.4.5 → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be67f7c4a1a9e75573c957233bc9381745fc143b483583a4abb02b6b794bf49a
4
- data.tar.gz: b12b518ba293af7e8f84e9a8c4770c5d5e8d776e213af30db94bcba753612802
3
+ metadata.gz: b894248e71e984932e9d2f177640c86a7f7d08cb57d3bf43bb91603ffe737003
4
+ data.tar.gz: e1ca295dc2a19a4c16ecb2b69ef3ad28cd353c5f5b17db90749d29c8b9eb3f79
5
5
  SHA512:
6
- metadata.gz: fa19e009a6af98f69b797a88fd0ce3e09c9cd74d0bed99fdcabf4c15e78435179736e66d5d18e0e2c66c44d6343ab75e443b3d8577b37beb91ed13065f00bf4c
7
- data.tar.gz: e055226106abe328b92d84da1f63e2d3e3ef22168cf3716837e96a7491249862894d98fa49edd92681083d6617787701411e51dab391322f1259af86a730149f
6
+ metadata.gz: 88f12ba8750400ac78ae399d0b1f1301a7d12626286d780a04268af16bc1fab58f2da168b8802766f631fb09327518ca0f6927e2b57f6c03a79b3ec4439b9033
7
+ data.tar.gz: f721594b1cf537dc05f7d999f99626814f88f3488bf8f2c3c67fa197214f24a1b5513a25e616666c2c8a840cd3b33c616517e8eb35eee9c169a21a3e63ad86ea
@@ -3,12 +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 = {}
44
+ @states = {}
12
45
  end
13
46
 
14
47
  def call(*args, **kwargs)
@@ -20,46 +53,39 @@ class RedisClient
20
53
  end
21
54
 
22
55
  def close
23
- @pubsub_states.each_value(&:close)
24
- @pubsub_states.clear
56
+ @states.each_value(&:close)
57
+ @states.clear
25
58
  end
26
59
 
27
60
  def next_event(timeout = nil)
28
- return if @pubsub_states.empty?
61
+ return if @states.empty?
29
62
 
30
- msgs = collect_messages(timeout).compact
31
- return msgs.first if msgs.size < 2
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
32
67
 
33
- msgs
68
+ @states.each_value do |pubsub|
69
+ message = pubsub.take_message(timeout)
70
+ return message if message
71
+ end
72
+ end
34
73
  end
35
74
 
36
75
  private
37
76
 
38
77
  def _call(command)
39
78
  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)
79
+ @states[node_key] = State.new(@router.find_node(node_key).pubsub) unless @states.key?(node_key)
80
+ @states[node_key].call(command)
46
81
  end
47
82
 
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
83
+ def obtain_current_time
84
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
85
+ end
57
86
 
58
- threads.each do |t|
59
- t.join
60
- acc << t[:reply] unless t[:reply].nil?
61
- end
62
- end
87
+ def calc_max_duration(timeout)
88
+ timeout.nil? || timeout < 0 ? 0 : timeout * 1_000_000
63
89
  end
64
90
  end
65
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.5
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