Decorators
provide.foundation.resilience.decorators
¶
TODO: Add module docstring.
Classes¶
Functions¶
circuit_breaker
¶
circuit_breaker(
failure_threshold: int = 5,
recovery_timeout: float = DEFAULT_CIRCUIT_BREAKER_RECOVERY_TIMEOUT,
expected_exception: (
type[Exception] | tuple[type[Exception], ...]
) = Exception,
time_source: Callable[[], float] | None = None,
registry: Registry | None = None,
) -> Callable[[F], F]
Create a circuit breaker decorator.
Creates a SyncCircuitBreaker for synchronous functions and an AsyncCircuitBreaker for asynchronous functions to avoid locking issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
failure_threshold
|
int
|
Number of failures before opening circuit. |
5
|
recovery_timeout
|
float
|
Seconds to wait before attempting recovery. |
DEFAULT_CIRCUIT_BREAKER_RECOVERY_TIMEOUT
|
expected_exception
|
type[Exception] | tuple[type[Exception], ...]
|
Exception type(s) that trigger the breaker. Can be a single exception type or a tuple of exception types. |
Exception
|
time_source
|
Callable[[], float] | None
|
Optional callable that returns current time (for testing). |
None
|
registry
|
Registry | None
|
Optional registry to register the breaker with (for DI). |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
Circuit breaker decorator. |
Examples:
>>> @circuit_breaker(failure_threshold=3, recovery_timeout=30)
... def unreliable_service():
... return external_api_call()
>>> @circuit_breaker(expected_exception=(ValueError, TypeError))
... async def async_unreliable_service():
... return await async_api_call()
Source code in provide/foundation/resilience/decorators.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
fallback
¶
Fallback decorator using FallbackChain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*fallback_funcs
|
Callable[..., Any]
|
Functions to use as fallbacks, in order of preference |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
Decorated function with fallback chain |
Examples:
>>> def backup_api():
... return "backup result"
...
>>> @fallback(backup_api)
... def primary_api():
... return external_api_call()
Source code in provide/foundation/resilience/decorators.py
reset_circuit_breakers_for_testing
async
¶
Reset all circuit breaker instances for test isolation.
This function is called by the test framework to ensure circuit breaker state doesn't leak between tests.
Note: This is an async function because AsyncCircuitBreaker has async methods. Both sync and async circuit breakers are reset using their reset() method.
Source code in provide/foundation/resilience/decorators.py
reset_test_circuit_breakers
async
¶
Reset circuit breaker instances created in test files.
This function resets circuit breakers that were created within test files to ensure proper test isolation without affecting production circuit breakers.
Note: This is an async function because AsyncCircuitBreaker has async methods. Both sync and async circuit breakers are reset using their reset() method.
Source code in provide/foundation/resilience/decorators.py
retry
¶
retry(
*exceptions: type[Exception],
policy: RetryPolicy | None = None,
max_attempts: int | None = None,
base_delay: float | None = None,
backoff: BackoffStrategy | None = None,
max_delay: float | None = None,
jitter: bool | None = None,
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], Any] | None = None
) -> Callable[[F], F]
Decorator for retrying operations on errors.
Can be used in multiple ways:
-
With a policy object: @retry(policy=RetryPolicy(max_attempts=5))
-
With individual parameters: @retry(max_attempts=3, base_delay=1.0)
-
With specific exceptions: @retry(ConnectionError, TimeoutError, max_attempts=3)
-
Without parentheses (uses defaults): @retry def my_func(): ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exceptions
|
type[Exception]
|
Exception types to retry (all if empty) |
()
|
policy
|
RetryPolicy | None
|
Complete retry policy (overrides other params) |
None
|
max_attempts
|
int | None
|
Maximum retry attempts |
None
|
base_delay
|
float | None
|
Base delay between retries |
None
|
backoff
|
BackoffStrategy | None
|
Backoff strategy |
None
|
max_delay
|
float | None
|
Maximum delay cap |
None
|
jitter
|
bool | None
|
Whether to add jitter |
None
|
on_retry
|
Callable[[int, Exception], None] | None
|
Callback for retry events |
None
|
time_source
|
Callable[[], float] | None
|
Optional callable that returns current time (for testing) |
None
|
sleep_func
|
Callable[[float], None] | None
|
Optional synchronous sleep function (for testing) |
None
|
async_sleep_func
|
Callable[[float], Any] | None
|
Optional asynchronous sleep function (for testing) |
None
|
Returns:
| Type | Description |
|---|---|
Callable[[F], F]
|
Decorated function with retry logic |
Examples:
>>> @retry(ConnectionError, max_attempts=5, base_delay=2.0)
... async def connect_to_service():
... # Async function with specific error handling
... pass
Source code in provide/foundation/resilience/decorators.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |