-
-
Save mekarpeles/4633838 to your computer and use it in GitHub Desktop.
Added debug kwarg
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
def exponential_backoff(exception, err=None, tries=5, debug=False): | |
"""Exponentially backoff on a certain Exception exception. | |
params: | |
tries - num of expn backed off attempts before quitting | |
err - custom error msg to display in addition to 'e' | |
debug - default False, boolean deciding whether to raise | |
error msg e along with Exception instance | |
note: | |
* debug flag used to avoid raising security sensitive exception err msgs | |
* sleep may be unsafe (hence adding a psuedo random | |
scalar, still bad since not really random) | |
* Consider logging errors via http://pypi.python.org/pypi/sentry | |
usage: | |
>>> @exponential_backoff(SomeNetworkError, tries=2) | |
... def foo(bar): | |
... transfer_file(bar) | |
SomeNetworkError: [ExponentialBackoff] Timed out | |
after 2 attempts. Network failed to connect. (details: None) | |
""" | |
def decorator(func): | |
def inner(*args, **kwargs): | |
for n in range(0, tries): | |
try: | |
return func(*args, **kwargs) | |
except exception as e: | |
scalar = random.randint(0, 1000) / 1000.0 | |
time.sleep((2 ** n) + scalar) | |
e = e if debug else '' | |
raise exception('[Exponential Backoff] ' \ | |
'Timed out after %s attempts. ' \ | |
'%s (details: %s)' % (tries, e, err)) | |
return inner | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally authored this as anonymous, accidentally. Forking back to keep track of it.