Skip to content

Commit

Permalink
Convert Redis options nested keys to symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeremyanin committed May 7, 2024
1 parent f8ab7d2 commit 8a32c7c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/sidekiq/redis_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Sidekiq
module RedisConnection
class << self
def create(options = {})
symbolized_options = options.transform_keys(&:to_sym)
symbolized_options = deep_symbolize_keys(options)
symbolized_options[:url] ||= determine_redis_provider

logger = symbolized_options.delete(:logger)
Expand All @@ -27,6 +27,19 @@ def create(options = {})

private

def deep_symbolize_keys(object)
case object
when Hash
object.each_with_object({}) do |(key, value), result|
result[key.to_sym] = deep_symbolize_keys(value)
end
when Array
object.map { |e| deep_symbolize_keys(e) }
else
object
end
end

def scrub(options)
redacted = "REDACTED"

Expand Down
20 changes: 20 additions & 0 deletions test/redis_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ def server_connection(args = {})
refute_includes(output, "ssl_params")
end
end

it "symbolizes redis options keys" do
options = {
"name" => "mymaster",
"sentinels" => [
{"host" => "host1", "port" => 26379, "password" => "secret"},
{"host" => "host2", "port" => 26379, "password" => "secret"},
{"host" => "host3", "port" => 26379, "password" => "secret"}
]
}

pool = Sidekiq::RedisConnection.create(options)
config = config_for(pool.checkout)

config.sentinels.each.with_index do |sentinel_config, idx|
assert_equal options["sentinels"][idx]["host"], sentinel_config.host
assert_equal options["sentinels"][idx]["port"], sentinel_config.port
assert_equal options["sentinels"][idx]["password"], sentinel_config.password
end
end
end

describe ".determine_redis_provider" do
Expand Down

0 comments on commit 8a32c7c

Please sign in to comment.