Skip to content

Instantly share code, notes, and snippets.

@mekarpeles
Forked from anonymous/decorate.py
Last active December 11, 2015 17:19
Show Gist options
  • Save mekarpeles/4633838 to your computer and use it in GitHub Desktop.
Save mekarpeles/4633838 to your computer and use it in GitHub Desktop.
Added debug kwarg
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
@mekarpeles
Copy link
Author

Originally authored this as anonymous, accidentally. Forking back to keep track of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment