Skip to content

Commit 3f55fb6

Browse files
committed
fix: make constant value sharable for Ractor
1 parent 4a4dd65 commit 3f55fb6

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

lib/redis_client/cluster/router.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ class Router
2020
DEDICATED_ACTIONS = lambda do # rubocop:disable Metrics/BlockLength
2121
action = Struct.new('RedisCommandRoutingAction', :method_name, :reply_transformer, keyword_init: true)
2222
pick_first = ->(reply) { reply.first } # rubocop:disable Style/SymbolProc
23+
flatten_strings = ->(reply) { reply.flatten.sort_by(&:to_s) }
24+
sum_num = ->(reply) { reply.select { |e| e.is_a?(Integer) }.sum }
25+
sort_numbers = ->(reply) { reply.sort_by(&:to_i) }
26+
if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
27+
Ractor.make_shareable(pick_first)
28+
Ractor.make_shareable(flatten_strings)
29+
Ractor.make_shareable(sum_num)
30+
Ractor.make_shareable(sort_numbers)
31+
end
2332
multiple_key_action = action.new(method_name: :send_multiple_keys_command)
2433
all_node_first_action = action.new(method_name: :send_command_to_all_nodes, reply_transformer: pick_first)
2534
primary_first_action = action.new(method_name: :send_command_to_primaries, reply_transformer: pick_first)
@@ -28,10 +37,10 @@ class Router
2837
{
2938
'ping' => action.new(method_name: :send_ping_command, reply_transformer: pick_first),
3039
'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 }),
40+
'keys' => action.new(method_name: :send_command_to_replicas, reply_transformer: flatten_strings),
41+
'dbsize' => action.new(method_name: :send_command_to_replicas, reply_transformer: sum_num),
3342
'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) }),
43+
'lastsave' => action.new(method_name: :send_command_to_all_nodes, reply_transformer: sort_numbers),
3544
'role' => action.new(method_name: :send_command_to_all_nodes),
3645
'config' => action.new(method_name: :send_config_command),
3746
'client' => action.new(method_name: :send_client_command),
@@ -59,8 +68,8 @@ class Router
5968
'multi' => keyless_action,
6069
'unwatch' => keyless_action
6170
}.each_with_object({}) do |(k, v), acc|
62-
acc[k] = v
63-
acc[k.upcase] = v
71+
acc[k] = v.freeze
72+
acc[k.upcase] = v.freeze
6473
end
6574
end.call.freeze
6675

lib/redis_client/cluster_config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class ClusterConfig
1414
DEFAULT_PORT = 6379
1515
DEFAULT_SCHEME = 'redis'
1616
SECURE_SCHEME = 'rediss'
17-
DEFAULT_NODES = ["#{DEFAULT_SCHEME}://#{DEFAULT_HOST}:#{DEFAULT_PORT}"].freeze
17+
DEFAULT_NODE = "#{DEFAULT_SCHEME}://#{DEFAULT_HOST}:#{DEFAULT_PORT}"
18+
Ractor.make_shareable(DEFAULT_NODE) if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
19+
DEFAULT_NODES = [DEFAULT_NODE].freeze
1820
VALID_SCHEMES = [DEFAULT_SCHEME, SECURE_SCHEME].freeze
1921
VALID_NODES_KEYS = %i[ssl username password host port db].freeze
2022
MERGE_CONFIG_KEYS = %i[ssl username password].freeze

0 commit comments

Comments
 (0)