Skip to content
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

Accept string keys in client config (sentinel) #515

Closed
wants to merge 1 commit into from

Conversation

maximerety
Copy link

There's already code to convert string keys into symbols in the options hash, but this is not working on the "sentinel part" of the options hash.

This PR adds support to init a Redis client like this:

Redis.new({
  "url" => "redis://master1",
  "sentinels" => [
    {"host" => "127.0.0.1", "port" => 26381},
    {"host" => "127.0.0.1", "port" => 26382}
  ],
  "role" => "master"
})

whereas previously it was mandatory to use :sentinels, :host, :port and :role.

This is especially useful when initializing a Redis client from a json config file, e.g.:

Redis.new(JSON.parse(File.read("config/redis.json")))

Side notes :

  • I'm also duplicating the nested array (map) and hashes (dup) of the options[:sentinels] value, to avoid modifying the original options hash (it was done here for the parent options hash)
  • I'm always deleting string keys when converting them into symbols to avoid keeping "duplicate" keys (one string + one symbol)
  • The test/sentinel_test.rb has been updated to test both cases (string keys vs. symbol keys)

@badboy
Copy link
Contributor

badboy commented Feb 24, 2015

Thanks. Looking into it.

@djanowski
Copy link
Collaborator

I'm hesitant.

In the future we'll move to named arguments, so I don't want to add more code to deal with string keys.

Especially when in your case it's just:

Redis.new(JSON.parse(File.read("config/redis.json"), symbolize_names: true))

@maximerety
Copy link
Author

Good point!

Though even with keyword arguments, it could still not be clear if the sentinels hashes should contain symbol or string keys (the sentinels: keyword argument will not impose any limitation/check on :host vs. "host", a.s.o.). Maybe add a warning in README.md to explain that all config keys should always be symbols?

I was surprised that it worked well when testing without sentinels (e.g. Redis.new({ "host" => "localhost", "port" => 6380 }) is ok) but then it didn't work with sentinels. I had to dive into the gem code to understand the reason of this failure... which was not straightforward. And I was even more confused to see a piece of code already in charge of converting string keys to symbols.

But I won't be offended if you decide this is not the way to go!

@quixoten
Copy link
Contributor

quixoten commented Nov 3, 2015

I implemented #559 before I saw this PR. It's an alternative implementation for the same issue. I don't know if it resolves the issue in a way that will alleviate @djanowski hesitations, but I offer it up for your consideration in the hopes that it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants