Skip to content

Commit 083e3fe

Browse files
authored
fix: make constant value shareable with Ractor (#429)
1 parent b818e49 commit 083e3fe

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

lib/redis_client/cluster/errors.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def with_config(config)
1212
end
1313

1414
ERR_ARG_NORMALIZATION = ->(arg) { Array[arg].flatten.reject { |e| e.nil? || (e.respond_to?(:empty?) && e.empty?) } }
15+
Ractor.make_shareable(ERR_ARG_NORMALIZATION) if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
1516

1617
private_constant :ERR_ARG_NORMALIZATION
1718

lib/redis_client/cluster/router.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ class Cluster
1717
class Router
1818
ZERO_CURSOR_FOR_SCAN = '0'
1919
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)
2021
DEDICATED_ACTIONS = lambda do # rubocop:disable Metrics/BlockLength
2122
action = Struct.new('RedisCommandRoutingAction', :method_name, :reply_transformer, keyword_init: true)
2223
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
2333
multiple_key_action = action.new(method_name: :send_multiple_keys_command)
2434
all_node_first_action = action.new(method_name: :send_command_to_all_nodes, reply_transformer: pick_first)
2535
primary_first_action = action.new(method_name: :send_command_to_primaries, reply_transformer: pick_first)
@@ -28,10 +38,10 @@ class Router
2838
{
2939
'ping' => action.new(method_name: :send_ping_command, reply_transformer: pick_first),
3040
'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),
3343
'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),
3545
'role' => action.new(method_name: :send_command_to_all_nodes),
3646
'config' => action.new(method_name: :send_config_command),
3747
'client' => action.new(method_name: :send_client_command),
@@ -59,8 +69,8 @@ class Router
5969
'multi' => keyless_action,
6070
'unwatch' => keyless_action
6171
}.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
6474
end
6575
end.call.freeze
6676

lib/redis_client/cluster_config.rb

+3-1
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)