|
| 1 | +import random |
| 2 | +import time |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | + |
| 7 | +class BackoffStrategy: |
| 8 | + initial_interval: int |
| 9 | + max_interval: int |
| 10 | + exponent: float |
| 11 | + max_elapsed_time: int |
| 12 | + |
| 13 | + def __init__(self, initial_interval: int, max_interval: int, exponent: float, max_elapsed_time: int): |
| 14 | + self.initial_interval = initial_interval |
| 15 | + self.max_interval = max_interval |
| 16 | + self.exponent = exponent |
| 17 | + self.max_elapsed_time = max_elapsed_time |
| 18 | + |
| 19 | + |
| 20 | +class RetryConfig: |
| 21 | + strategy: str |
| 22 | + backoff: BackoffStrategy |
| 23 | + retry_connection_errors: bool |
| 24 | + |
| 25 | + def __init__(self, strategy: str, retry_connection_errors: bool): |
| 26 | + self.strategy = strategy |
| 27 | + self.retry_connection_errors = retry_connection_errors |
| 28 | + |
| 29 | + |
| 30 | +class Retries: |
| 31 | + config: RetryConfig |
| 32 | + status_codes: list[str] |
| 33 | + |
| 34 | + def __init__(self, config: RetryConfig, status_codes: list[str]): |
| 35 | + self.config = config |
| 36 | + self.status_codes = status_codes |
| 37 | + |
| 38 | + |
| 39 | +class TemporaryError(Exception): |
| 40 | + response: requests.Response |
| 41 | + |
| 42 | + def __init__(self, response: requests.Response): |
| 43 | + self.response = response |
| 44 | + |
| 45 | + |
| 46 | +class PermanentError(Exception): |
| 47 | + inner: Exception |
| 48 | + |
| 49 | + def __init__(self, inner: Exception): |
| 50 | + self.inner = inner |
| 51 | + |
| 52 | + |
| 53 | +def retry(fn, retries: Retries): |
| 54 | + if retries.config.strategy == 'backoff': |
| 55 | + def do_request(): |
| 56 | + res: requests.Response |
| 57 | + try: |
| 58 | + res = fn() |
| 59 | + |
| 60 | + for code in retries.status_codes: |
| 61 | + if "X" in code.upper(): |
| 62 | + codeRange = int(code[0]) |
| 63 | + |
| 64 | + s = res.status_code / 100 |
| 65 | + |
| 66 | + if s >= codeRange and s < codeRange + 1: |
| 67 | + raise TemporaryError(res) |
| 68 | + else: |
| 69 | + parsed_code = int(code) |
| 70 | + |
| 71 | + if res.status_code == parsed_code: |
| 72 | + raise TemporaryError(res) |
| 73 | + except requests.exceptions.ConnectionError as e: |
| 74 | + if not retries.config.config.retry_connection_errors: |
| 75 | + raise |
| 76 | + else: |
| 77 | + raise PermanentError(e) |
| 78 | + except requests.exceptions.Timeout as e: |
| 79 | + if not retries.config.config.retry_connection_errors: |
| 80 | + raise |
| 81 | + else: |
| 82 | + raise PermanentError(e) |
| 83 | + except TemporaryError: |
| 84 | + raise |
| 85 | + except Exception as e: |
| 86 | + raise PermanentError(e) |
| 87 | + |
| 88 | + return res |
| 89 | + |
| 90 | + return retry_with_backoff(do_request, retries.config.backoff.initial_interval, retries.config.backoff.max_interval, retries.config.backoff.exponent, retries.config.backoff.max_elapsed_time) |
| 91 | + else: |
| 92 | + fn() |
| 93 | + |
| 94 | + |
| 95 | +def retry_with_backoff(fn, initial_interval=500, max_interval=60000, exponent=1.5, max_elapsed_time=3600000): |
| 96 | + start = round(time.time()*1000) |
| 97 | + x = 0 |
| 98 | + |
| 99 | + while True: |
| 100 | + try: |
| 101 | + return fn() |
| 102 | + except PermanentError as e: |
| 103 | + raise e.inner |
| 104 | + except Exception as e: |
| 105 | + now = round(time.time()*1000) |
| 106 | + if now - start > max_elapsed_time: |
| 107 | + if isinstance(e, TemporaryError): |
| 108 | + return e.response |
| 109 | + else: |
| 110 | + raise |
| 111 | + sleep = ((initial_interval/1000) * |
| 112 | + exponent**x + random.uniform(0, 1)) |
| 113 | + if sleep > max_interval/1000: |
| 114 | + sleep = max_interval/1000 |
| 115 | + time.sleep(sleep) |
| 116 | + x += 1 |
0 commit comments