@@ -14,6 +14,7 @@ class Cluster
14
14
class Router
15
15
ZERO_CURSOR_FOR_SCAN = '0'
16
16
METHODS_FOR_BLOCKING_CMD = %i[ blocking_call_v blocking_call ] . freeze
17
+ TSF = -> ( b , s ) { b . nil? ? s : b . call ( s ) } . curry
17
18
18
19
attr_reader :node
19
20
@@ -30,25 +31,25 @@ def initialize(config, pool: nil, **kwargs)
30
31
def send_command ( method , command , *args , &block ) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
31
32
cmd = ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_command ( command )
32
33
case cmd
33
- when 'acl' , 'auth' , 'bgrewriteaof' , 'bgsave' , 'quit' , 'save'
34
- @node . call_all ( method , command , args , &block ) . first
35
- when 'flushall' , 'flushdb'
36
- @node . call_primaries ( method , command , args , &block ) . first
37
- when 'ping' then @node . send_ping ( method , command , args , &block ) . first
34
+ when 'ping' then @node . send_ping ( method , command , args ) . first . then ( &TSF . call ( block ) )
38
35
when 'wait' then send_wait_command ( method , command , args , &block )
39
- when 'keys' then @node . call_replicas ( method , command , args , & block ) . flatten . sort_by ( &:to_s )
40
- when 'dbsize' then @node . call_replicas ( method , command , args , & block ) . select { |e | e . is_a? ( Integer ) } . sum
36
+ when 'keys' then @node . call_replicas ( method , command , args ) . flatten . sort_by ( &:to_s ) . then ( & TSF . call ( block ) )
37
+ when 'dbsize' then @node . call_replicas ( method , command , args ) . select { |e | e . is_a? ( Integer ) } . sum . then ( & TSF . call ( block ) )
41
38
when 'scan' then scan ( command , seed : 1 )
42
- when 'lastsave' then @node . call_all ( method , command , args , & block ) . sort_by ( &:to_i )
39
+ when 'lastsave' then @node . call_all ( method , command , args ) . sort_by ( &:to_i ) . then ( & TSF . call ( block ) )
43
40
when 'role' then @node . call_all ( method , command , args , &block )
44
41
when 'config' then send_config_command ( method , command , args , &block )
45
42
when 'client' then send_client_command ( method , command , args , &block )
46
43
when 'cluster' then send_cluster_command ( method , command , args , &block )
47
- when 'readonly' , 'readwrite' , 'shutdown'
48
- raise ::RedisClient ::Cluster ::OrchestrationCommandNotSupported , cmd
49
44
when 'memory' then send_memory_command ( method , command , args , &block )
50
45
when 'script' then send_script_command ( method , command , args , &block )
51
46
when 'pubsub' then send_pubsub_command ( method , command , args , &block )
47
+ when 'acl' , 'auth' , 'bgrewriteaof' , 'bgsave' , 'quit' , 'save'
48
+ @node . call_all ( method , command , args ) . first . then ( &TSF . call ( block ) )
49
+ when 'flushall' , 'flushdb'
50
+ @node . call_primaries ( method , command , args ) . first . then ( &TSF . call ( block ) )
51
+ when 'readonly' , 'readwrite' , 'shutdown'
52
+ raise ::RedisClient ::Cluster ::OrchestrationCommandNotSupported , cmd
52
53
when 'discard' , 'exec' , 'multi' , 'unwatch'
53
54
raise ::RedisClient ::Cluster ::AmbiguousNodeError , cmd
54
55
else
@@ -208,7 +209,7 @@ def assign_asking_node(err_msg)
208
209
private
209
210
210
211
def send_wait_command ( method , command , args , retry_count : 3 , &block ) # rubocop:disable Metrics/AbcSize
211
- @node . call_primaries ( method , command , args , & block ) . select { |r | r . is_a? ( Integer ) } . sum
212
+ @node . call_primaries ( method , command , args ) . select { |r | r . is_a? ( Integer ) } . sum . then ( & TSF . call ( block ) )
212
213
rescue ::RedisClient ::Cluster ::ErrorCollection => e
213
214
raise if e . errors . any? ( ::RedisClient ::CircuitBreaker ::OpenCircuitError )
214
215
raise if retry_count <= 0
@@ -224,24 +225,24 @@ def send_wait_command(method, command, args, retry_count: 3, &block) # rubocop:d
224
225
def send_config_command ( method , command , args , &block )
225
226
case ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_subcommand ( command )
226
227
when 'resetstat' , 'rewrite' , 'set'
227
- @node . call_all ( method , command , args , & block ) . first
228
+ @node . call_all ( method , command , args ) . first . then ( & TSF . call ( block ) )
228
229
else assign_node ( command ) . public_send ( method , *args , command , &block )
229
230
end
230
231
end
231
232
232
233
def send_memory_command ( method , command , args , &block )
233
234
case ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_subcommand ( command )
234
235
when 'stats' then @node . call_all ( method , command , args , &block )
235
- when 'purge' then @node . call_all ( method , command , args , & block ) . first
236
+ when 'purge' then @node . call_all ( method , command , args ) . first . then ( & TSF . call ( block ) )
236
237
else assign_node ( command ) . public_send ( method , *args , command , &block )
237
238
end
238
239
end
239
240
240
241
def send_client_command ( method , command , args , &block )
241
242
case ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_subcommand ( command )
242
- when 'list' then @node . call_all ( method , command , args , & block ) . flatten
243
+ when 'list' then @node . call_all ( method , command , args ) . flatten . then ( & TSF . call ( block ) )
243
244
when 'pause' , 'reply' , 'setname'
244
- @node . call_all ( method , command , args , & block ) . first
245
+ @node . call_all ( method , command , args ) . first . then ( & TSF . call ( block ) )
245
246
else assign_node ( command ) . public_send ( method , *args , command , &block )
246
247
end
247
248
end
@@ -251,7 +252,7 @@ def send_cluster_command(method, command, args, &block)
251
252
when 'addslots' , 'delslots' , 'failover' , 'forget' , 'meet' , 'replicate' ,
252
253
'reset' , 'set-config-epoch' , 'setslot'
253
254
raise ::RedisClient ::Cluster ::OrchestrationCommandNotSupported , [ 'cluster' , subcommand ]
254
- when 'saveconfig' then @node . call_all ( method , command , args , & block ) . first
255
+ when 'saveconfig' then @node . call_all ( method , command , args ) . first . then ( & TSF . call ( block ) )
255
256
when 'getkeysinslot'
256
257
raise ArgumentError , command . join ( ' ' ) if command . size != 4
257
258
@@ -260,25 +261,25 @@ def send_cluster_command(method, command, args, &block)
260
261
end
261
262
end
262
263
263
- def send_script_command ( method , command , args , &block )
264
+ def send_script_command ( method , command , args , &block ) # rubocop:disable Metrics/AbcSize
264
265
case ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_subcommand ( command )
265
266
when 'debug' , 'kill'
266
- @node . call_all ( method , command , args , & block ) . first
267
+ @node . call_all ( method , command , args ) . first . then ( & TSF . call ( block ) )
267
268
when 'flush' , 'load'
268
- @node . call_primaries ( method , command , args , & block ) . first
269
+ @node . call_primaries ( method , command , args ) . first . then ( & TSF . call ( block ) )
269
270
when 'exists'
270
- @node . call_all ( method , command , args , & block ) . transpose . map { |arr | arr . any? ( &:zero? ) ? 0 : 1 }
271
+ @node . call_all ( method , command , args ) . transpose . map { |arr | arr . any? ( &:zero? ) ? 0 : 1 } . then ( & TSF . call ( block ) )
271
272
else assign_node ( command ) . public_send ( method , *args , command , &block )
272
273
end
273
274
end
274
275
275
276
def send_pubsub_command ( method , command , args , &block ) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
276
277
case ::RedisClient ::Cluster ::NormalizedCmdName . instance . get_by_subcommand ( command )
277
- when 'channels' then @node . call_all ( method , command , args , & block ) . flatten . uniq . sort_by ( &:to_s )
278
+ when 'channels' then @node . call_all ( method , command , args ) . flatten . uniq . sort_by ( &:to_s ) . then ( & TSF . call ( block ) )
278
279
when 'numsub'
279
- @node . call_all ( method , command , args , & block ) . reject ( &:empty? ) . map { |e | Hash [ *e ] }
280
- . reduce ( { } ) { |a , e | a . merge ( e ) { |_ , v1 , v2 | v1 + v2 } }
281
- when 'numpat' then @node . call_all ( method , command , args , & block ) . select { |e | e . is_a? ( Integer ) } . sum
280
+ @node . call_all ( method , command , args ) . reject ( &:empty? ) . map { |e | Hash [ *e ] }
281
+ . reduce ( { } ) { |a , e | a . merge ( e ) { |_ , v1 , v2 | v1 + v2 } } . then ( & TSF . call ( block ) )
282
+ when 'numpat' then @node . call_all ( method , command , args ) . select { |e | e . is_a? ( Integer ) } . sum . then ( & TSF . call ( block ) )
282
283
else assign_node ( command ) . public_send ( method , *args , command , &block )
283
284
end
284
285
end
0 commit comments