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
Fix cluster connecting option bug #889
Merged
byroot
merged 4 commits into
redis:master
from
supercaracal:fix-password-option-bug-for-cluster
Feb 1, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
538034a
Fix redundant make task declarations
supercaracal 7a6dabc
Fix cluster connecting option bug and resolve #888
supercaracal 584a8c6
Fix several codes for clean and readable code
supercaracal c44fdde
Use Hash#reject insted of Hash#compact for legacy Ruby
supercaracal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,35 +15,33 @@ class Option | |
def initialize(options) | ||
options = options.dup | ||
node_addrs = options.delete(:cluster) | ||
@node_uris = build_node_uris(node_addrs) | ||
@node_opts = build_node_options(node_addrs) | ||
@replica = options.delete(:replica) == true | ||
add_common_node_option_if_needed(options, @node_opts, :scheme) | ||
add_common_node_option_if_needed(options, @node_opts, :password) | ||
@options = options | ||
end | ||
|
||
def per_node_key | ||
@node_uris.map { |uri| [NodeKey.build_from_uri(uri), @options.merge(url: uri.to_s)] } | ||
@node_opts.map { |opt| [NodeKey.build_from_host_port(opt[:host], opt[:port]), @options.merge(opt)] } | ||
.to_h | ||
end | ||
|
||
def secure? | ||
@node_uris.any? { |uri| uri.scheme == SECURE_SCHEME } || @options[:ssl_params] || false | ||
end | ||
|
||
def use_replica? | ||
@replica | ||
end | ||
|
||
def update_node(addrs) | ||
@node_uris = build_node_uris(addrs) | ||
@node_opts = build_node_options(addrs) | ||
end | ||
|
||
def add_node(host, port) | ||
@node_uris << parse_node_hash(host: host, port: port) | ||
@node_opts << { host: host, port: port } | ||
end | ||
|
||
private | ||
|
||
def build_node_uris(addrs) | ||
def build_node_options(addrs) | ||
raise InvalidClientOptionError, 'Redis option of `cluster` must be an Array' unless addrs.is_a?(Array) | ||
addrs.map { |addr| parse_node_addr(addr) } | ||
end | ||
|
@@ -53,7 +51,7 @@ def parse_node_addr(addr) | |
when String | ||
parse_node_url(addr) | ||
when Hash | ||
parse_node_hash(addr) | ||
parse_node_option(addr) | ||
else | ||
raise InvalidClientOptionError, 'Redis option of `cluster` must includes String or Hash' | ||
end | ||
|
@@ -62,15 +60,27 @@ def parse_node_addr(addr) | |
def parse_node_url(addr) | ||
uri = URI(addr) | ||
raise InvalidClientOptionError, "Invalid uri scheme #{addr}" unless VALID_SCHEMES.include?(uri.scheme) | ||
uri | ||
|
||
db = uri.path.split('/')[1]&.to_i | ||
{ scheme: uri.scheme, password: uri.password, host: uri.host, port: uri.port, db: db }.compact | ||
rescue URI::InvalidURIError => err | ||
raise InvalidClientOptionError, err.message | ||
end | ||
|
||
def parse_node_hash(addr) | ||
def parse_node_option(addr) | ||
addr = addr.map { |k, v| [k.to_sym, v] }.to_h | ||
raise InvalidClientOptionError, 'Redis option of `cluster` must includes `:host` and `:port` keys' if addr.values_at(:host, :port).any?(&:nil?) | ||
URI::Generic.build(scheme: DEFAULT_SCHEME, host: addr[:host], port: addr[:port].to_i) | ||
|
||
addr | ||
end | ||
|
||
# Redis cluster node returns only host and port information. | ||
# So we should complement additional information such as: | ||
# scheme, password and so on. | ||
def add_common_node_option_if_needed(options, node_opts, key) | ||
return options if options[key].nil? && node_opts.first[key].nil? | ||
|
||
options[key] = options[key] || node_opts.first[key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed 584a8c6 |
||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but since the return values doesn't matter,
.each
would make sense here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed 584a8c6