redis-cluster-client 0.4.6 → 0.4.8

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: eab80ca6b1a69e3c45bc0dd59f9dd05189559907e0976a534ddc5dc0de26527b
4
+ data.tar.gz: 90aa485da1918f0a46f36964ff7ede3c775dcf90ebf0477df0a6d5f2cc3846f7
5
5
  SHA512:
6
- metadata.gz: 764b1ff360c46af8afebe9d937a2ac2ba3442701a4b5e9626af6157557bb29f9e35f0d0dcaff473cc77aa47b9930c6e179e42ceffa501d2105ab478e2d4f6fc8
7
- data.tar.gz: 2eec106a7a6d0e38253ffca1f115d2a5368f3dd3c5bcfaafe25bb51bba35be67ba765dd5f6d93916d3eb23193bf4e588fe33833df8602475b58867ef8801585f
6
+ metadata.gz: 101ec6c98f8f2164b4c8da1a497ca31ca456d7c567ac9c96e8ac3220b37be96160ad558111c19e92d67c1947c29523463011c073016158a2930c7462231f3bc8
7
+ data.tar.gz: 72067a86f5fb1839bab4766dad20e5157932238c1a6237c9de805cf7dbd77e784b5b1b47e560f3585550521b36632dd7e9a094ccecad0e4ee893eb27bfc1f342
@@ -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.values.shuffle.each 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.8
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