pub struct CircuitBreaker { /* private fields */ }Expand description
A circuit breaker that tracks consecutive failures and temporarily disables execution when a threshold is exceeded.
State transitions:
Closed→Open: whenconsecutive_failures >= failure_thresholdOpen→HalfOpen: afteropen_duration_mshas elapsedHalfOpen→Closed: on successHalfOpen→Open: on failure (resets timer)
Designed to be held via Arc<CircuitBreaker> and referenced via
Weak<CircuitBreaker> from executors, so the breaker can be dropped
independently without leaking memory.
Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(failure_threshold: usize, open_duration_ms: u64) -> Self
pub fn new(failure_threshold: usize, open_duration_ms: u64) -> Self
Create a new circuit breaker.
failure_threshold: number of consecutive failures before opening.open_duration_ms: how long (in milliseconds) to stay open before transitioning to half-open.
Sourcepub fn record_success(&self)
pub fn record_success(&self)
Record a successful operation.
Resets the consecutive failure counter and transitions to Closed.
Sourcepub fn record_failure(&self)
pub fn record_failure(&self)
Record a failed operation.
Increments the failure counter. If the threshold is reached,
transitions to Open and records the timestamp.
Sourcepub fn is_available(&self) -> bool
pub fn is_available(&self) -> bool
Check whether the circuit breaker allows execution.
Returns true if the circuit is Closed or has transitioned from
Open to HalfOpen (enough time has passed). Returns false if
the circuit is still Open.
When returning true in the HalfOpen state, the caller should
proceed with a single trial request and call record_success or
record_failure accordingly.
Sourcepub fn state(&self) -> CircuitState
pub fn state(&self) -> CircuitState
Get the current state.
Sourcepub fn consecutive_failures(&self) -> usize
pub fn consecutive_failures(&self) -> usize
Get the current consecutive failure count.