Circuit sync
๐ค AI-Generated Content
This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
provide.foundation.resilience.circuit_sync
¶
Classes¶
SyncCircuitBreaker
¶
SyncCircuitBreaker(
failure_threshold: int = 5,
recovery_timeout: float = 30.0,
expected_exception: type[Exception]
| tuple[type[Exception], ...] = Exception,
time_source: Callable[[], float] | None = None,
)
Synchronous circuit breaker for resilience patterns.
Uses threading.RLock for thread-safe state management in synchronous code. For async code, use AsyncCircuitBreaker instead.
Initialize the synchronous 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_sync.py
Functions¶
call
¶
Execute a synchronous function through the circuit breaker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
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_sync.py
failure_count
¶
reset
¶
Reset the circuit breaker to its initial state.
state
¶
Get the current state of the circuit breaker.