Skip to content

Instantly share code, notes, and snippets.

@Turin86
Last active December 18, 2017 10:33
Show Gist options
  • Save Turin86/66919f717e3c8f9af229 to your computer and use it in GitHub Desktop.
Save Turin86/66919f717e3c8f9af229 to your computer and use it in GitHub Desktop.
Utility class for exponential backoff
# https://code.google.com/p/google-api-python-client/source/browse/samples/threadqueue/main.py?spec=svn67b8dcf946169e1ad62ecb0555942865b9186ea8&r=67b8dcf946169e1ad62ecb0555942865b9186ea8
from __future__ import print_function
from random import randint
import sys
from time import sleep
class Backoff:
"""Exponential Backoff
Implements an exponential backoff algorithm.
Instantiate and call loop() each time through
the loop, and each time a request fails call
fail() which will delay an appropriate amount
of time.
"""
def __init__(self, maxretries=7):
self.retry = 0
if not isinstance(maxretries, int) or maxretries < 1:
self.maxretries = 7
else:
self.maxretries = maxretries
def loop(self):
return self.retry < self.maxretries
def fail(self):
self.retry += 1
sleeptime = 2 ** self.retry
print('Retrying in {} seconds...'.format(sleeptime))
sleep(sleeptime)
def backoff(self, function, kwargs={}, *args):
while self.loop():
try:
return function(*args, **kwargs)
except Exception as e:
error = e
print(str(e))
self.fail()
print('Backoff failed: {}'.format(str(e)), file=sys.stderr)
raise error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment