Skip to content

Instantly share code, notes, and snippets.

@datashaman
Last active March 11, 2022 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datashaman/fc02882d6be49d0b882f to your computer and use it in GitHub Desktop.
Save datashaman/fc02882d6be49d0b882f to your computer and use it in GitHub Desktop.
Decorator version of resilientsession
from requests import Session
from requests.exceptions import ConnectionError
import logging
import time
def is_recoverable(error, url, request, counter=1):
if hasattr(error,'status_code'):
if error.status_code in [502, 503, 504]:
error = "HTTP %s" % error.status_code
else:
return False
DELAY = 10 * counter
logging.warn("Got recoverable error [%s] from %s %s, retry #%s in %ss" % (error, request, url, counter, DELAY))
time.sleep(DELAY)
return True
def recoverable(f):
def func(self, url, **kwargs):
counter = 0
while True:
counter += 1
try:
r = f(self, url, **kwargs)
except ConnectionError as e:
r = e.message
if is_recoverable(r, url, f.__name__.upper(), counter):
continue
return r
return func
class ResilientSession(Session):
"""
This class is supposed to retry requests that do return temporary errors.
At this moment it supports: 502, 503, 504
"""
@recoverable
def get(self, *args, **kwargs):
return super(ResilientSession, self).get(*args, **kwargs)
@recoverable
def post(self, *args, **kwargs):
return super(ResilientSession, self).post(*args, **kwargs)
@recoverable
def delete(self, *args, **kwargs):
return super(ResilientSession, self).delete(*args, **kwargs)
@recoverable
def put(self, *args, **kwargs):
return super(ResilientSession, self).put(*args, **kwargs)
@recoverable
def head(self, *args, **kwargs):
return super(ResilientSession, self).head(*args, **kwargs)
@recoverable
def patch(self, *args, **kwargs):
return super(ResilientSession, self).patch(*args, **kwargs)
@recoverable
def options(self, *args, **kwargs):
return super(ResilientSession, self).options(*args, **kwargs)
if __name__ == '__main__':
s = ResilientSession()
s.get('http://httpstat.us/502')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment