Retry
provide.foundation.resilience.retry
¶
TODO: Add module docstring.
Classes¶
RetryExecutor
¶
RetryExecutor(
policy: RetryPolicy,
on_retry: (
Callable[[int, Exception], None] | None
) = None,
time_source: Callable[[], float] | None = None,
sleep_func: Callable[[float], None] | None = None,
async_sleep_func: (
Callable[[float], Awaitable[None]] | None
) = None,
)
Unified retry execution engine.
This executor handles the actual retry loop logic for both sync and async functions, using a RetryPolicy for configuration. It's used internally by both the @retry decorator and RetryMiddleware.
Initialize retry executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
policy
|
RetryPolicy
|
Retry policy configuration |
required |
on_retry
|
Callable[[int, Exception], None] | None
|
Optional callback for retry events (attempt, error) |
None
|
time_source
|
Callable[[], float] | None
|
Optional callable that returns current time (for testing). Defaults to time.time() for production use. |
None
|
sleep_func
|
Callable[[float], None] | None
|
Optional synchronous sleep function (for testing). Defaults to time.sleep() for production use. |
None
|
async_sleep_func
|
Callable[[float], Awaitable[None]] | None
|
Optional asynchronous sleep function (for testing). Defaults to asyncio.sleep() for production use. |
None
|
Source code in provide/foundation/resilience/retry.py
Functions¶
execute_async
async
¶
Execute asynchronous function with retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Awaitable[T]]
|
Async function to execute |
required |
*args
|
Any
|
Positional arguments for func |
()
|
**kwargs
|
Any
|
Keyword arguments for func |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
Result from successful execution |
Raises:
| Type | Description |
|---|---|
Exception
|
The last exception raised if all retry attempts are exhausted |
Source code in provide/foundation/resilience/retry.py
execute_sync
¶
Execute synchronous function with retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., T]
|
Function to execute |
required |
*args
|
Any
|
Positional arguments for func |
()
|
**kwargs
|
Any
|
Keyword arguments for func |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
Result from successful execution |
Raises:
| Type | Description |
|---|---|
Exception
|
The last exception raised if all retry attempts are exhausted |
Source code in provide/foundation/resilience/retry.py
RetryPolicy
¶
Configuration for retry behavior.
This policy can be used with both the @retry decorator and transport middleware, providing a unified configuration model for all retry scenarios.
Attributes:
| Name | Type | Description |
|---|---|---|
max_attempts |
int
|
Maximum number of retry attempts (must be >= 1) |
backoff |
BackoffStrategy
|
Backoff strategy to use for delays |
base_delay |
float
|
Base delay in seconds between retries |
max_delay |
float
|
Maximum delay in seconds (caps exponential growth) |
jitter |
bool
|
Whether to add random jitter to delays (±25%) |
retryable_errors |
tuple[type[Exception], ...] | None
|
Tuple of exception types to retry (None = all) |
retryable_status_codes |
set[int] | None
|
Set of HTTP status codes to retry (for middleware) |
Functions¶
__str__
¶
Human-readable string representation.
calculate_delay
¶
Calculate delay for a given attempt number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attempt
|
int
|
Attempt number (1-based) |
required |
Returns:
| Type | Description |
|---|---|
float
|
Delay in seconds |
Source code in provide/foundation/resilience/retry.py
should_retry
¶
Determine if an error should be retried.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception that occurred |
required |
attempt
|
int
|
Current attempt number (1-based) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if should retry, False otherwise |
Source code in provide/foundation/resilience/retry.py
should_retry_response
¶
Check if HTTP response should be retried.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Any
|
Response object with status attribute |
required |
attempt
|
int
|
Current attempt number (1-based) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if should retry, False otherwise |