Skip to main content

ToolCircuitBreaker

Struct ToolCircuitBreaker 

Source
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

Source

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.

Source

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.

Source

pub fn allow_request(&self) -> bool

Whether a request is allowed to proceed.

  • Closed: always allowed.
  • Open: allowed only if recovery_duration has elapsed since the last failure, in which case the breaker transitions to HalfOpen and the caller becomes the sole probe.
  • HalfOpen: already probing — no additional probes allowed (returns false to prevent thundering-herd).
Source

pub fn would_allow_request(&self) -> bool

Whether a request would be allowed, without the OpenHalfOpen 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.

Source

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 OpenHalfOpen 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.

Source

pub fn record_success(&self)

Record a successful call.

Resets consecutive failures to zero and transitions the breaker to Closed.

Source

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.

Source

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.

Source

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.

Source

pub fn is_closed(&self) -> bool

Whether the breaker is currently in the Closed (healthy) state.

true when requests are allowed unconditionally.

Source

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).

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more