-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Playing with retries. #2040
Merged
Merged
Playing with retries. #2040
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dc08cf
Playing with retries.
daspecster 1de7a5c
Feedback updates.
daspecster 49b3027
Switch retry to Class style decorator.
daspecster c532444
Fix tries counter and change comments.
daspecster f33b2ea
Scoping the settings.
daspecster e06815d
Change comments, Retry class properties.
daspecster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import time | ||
from functools import wraps | ||
|
||
import six | ||
|
||
|
||
class Retry(object): | ||
"""Retry class for retrying eventually consistent resources in testing.""" | ||
|
||
def __init__(self, exception, tries=4, delay=3, backoff=2, logger=None): | ||
"""Retry calling the decorated function using an exponential backoff. | ||
|
||
:type exception: Exception or tuple of Exceptions | ||
:param exception: The exception to check or may be a tuple of | ||
exceptions to check. | ||
|
||
:type tries: int | ||
:param tries: Number of times to try (not retry) before giving up. | ||
|
||
:type delay: int | ||
:param delay: Initial delay between retries in seconds. | ||
|
||
:type backoff: int | ||
:param backoff: Backoff multiplier e.g. value of 2 will double the | ||
delay each retry. | ||
|
||
:type logger: logging.Logger instance | ||
:param logger: Logger to use. If None, print. | ||
""" | ||
|
||
self.exception = exception | ||
self.tries = tries | ||
self.delay = delay | ||
self.backoff = backoff | ||
self.logger = logger.warning if logger else six.print_ | ||
|
||
def __call__(self, to_wrap): | ||
@wraps(to_wrap) | ||
def wrapped_function(*args, **kwargs): | ||
tries_counter = self.tries | ||
delay = self.delay | ||
while tries_counter > 0: | ||
try: | ||
return to_wrap(*args, **kwargs) | ||
except self.exception as caught_exception: | ||
msg = ("%s, Trying again in %d seconds..." % | ||
(str(caught_exception), delay)) | ||
self.logger(msg) | ||
|
||
time.sleep(delay) | ||
tries_counter -= 1 | ||
delay *= self.backoff | ||
return to_wrap(*args, **kwargs) | ||
|
||
return wrapped_function |
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.
This comment was marked as spam.
Sorry, something went wrong.