(*args, **kwargs)
| 25 | """Retry a function with exponential backoff.""" |
| 26 | |
| 27 | def wrapper(*args, **kwargs): # type: ignore |
| 28 | # Initialize variables |
| 29 | num_retries = 0 |
| 30 | delay = initial_delay |
| 31 | |
| 32 | # Loop until a successful response or max_retries is hit or an exception is raised |
| 33 | while True: |
| 34 | try: |
| 35 | return func(*args, **kwargs) |
| 36 | # Retry on specified errors |
| 37 | except errors as e: |
| 38 | # Increment retries |
| 39 | num_retries += 1 |
| 40 | |
| 41 | # Check if max retries has been reached |
| 42 | if num_retries > max_retries: |
| 43 | raise Exception( |
| 44 | f"Maximum number of retries ({max_retries}) exceeded." |
| 45 | ) |
| 46 | |
| 47 | # Increment the delay |
| 48 | delay *= exponential_base * (1 + jitter * random.random()) |
| 49 | print(f"Retrying in {delay} seconds.") |
| 50 | # Sleep for the delay |
| 51 | time.sleep(delay) |
| 52 | |
| 53 | # Raise exceptions for any errors not specified |
| 54 | except Exception as e: |
| 55 | raise e |
| 56 | |
| 57 | return wrapper |
| 58 |
nothing calls this directly
no outgoing calls
no test coverage detected