Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

__retry decorator

View client_with_retry.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class ApiClient(object):
default_retries = (1, 2, 5, 10, 20, 30)
 
def __retry(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
retries = kwargs.pop('retries', self.default_retries)
try:
if kwargs.pop('reconnect', False):
self.__init__(self.ftp_uri, retries=())
return func(self, *args, **kwargs)
except (IOError, OSError, EOFError):
if retries:
#exc_type, exc_value, exc_tb = sys.exc_info()
# TODO: strip 2 upper entries from exc_tb cause they are
# just wrapper calls
logger.exception(
"%s(%s, %s) raised exception. %s retries left. Sleeping %s secs. Caller traceback: %s\nInternal traceback:",
func.__name__, args, kwargs, len(retries), retries[0],
''.join(traceback.format_stack(
f=sys._getframe(
1 + len(self.default_retries) - len(retries)
),
limit=10,
)),
)
time.sleep(retries[0])
kwargs['retries'] = retries[1:]
kwargs['reconnect'] = True
 
return wrapper(self, *args, **kwargs)
else:
raise
return wrapper
 
@__retry
def __init__(self, uri):
self.uri = uri
# open connection to uri, blabla
 
@__retry
def do_stuff(self):
# eg: do stuff with self.connection
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.