Skip to content

Commit 2474a8b

Browse files
committed
fix
1 parent 76cb715 commit 2474a8b

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

lib/redis_client/cluster.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,37 @@ def blocking_call_v(timeout, command, &block)
6262
end
6363

6464
def scan(*args, **kwargs, &block)
65-
raise ArgumentError, 'block required' unless block
65+
return to_enum(__callee__, *args, **kwargs) unless block_given?
6666

67+
command = @command_builder.generate(['SCAN', ZERO_CURSOR_FOR_SCAN] + args, kwargs)
6768
seed = Random.new_seed
68-
cursor = ZERO_CURSOR_FOR_SCAN
6969
loop do
70-
cursor, keys = router.scan('SCAN', cursor, *args, seed: seed, **kwargs)
70+
cursor, keys = router.scan(command, seed: seed)
71+
command[1] = cursor
7172
keys.each(&block)
7273
break if cursor == ZERO_CURSOR_FOR_SCAN
7374
end
7475
end
7576

7677
def sscan(key, *args, **kwargs, &block)
77-
node = router.assign_node(['SSCAN', key])
78-
router.try_delegate(node, :sscan, key, *args, **kwargs, &block)
78+
return to_enum(__callee__, key, *args, **kwargs) unless block_given?
79+
80+
command = @command_builder.generate(['SSCAN', key, ZERO_CURSOR_FOR_SCAN] + args, kwargs)
81+
router.scan_single_key(command, arity: 1, &block)
7982
end
8083

8184
def hscan(key, *args, **kwargs, &block)
82-
node = router.assign_node(['HSCAN', key])
83-
router.try_delegate(node, :hscan, key, *args, **kwargs, &block)
85+
return to_enum(__callee__, key, *args, **kwargs) unless block_given?
86+
87+
command = @command_builder.generate(['HSCAN', key, ZERO_CURSOR_FOR_SCAN] + args, kwargs)
88+
router.scan_single_key(command, arity: 2, &block)
8489
end
8590

8691
def zscan(key, *args, **kwargs, &block)
87-
node = router.assign_node(['ZSCAN', key])
88-
router.try_delegate(node, :zscan, key, *args, **kwargs, &block)
92+
return to_enum(__callee__, key, *args, **kwargs) unless block_given?
93+
94+
command = @command_builder.generate(['ZSCAN', key, ZERO_CURSOR_FOR_SCAN] + args, kwargs)
95+
router.scan_single_key(command, arity: 2, &block)
8996
end
9097

9198
def pipelined(exception: true)

lib/redis_client/cluster/router.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ def try_send(node, method, command, args, retry_count: 3, &block)
104104
end
105105
end
106106

107-
def try_delegate(node, method, *args, retry_count: 3, **kwargs, &block)
108-
handle_redirection(node, nil, retry_count: retry_count) do |on_node|
109-
on_node.public_send(method, *args, **kwargs, &block)
110-
end
111-
end
112-
113107
def handle_redirection(node, command, retry_count:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
114108
yield node
115109
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
@@ -153,9 +147,7 @@ def handle_redirection(node, command, retry_count:) # rubocop:disable Metrics/Ab
153147
raise
154148
end
155149

156-
def scan(*command, seed: nil, **kwargs) # rubocop:disable Metrics/AbcSize
157-
command = @command_builder.generate(command, kwargs)
158-
150+
def scan(command, seed: nil) # rubocop:disable Metrics/AbcSize
159151
command[1] = ZERO_CURSOR_FOR_SCAN if command.size == 1
160152
input_cursor = Integer(command[1])
161153

@@ -180,6 +172,19 @@ def scan(*command, seed: nil, **kwargs) # rubocop:disable Metrics/AbcSize
180172
raise
181173
end
182174

175+
def scan_single_key(command, arity:, &block)
176+
node = assign_node(command)
177+
178+
handle_redirection(node, nil, retry_count: 3) do |on_node|
179+
loop do
180+
cursor, values = on_node.call_v(command)
181+
command[2] = cursor
182+
arity < 2 ? values.each(&block) : values.each_slice(arity, &block)
183+
break if cursor == ZERO_CURSOR_FOR_SCAN
184+
end
185+
end
186+
end
187+
183188
def assign_node(command)
184189
handle_node_reload_error do
185190
node_key = find_node_key(command)

test/redis_client/test_cluster.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ def test_blocking_call
9292
end
9393

9494
def test_scan
95-
assert_raises(ArgumentError) { @client.scan }
96-
9795
10.times { |i| @client.call('SET', "key#{i}", i) }
9896
wait_for_replication
9997
want = (0..9).map { |i| "key#{i}" }
@@ -117,9 +115,9 @@ def test_hscan
117115
10.times do |i|
118116
10.times { |j| @client.call('HSET', "key#{i}", "field#{j}", j) }
119117
wait_for_replication
120-
want = (0..9).map { |j| "field#{j}" }
118+
want = (0..9).map { |j| ["field#{j}", j.to_s] }
121119
got = []
122-
@client.hscan("key#{i}", 'COUNT', '5') { |field| got << field }
120+
@client.hscan("key#{i}", 'COUNT', '5') { |pair| got << pair }
123121
assert_equal(want, got.sort)
124122
end
125123
end
@@ -128,9 +126,9 @@ def test_zscan
128126
10.times do |i|
129127
10.times { |j| @client.call('ZADD', "key#{i}", j, "member#{j}") }
130128
wait_for_replication
131-
want = (0..9).map { |j| "member#{j}" }
129+
want = (0..9).map { |j| ["member#{j}", j.to_s] }
132130
got = []
133-
@client.zscan("key#{i}", 'COUNT', '5') { |member| got << member }
131+
@client.zscan("key#{i}", 'COUNT', '5') { |pair| got << pair }
134132
assert_equal(want, got.sort)
135133
end
136134
end

0 commit comments

Comments
 (0)