Circuit async
provide.foundation.resilience.circuit_async
¶
TODO: Add module docstring.
Classes¶
AsyncCircuitBreaker
¶
AsyncCircuitBreaker(
failure_threshold: int = 5,
recovery_timeout: float = 30.0,
expected_exception: (
type[Exception] | tuple[type[Exception], ...]
) = Exception,
time_source: Callable[[], float] | None = None,
)
Asynchronous circuit breaker for resilience patterns.
Uses asyncio.Lock for async-safe state management. For synchronous code, use SyncCircuitBreaker instead.
Initialize the asynchronous circuit breaker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
failure_threshold
|
int
|
Number of failures before opening circuit |
5
|
recovery_timeout
|
float
|
Seconds to wait before attempting recovery |
30.0
|
expected_exception
|
type[Exception] | tuple[type[Exception], ...]
|
Exception type(s) to catch |
Exception
|
time_source
|
Callable[[], float] | None
|
Optional callable that returns current time (for testing). Defaults to time.time() for production use. |
None
|
Source code in provide/foundation/resilience/circuit_async.py
Functions¶
call
async
¶
Execute an asynchronous function through the circuit breaker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Async callable to execute |
required |
*args
|
Any
|
Positional arguments for func |
()
|
**kwargs
|
Any
|
Keyword arguments for func |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Result from func |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If circuit is open |
Exception
|
Whatever exception func raises |
Source code in provide/foundation/resilience/circuit_async.py
failure_count
async
¶
reset
async
¶
Reset the circuit breaker to its initial state.
state
async
¶
Get the current state of the circuit breaker.
Returns:
| Type | Description |
|---|---|
CircuitState
|
Current circuit state |