Skip to content

fix: make constants shareable with Ractor #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/redis_client/cluster/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def with_config(config)
end

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

private_constant :ERR_ARG_NORMALIZATION

Expand Down
20 changes: 15 additions & 5 deletions lib/redis_client/cluster/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ class Cluster
class Router
ZERO_CURSOR_FOR_SCAN = '0'
TSF = ->(f, x) { f.nil? ? x : f.call(x) }.curry
Ractor.make_shareable(TSF) if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
DEDICATED_ACTIONS = lambda do # rubocop:disable Metrics/BlockLength
action = Struct.new('RedisCommandRoutingAction', :method_name, :reply_transformer, keyword_init: true)
pick_first = ->(reply) { reply.first } # rubocop:disable Style/SymbolProc
flatten_strings = ->(reply) { reply.flatten.sort_by(&:to_s) }
sum_num = ->(reply) { reply.select { |e| e.is_a?(Integer) }.sum }
sort_numbers = ->(reply) { reply.sort_by(&:to_i) }
if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
Ractor.make_shareable(pick_first)
Ractor.make_shareable(flatten_strings)
Ractor.make_shareable(sum_num)
Ractor.make_shareable(sort_numbers)
end
multiple_key_action = action.new(method_name: :send_multiple_keys_command)
all_node_first_action = action.new(method_name: :send_command_to_all_nodes, reply_transformer: pick_first)
primary_first_action = action.new(method_name: :send_command_to_primaries, reply_transformer: pick_first)
Expand All @@ -28,10 +38,10 @@ class Router
{
'ping' => action.new(method_name: :send_ping_command, reply_transformer: pick_first),
'wait' => action.new(method_name: :send_wait_command),
'keys' => action.new(method_name: :send_command_to_replicas, reply_transformer: ->(reply) { reply.flatten.sort_by(&:to_s) }),
'dbsize' => action.new(method_name: :send_command_to_replicas, reply_transformer: ->(reply) { reply.select { |e| e.is_a?(Integer) }.sum }),
'keys' => action.new(method_name: :send_command_to_replicas, reply_transformer: flatten_strings),
'dbsize' => action.new(method_name: :send_command_to_replicas, reply_transformer: sum_num),
'scan' => action.new(method_name: :send_scan_command),
'lastsave' => action.new(method_name: :send_command_to_all_nodes, reply_transformer: ->(reply) { reply.sort_by(&:to_i) }),
'lastsave' => action.new(method_name: :send_command_to_all_nodes, reply_transformer: sort_numbers),
'role' => action.new(method_name: :send_command_to_all_nodes),
'config' => action.new(method_name: :send_config_command),
'client' => action.new(method_name: :send_client_command),
Expand Down Expand Up @@ -59,8 +69,8 @@ class Router
'multi' => keyless_action,
'unwatch' => keyless_action
}.each_with_object({}) do |(k, v), acc|
acc[k] = v
acc[k.upcase] = v
acc[k] = v.freeze
acc[k.upcase] = v.freeze
end
end.call.freeze

Expand Down
4 changes: 3 additions & 1 deletion lib/redis_client/cluster_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class ClusterConfig
DEFAULT_PORT = 6379
DEFAULT_SCHEME = 'redis'
SECURE_SCHEME = 'rediss'
DEFAULT_NODES = ["#{DEFAULT_SCHEME}://#{DEFAULT_HOST}:#{DEFAULT_PORT}"].freeze
DEFAULT_NODE = "#{DEFAULT_SCHEME}://#{DEFAULT_HOST}:#{DEFAULT_PORT}"
Ractor.make_shareable(DEFAULT_NODE) if Object.const_defined?(:Ractor, false) && Ractor.respond_to?(:make_shareable)
DEFAULT_NODES = [DEFAULT_NODE].freeze
VALID_SCHEMES = [DEFAULT_SCHEME, SECURE_SCHEME].freeze
VALID_NODES_KEYS = %i[ssl username password host port db].freeze
MERGE_CONFIG_KEYS = %i[ssl username password].freeze
Expand Down