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
Add auto retry mechanism on connection failure #133
Changes from 6 commits
b466007
2099bd7
d89b956
1a7092a
81e754f
ad0f060
9672497
a1c1134
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ class Keyspace | |
attr_reader :hosts | ||
# @return Integer port to connect to Cassandra nodes on | ||
attr_reader :port | ||
# @return Integer maximum number of retries to reconnect to Cassandra | ||
attr_reader :max_retries | ||
# @return [Symbol] the default consistency for queries in this keyspace | ||
# @since 1.1.0 | ||
attr_writer :default_consistency | ||
|
@@ -98,6 +100,8 @@ def initialize(configuration={}) | |
# single Cassandra instance to connect to | ||
# @option configuration [Integer] :port (9042) port on which to connect | ||
# to all specified hosts | ||
# @option configuration [Integer] :max-retries maximum number of retries | ||
# on connection failure | ||
# @option configuration [Array<String>] :hosts list of Cassandra | ||
# instances to connect to (hostnames only) | ||
# @option configuration [String] :username user to auth with (leave blank | ||
|
@@ -115,9 +119,11 @@ def configure(configuration = {}) | |
@configuration = configuration | ||
|
||
@hosts, @port = extract_hosts_and_port(configuration) | ||
@credentials = extract_credentials(configuration) | ||
@credentials = extract_credentials(configuration) | ||
@max_retries = extract_max_retries(configuration) | ||
|
||
@name = configuration[:keyspace] | ||
|
||
# reset the connections | ||
clear_active_connections! | ||
end | ||
|
@@ -153,14 +159,27 @@ def client | |
# | ||
# Execute a CQL query in this keyspace | ||
# | ||
# If a connection error occurs, will retry a maximum number of | ||
# time (default 3) before re-raising the original connection | ||
# error. | ||
# | ||
# @param statement [String] CQL string | ||
# @param bind_vars [Object] values for bind variables | ||
# @return [Enumerable] the results of the query | ||
# | ||
# @see #execute_with_consistency | ||
# | ||
def execute(statement, *bind_vars) | ||
execute_with_consistency(statement, bind_vars, default_consistency) | ||
retries = max_retries | ||
|
||
begin | ||
execute_with_consistency(statement, bind_vars, default_consistency) | ||
rescue Cql::NotConnectedError, Ione::Io::ConnectionError => e | ||
@raw_client = nil | ||
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. I don't think this will have the intended effect since 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. As a meta-point, I'm not thrilled about having both the 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. I didn't look closely enough for the |
||
raise e if retries < 0 | ||
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.
|
||
retries -= 1 | ||
retry | ||
end | ||
end | ||
|
||
# | ||
|
@@ -269,6 +288,10 @@ def extract_hosts_and_port(configuration) | |
def extract_credentials(configuration) | ||
configuration.slice(:username, :password).presence | ||
end | ||
|
||
def extract_max_retries(configuration) | ||
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. It doesn't seem strictly necessary to have this extracted into a method but I don't feel strongly one way or another. |
||
configuration.fetch(:"max-retries", 3) | ||
end | ||
end | ||
end | ||
end |
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.
I think you mean
:max_retries
not:max-retries
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.
Oh I guess you do mean
:max-retries
but:max_retries
seems more idiomatic no?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.
I don't think it matters—I can do either it either way. If you feel strongly about it, I have no problem changing it to an underscore.