pub struct ToolCircuitBreaker { /* private fields */ }Expand description
Per-tool circuit breaker.
When a tool fails failure_threshold times consecutively the breaker
opens. After recovery_duration it transitions to HalfOpen and allows
one probe call. If the probe succeeds the breaker closes; if it fails
the breaker reopens.
All mutable state is behind a single Mutex, making every method’s
read-modify-write atomic with respect to every other method — no
window for a counter/state race between concurrent record_success
and record_failure calls.
§Example
use loopctl::tool::health::ToolCircuitBreaker;
use std::time::Duration;
let breaker = ToolCircuitBreaker::new(Duration::from_millis(100), 2);
// Initially closed — requests are allowed
assert!(breaker.allow_request());
// Record failures until it opens
breaker.record_failure();
assert!(breaker.allow_request()); // 1 failure < threshold 2
breaker.record_failure();
// Now open — requests blocked
assert!(!breaker.allow_request());Implementations§
Source§impl ToolCircuitBreaker
impl ToolCircuitBreaker
Sourcepub fn new(recovery_duration: Duration, failure_threshold: u64) -> Self
pub fn new(recovery_duration: Duration, failure_threshold: u64) -> Self
Create a new circuit breaker with the given recovery duration and failure threshold.
The breaker starts in the Closed state.
Sourcepub fn from_config(config: &CircuitBreakerConfig) -> Self
pub fn from_config(config: &CircuitBreakerConfig) -> Self
Create a circuit breaker from a CircuitBreakerConfig.
Convenience constructor that unpacks the threshold and recovery
duration from a config struct, delegating to new.
Useful when many breakers share a single config.
Sourcepub fn allow_request(&self) -> bool
pub fn allow_request(&self) -> bool
Whether a request is allowed to proceed.
- Closed: always allowed.
- Open: allowed only if
recovery_durationhas elapsed since the last failure, in which case the breaker transitions toHalfOpenand the caller becomes the sole probe. HalfOpen: already probing — no additional probes allowed (returnsfalseto prevent thundering-herd).
Sourcepub fn would_allow_request(&self) -> bool
pub fn would_allow_request(&self) -> bool
Whether a request would be allowed, without the Open→HalfOpen side effect.
Pure read mirroring allow_request’s decision
logic: returns true for Closed, false for HalfOpen, and for
Open returns true only if the recovery duration has elapsed (i.e.
the next allow_request call would transition
to HalfOpen and grant the probe). Crucially, this performs no
state transition — use it for availability checks
(is_tool_available) so a
read does not consume the single HalfOpen probe slot that belongs to
the real dispatch path.
Sourcepub fn would_be_half_open(&self) -> bool
pub fn would_be_half_open(&self) -> bool
Whether the next allow_request call would
transition an Open breaker into HalfOpen.
Pure read: true only when the breaker is Open and the recovery
duration has elapsed — i.e. the next allow_request would perform the
Open→HalfOpen transition and grant the probe slot. Returns false
for HalfOpen (a probe is already in flight; the next
allow_request refuses to avoid a thundering herd) and for Closed
(requests are allowed unconditionally, no transition pending).
Complements would_allow_request.
Sourcepub fn record_success(&self)
pub fn record_success(&self)
Record a successful call.
Resets consecutive failures to zero and transitions the breaker to Closed.
Sourcepub fn record_failure(&self)
pub fn record_failure(&self)
Record a failed call.
Increments the consecutive-failure counter. If the count reaches
failure_threshold, the breaker transitions to Open. In the
HalfOpen state, a single failure reopens the breaker.
Sourcepub fn state_label(&self) -> &'static str
pub fn state_label(&self) -> &'static str
Current state of the breaker as a human-readable string.
Returns "closed", "open", or "half-open". Intended for
logs and metrics where a string label is preferable to the
numeric encoding.
Sourcepub fn consecutive_failures(&self) -> u64
pub fn consecutive_failures(&self) -> u64
Number of consecutive failures recorded since the last success.
Reset to zero on every success; reaching failure_threshold
trips the breaker.
Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Whether the breaker is currently in the Closed (healthy) state.
true when requests are allowed unconditionally.
Sourcepub fn is_open(&self) -> bool
pub fn is_open(&self) -> bool
Whether the breaker is currently in the Open (blocking) state.
true when requests are refused outright (subject to the
recovery-duration transition handled inside
allow_request).
Sourcepub fn is_half_open(&self) -> bool
pub fn is_half_open(&self) -> bool
Whether the breaker is currently in the HalfOpen (probing)
state.
true when a single probe call is in flight and additional
probes are refused to avoid a thundering herd.