pub struct CircuitBreaker { /* private fields */ }Expand description
Circuit breaker guarding a fallible operation.
§Guarantees
- Opens after
thresholdconsecutive failures - Transitions to
HalfOpenafterrecovery_windowhas elapsed - Closes on the first successful probe in
HalfOpen
Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(
service: impl Into<String>,
threshold: u32,
recovery_window: Duration,
) -> Result<Self, AgentRuntimeError>
pub fn new( service: impl Into<String>, threshold: u32, recovery_window: Duration, ) -> Result<Self, AgentRuntimeError>
Create a new circuit breaker backed by an in-memory backend.
§Arguments
service— name used in error messages and logsthreshold— consecutive failures before openingrecovery_window— how long to stay open before probing
Sourcepub fn with_backend(self, backend: Arc<dyn CircuitBreakerBackend>) -> Self
pub fn with_backend(self, backend: Arc<dyn CircuitBreakerBackend>) -> Self
Replace the default in-memory backend with a custom one.
Useful for sharing circuit breaker state across processes.
Sourcepub fn call<T, E, F>(&self, f: F) -> Result<T, AgentRuntimeError>
pub fn call<T, E, F>(&self, f: F) -> Result<T, AgentRuntimeError>
Attempt to call f, respecting the circuit breaker state.
§Errors
AgentRuntimeError::CircuitOpen— the breaker is in theOpenstate and the recovery window has not yet elapsedAgentRuntimeError::Orchestration—freturned an error; the error message is theDisplayof the inner error. This call may open the breaker if it pushes the consecutive failure count abovethreshold.
Sourcepub fn state(&self) -> Result<CircuitState, AgentRuntimeError>
pub fn state(&self) -> Result<CircuitState, AgentRuntimeError>
Return the current circuit state.
Sourcepub fn failure_count(&self) -> Result<u32, AgentRuntimeError>
pub fn failure_count(&self) -> Result<u32, AgentRuntimeError>
Return the consecutive failure count.
Sourcepub fn record_success(&self)
pub fn record_success(&self)
Record a successful call, resetting the consecutive failure counter.
Call this when a protected operation succeeds so the circuit can
transition back to Closed after a HalfOpen probe.
Sourcepub fn record_failure(&self)
pub fn record_failure(&self)
Record a failed call, incrementing the consecutive failure counter.
Opens the circuit when the failure count reaches threshold.
Sourcepub fn service_name(&self) -> &str
pub fn service_name(&self) -> &str
Return the service name this circuit breaker is protecting.
Sourcepub fn is_half_open(&self) -> bool
pub fn is_half_open(&self) -> bool
Return true if the circuit is currently HalfOpen (probing).
Sourcepub fn is_healthy(&self) -> bool
pub fn is_healthy(&self) -> bool
Return true if the circuit is in a state that allows calls to proceed.
Calls are allowed in both Closed and HalfOpen states; only Open
fast-fails.
Sourcepub fn threshold(&self) -> u32
pub fn threshold(&self) -> u32
Return the configured consecutive-failure threshold.
The circuit opens when failure_count() reaches this value.
Sourcepub fn failure_headroom(&self) -> u32
pub fn failure_headroom(&self) -> u32
Return how many more failures can be recorded before the circuit opens.
Returns 0 when the circuit is already open or at the threshold.
Useful for alerting logic that needs to know how close the system is
to being cut off.
Sourcepub fn failure_rate(&self) -> f64
pub fn failure_rate(&self) -> f64
Return the current failure count as a ratio of the threshold.
Returns a value in [0.0, 1.0] where 1.0 (or greater) means the
circuit will open (or is already open). Returns 0.0 when the
threshold is zero to avoid division by zero.
Sourcepub fn is_at_threshold(&self) -> bool
pub fn is_at_threshold(&self) -> bool
Return true when failure_count() has reached the configured threshold.
The circuit opens immediately when this returns true on the next
record_failure call.
Sourcepub fn failures_until_open(&self) -> u32
pub fn failures_until_open(&self) -> u32
Return the number of additional failures needed to open the circuit.
Returns 0 when the circuit is already at or beyond threshold.
Sourcepub fn recovery_window(&self) -> Duration
pub fn recovery_window(&self) -> Duration
Return the configured recovery window duration.
After the circuit has been Open for this long, it transitions to
HalfOpen and allows the next call through as a recovery probe.
Sourcepub fn reset(&self)
pub fn reset(&self)
Force the circuit back to Closed state, resetting all failure counters.
Useful for tests and manual operator recovery. Under normal operation
the circuit closes automatically after a successful HalfOpen probe.
Sourcepub fn describe(&self) -> Result<String, AgentRuntimeError>
pub fn describe(&self) -> Result<String, AgentRuntimeError>
Return a human-readable one-line summary of the circuit breaker state.
Format: "service='<name>' state=<State> failures=<n>/<threshold>".
§Errors
Propagates any error returned by state or failure_count.
Sourcepub async fn async_call<T, E, F, Fut>(
&self,
backend: &dyn AsyncCircuitBreakerBackend,
f: F,
) -> Result<T, AgentRuntimeError>
pub async fn async_call<T, E, F, Fut>( &self, backend: &dyn AsyncCircuitBreakerBackend, f: F, ) -> Result<T, AgentRuntimeError>
Execute an async fallible operation under the circuit breaker using an
AsyncCircuitBreakerBackend.
This is the async counterpart of call and is intended for backends
that perform genuine async I/O (e.g. Redis, etcd, distributed stores).
The in-process default can be used via InMemoryCircuitBreakerBackend
which trivially implements AsyncCircuitBreakerBackend.
Trait Implementations§
Source§impl Clone for CircuitBreaker
impl Clone for CircuitBreaker
Source§fn clone(&self) -> CircuitBreaker
fn clone(&self) -> CircuitBreaker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more