forge.retry¶
Exponential backoff, jitter, and circuit breaker for resilient external service calls.
forge.retry ¶
Retry and resilience module — exponential backoff, jitter, and circuit breaker.
Provides the @retry decorator and CircuitBreaker class for handling
transient failures in external service calls. Supports configurable backoff
strategies, exception filtering, and automatic logging.
Classes¶
CircuitBreaker ¶
Async circuit breaker with three-state machine.
States: CLOSED (normal) → OPEN (failing) → HALF_OPEN (probing).
Source code in src/forge/retry/circuit.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
CircuitBreakerOpenError ¶
NonRetryableError ¶
RetryError ¶
Bases: Exception
Raised when all retry attempts have been exhausted.
Attributes¶
original_exception : The last exception raised by the wrapped callable. attempt_count : Total number of attempts made (including the initial call). total_delay : Total wall-clock time spent sleeping between retries.
Source code in src/forge/retry/module.py
RetryModule ¶
Bases: ForgeModule
Source code in src/forge/retry/module.py
Methods:¶
circuit_breaker ¶
circuit_breaker(failure_threshold: int | None = None, recovery_time: float | None = None, *, name: str = '') -> Any
Return a :class:~forge.retry.circuit.CircuitBreaker with module defaults.
Source code in src/forge/retry/module.py
retry ¶
retry(fn: Callable[..., Any] | None = None, *, attempts: int | None = None, backoff: str | None = None, base_delay: float | None = None, max_delay: float | None = None, jitter: bool | None = None, timeout: float | None = None, retryable_exceptions: tuple[type[Exception], ...] | None = None) -> Any
Return a :func:retry pre-configured with module defaults.
Source code in src/forge/retry/module.py
Functions:¶
constant ¶
exponential ¶
Exponential backoff with full jitter.
Delay = U(0, min(base_delay * 2 ** (attempt - 1), max_delay))
Source code in src/forge/retry/backoff.py
get_backoff ¶
Return the backoff function for the given strategy name.
Raises ValueError for unknown strategies.
Source code in src/forge/retry/backoff.py
linear ¶
Linear backoff.
Delay = min(base_delay * attempt, max_delay)
retry ¶
retry(fn: Callable[..., Any] | None = None, *, attempts: int = 3, backoff: str = 'exponential', base_delay: float = 1.0, max_delay: float = 60.0, jitter: bool = True, timeout: float | None = None, retryable_exceptions: tuple[type[Exception], ...] | None = None) -> Any
Async retry — use as a decorator or async context manager.
Decorator form::
@retry(attempts=3)
async def fetch() -> bytes:
...
Context manager form::
async with retry(attempts=3):
data = await fetch()