import math import time import requests from flask import Response def sleep(seconds: int): time.sleep(seconds) def get(url: str, headers: dict, retry_limit=4) -> Response: """ Retry responses that have 5xx status codes as these have likely failed due to flakiness of other services. Exponentially sleeps in between calls, if retry_limit is reached then gives up retrying. """ attempts = 1 def should_retry(response: Response): return response.status_code in [502] def get_response(): return requests.get(url, headers=headers) response = get_response() if not should_retry(response): return response while attempts <= retry_limit: if should_retry(response): sleep(math.pow(attempts, 2)) response = get_response() attempts += 1 continue else: break return response