PyBreaker models the health of a downstream dependency using three distinct states. EveryDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/danielfm/pybreaker/llms.txt
Use this file to discover all available pages before exploring further.
CircuitBreaker instance is always in exactly one of these states, and each state determines whether an incoming call is executed, rejected immediately, or treated as a probe. Understanding the states — and the conditions that drive transitions between them — is the key to using PyBreaker effectively.
Closed State
Constant:STATE_CLOSED = 'closed'
Closed is the normal, healthy operating state. Every call passes through to the guarded function as if the circuit breaker were not there.
Behavior:
- Calls are executed normally.
- On a successful call, the failure counter resets to
0. - On a system error, the failure counter increments by
1. - When
fail_counter >= fail_max, the circuit trips and transitions to Open.
Open State
Constant:STATE_OPEN = 'open'
Open is the tripped state. The circuit breaker has seen too many consecutive failures and is protecting the system by rejecting all calls immediately.
Behavior:
- All calls raise
CircuitBreakerErrorimmediately — the guarded function is never invoked. - PyBreaker records an
opened_attimestamp when the circuit opens. - After
reset_timeoutseconds have elapsed, the next call attempt causes a transition to Half-Open rather than raising immediately.
CircuitBreakerError is a distinct exception class. Catching it separately from other exceptions lets you apply a targeted fallback (cached data, a default value, a user-friendly error message) instead of treating the open-circuit condition the same as a real database error.Half-Open State
Constant:STATE_HALF_OPEN = 'half-open'
Half-open is the trial state. After the reset timeout expires, PyBreaker cautiously allows calls through again to test whether the downstream service has recovered.
Behavior:
- Calls are allowed through to the guarded function as trial calls.
- On each successful trial call, the success counter increments by 1. Once
success_counter >= success_threshold, the circuit closes. - On a failed trial call, the circuit immediately opens again and the reset timeout restarts.
- This prevents your code from hammering a service that is still recovering — traffic only resumes fully once the service has demonstrated it can handle requests successfully.
State Transitions
The following table describes every valid automatic and manual state transition:| From | To | Trigger |
|---|---|---|
CLOSED | OPEN | fail_counter >= fail_max (threshold reached) |
OPEN | HALF_OPEN | reset_timeout has elapsed; next call attempted |
HALF_OPEN | CLOSED | success_counter >= success_threshold |
HALF_OPEN | OPEN | Trial call raises a system error |
| Any | CLOSED | Manual: cb.close() |
| Any | OPEN | Manual: cb.open() |
| Any | HALF_OPEN | Manual: cb.half_open() |
