@@ -17,9 +17,19 @@ class Cluster
17
17
class Router
18
18
ZERO_CURSOR_FOR_SCAN = '0'
19
19
TSF = -> ( f , x ) { f . nil? ? x : f . call ( x ) } . curry
20
+ Ractor . make_shareable ( TSF ) if Object . const_defined? ( :Ractor , false ) && Ractor . respond_to? ( :make_shareable )
20
21
DEDICATED_ACTIONS = lambda do # rubocop:disable Metrics/BlockLength
21
22
action = Struct . new ( 'RedisCommandRoutingAction' , :method_name , :reply_transformer , keyword_init : true )
22
23
pick_first = -> ( reply ) { reply . first } # rubocop:disable Style/SymbolProc
24
+ flatten_strings = -> ( reply ) { reply . flatten . sort_by ( &:to_s ) }
25
+ sum_num = -> ( reply ) { reply . select { |e | e . is_a? ( Integer ) } . sum }
26
+ sort_numbers = -> ( reply ) { reply . sort_by ( &:to_i ) }
27
+ if Object . const_defined? ( :Ractor , false ) && Ractor . respond_to? ( :make_shareable )
28
+ Ractor . make_shareable ( pick_first )
29
+ Ractor . make_shareable ( flatten_strings )
30
+ Ractor . make_shareable ( sum_num )
31
+ Ractor . make_shareable ( sort_numbers )
32
+ end
23
33
multiple_key_action = action . new ( method_name : :send_multiple_keys_command )
24
34
all_node_first_action = action . new ( method_name : :send_command_to_all_nodes , reply_transformer : pick_first )
25
35
primary_first_action = action . new ( method_name : :send_command_to_primaries , reply_transformer : pick_first )
@@ -28,10 +38,10 @@ class Router
28
38
{
29
39
'ping' => action . new ( method_name : :send_ping_command , reply_transformer : pick_first ) ,
30
40
'wait' => action . new ( method_name : :send_wait_command ) ,
31
- 'keys' => action . new ( method_name : :send_command_to_replicas , reply_transformer : -> ( reply ) { reply . flatten . sort_by ( & :to_s ) } ) ,
32
- 'dbsize' => action . new ( method_name : :send_command_to_replicas , reply_transformer : -> ( reply ) { reply . select { | e | e . is_a? ( Integer ) } . sum } ) ,
41
+ 'keys' => action . new ( method_name : :send_command_to_replicas , reply_transformer : flatten_strings ) ,
42
+ 'dbsize' => action . new ( method_name : :send_command_to_replicas , reply_transformer : sum_num ) ,
33
43
'scan' => action . new ( method_name : :send_scan_command ) ,
34
- 'lastsave' => action . new ( method_name : :send_command_to_all_nodes , reply_transformer : -> ( reply ) { reply . sort_by ( & :to_i ) } ) ,
44
+ 'lastsave' => action . new ( method_name : :send_command_to_all_nodes , reply_transformer : sort_numbers ) ,
35
45
'role' => action . new ( method_name : :send_command_to_all_nodes ) ,
36
46
'config' => action . new ( method_name : :send_config_command ) ,
37
47
'client' => action . new ( method_name : :send_client_command ) ,
@@ -59,8 +69,8 @@ class Router
59
69
'multi' => keyless_action ,
60
70
'unwatch' => keyless_action
61
71
} . each_with_object ( { } ) do |( k , v ) , acc |
62
- acc [ k ] = v
63
- acc [ k . upcase ] = v
72
+ acc [ k ] = v . freeze
73
+ acc [ k . upcase ] = v . freeze
64
74
end
65
75
end . call . freeze
66
76
0 commit comments