forked from mongoid/moped
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mongoid#351 from wandenberg/fix_retries_operations
fix retries operations
- Loading branch information
Showing
6 changed files
with
397 additions
and
62 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# encoding: utf-8 | ||
module Moped | ||
# Provides the shared behaviour for retry failed operations. | ||
# | ||
# @since 2.0.0 | ||
module Retryable | ||
|
||
private | ||
|
||
# Execute the provided block on the cluster and retry if the execution | ||
# fails. | ||
# | ||
# @api private | ||
# | ||
# @example Execute with retry. | ||
# preference.with_retry(cluster) do | ||
# cluster.with_primary do |node| | ||
# node.refresh | ||
# end | ||
# end | ||
# | ||
# @param [ Cluster ] cluster The cluster. | ||
# @param [ Integer ] retries The number of times to retry. | ||
# | ||
# @return [ Object ] The result of the block. | ||
# | ||
# @since 2.0.0 | ||
def with_retry(cluster, retries = cluster.max_retries, &block) | ||
begin | ||
block.call | ||
rescue Errors::ConnectionFailure, Errors::PotentialReconfiguration => e | ||
raise e if e.is_a?(Errors::PotentialReconfiguration) && | ||
! (e.message.include?("not master") || e.message.include?("Not primary")) | ||
|
||
if retries > 0 | ||
Loggable.warn(" MOPED:", "Retrying connection attempt #{retries} more time(s).", "n/a") | ||
sleep(cluster.retry_interval) | ||
cluster.refresh | ||
with_retry(cluster, retries - 1, &block) | ||
else | ||
raise e | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.