Retry Decorator
Automatically retry operations that fail due to transient errors.
Usage
The @retry decorator handles exponential backoff and specific exception targeting.
routes/upstream.py
from jec_api.decorators import retry
# Retry 3 times, catching only ConnectionError, doubling wait time (1s, 2s, 4s)
@retry(attempts=3, delay=1.0, backoff=2.0, exceptions=(ConnectionError,))
async def flaky_upstream_call():
...Signature
def retry(
attempts: int = 3,
delay: float = 1.0,
backoff: float = 2.0,
exceptions: tuple = (Exception,)
)- attempts: Total attempts (1 initial + retries).
- delay: Initial wait time between retries (seconds).
- backoff: Multiplier for delay after each failure.
- exceptions: Tuple of Exception classes to catch.