Retry API Reference¶
concurry.core.retry.RetryConfig
¶
Bases: Typed
Configuration for retry behavior.
This class encapsulates all retry-related settings for worker method calls.
Attributes:
Name | Type | Description |
---|---|---|
num_retries |
Union[conint(ge=0), _NO_ARG_TYPE]
|
Maximum number of retry attempts after initial failure. Total attempts = num_retries + 1 (initial attempt). Default value is determined by global_config.defaults.num_retries |
retry_on |
Union[type, Callable, List[Union[type, Callable]]]
|
Exception types or callables that trigger retries. Can be a single exception class, a callable, or a list of either. Callables receive context as kwargs and should return bool. Default: [Exception] (retry on all exceptions). |
retry_algorithm |
Union[RetryAlgorithm, _NO_ARG_TYPE]
|
Backoff strategy for wait times. Default value is determined by global_config.defaults.retry_algorithm |
retry_wait |
Union[confloat(gt=0), _NO_ARG_TYPE]
|
Minimum wait time between retries in seconds. This is the base wait time before applying strategy and jitter. Default value is determined by global_config.defaults.retry_wait |
retry_jitter |
Union[confloat(ge=0, le=1), _NO_ARG_TYPE]
|
Jitter factor between 0 and 1. Uses Full Jitter algorithm from AWS: sleep = random(0, calculated_wait). Set to 0 to disable jitter. Default value is determined by global_config.defaults.retry_jitter |
retry_until |
Optional[Union[Callable, List[Callable]]]
|
Validation functions for output (default: None). Can be a single callable or list of callables. All must return True. Callables receive result and context as kwargs. If validation fails, triggers retry even without exception. |
Example
from concurry import RetryConfig, RetryAlgorithm
# Basic exponential backoff
config = RetryConfig(
num_retries=3,
retry_algorithm=RetryAlgorithm.Exponential,
retry_wait=1.0,
retry_jitter=0.3
)
# Retry only on specific exceptions
config = RetryConfig(
num_retries=5,
retry_on=[ConnectionError, TimeoutError],
retry_algorithm=RetryAlgorithm.Linear
)
# Custom exception filter
config = RetryConfig(
num_retries=3,
retry_on=lambda exception, **ctx: (
isinstance(exception, ValueError) and "retry" in str(exception)
)
)
# Output validation (e.g., for LLM responses)
config = RetryConfig(
num_retries=5,
retry_until=lambda result, **ctx: (
isinstance(result, dict) and "data" in result
)
)
Source code in src/concurry/core/retry.py
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
post_initialize() -> None
¶
Set defaults from global config for _NO_ARG values.
Source code in src/concurry/core/retry.py
validate_num_retries(v)
classmethod
¶
Validate num_retries is non-negative or _NO_ARG.
Source code in src/concurry/core/retry.py
validate_retry_wait(v)
classmethod
¶
Validate retry_wait is positive or _NO_ARG.
Source code in src/concurry/core/retry.py
validate_retry_jitter(v)
classmethod
¶
Validate retry_jitter is in [0, 1] or _NO_ARG.
Source code in src/concurry/core/retry.py
validate_retry_on(v)
classmethod
¶
Ensure retry_on is a list of exception types or callables.
Source code in src/concurry/core/retry.py
validate_retry_until(v)
classmethod
¶
Ensure retry_until is a list of callables.
Source code in src/concurry/core/retry.py
concurry.core.retry.RetryValidationError
¶
Bases: Exception
Raised when retry_until validation fails after all retries.
This exception contains all results from retry attempts and validation failure reasons, useful for debugging why validation failed.
Attributes:
Name | Type | Description |
---|---|---|
attempts |
Number of attempts made (including initial attempt) |
|
all_results |
List of all output values from each attempt |
|
validation_errors |
List of validation failure reasons for each attempt |
|
method_name |
Name of the method that was retried |
Example
Source code in src/concurry/core/retry.py
concurry.core.retry.calculate_retry_wait(attempt: int, config: RetryConfig) -> float
¶
Calculate wait time for a retry attempt with strategy and jitter.
This function implements three backoff strategies and applies Full Jitter as described in the AWS blog post on exponential backoff: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attempt
|
int
|
Retry attempt number (1-indexed, 1 = first retry) |
required |
config
|
RetryConfig
|
Retry configuration with strategy, wait time, and jitter settings |
required |
Returns:
Type | Description |
---|---|
float
|
Wait time in seconds (always >= 0) |