Skip to main content

CircuitStats

Struct CircuitStats 

Source
pub struct CircuitStats { /* private fields */ }
Expand description

Circuit breaker statistics for tracking state transitions.

Maintains a rolling window of successes and failures to determine when the circuit should open, close, or enter half-open state.

Implementations§

Source§

impl CircuitStats

Source

pub fn new() -> Self

Create new circuit stats in the closed state.

Source

pub fn state(&self) -> CircuitState

Get the current circuit state.

Source

pub fn opened_at(&self) -> Option<u64>

Get the timestamp when the circuit opened (if open).

Source

pub fn last_state_change(&self) -> u64

Get the timestamp of the last state change.

Source

pub fn consecutive_failures(&self) -> u32

Get the consecutive failure count.

Source

pub fn error_rate(&self) -> f64

Get the error rate in the current window (0.0-1.0).

Source

pub fn record_success(&mut self)

Record a successful operation.

In closed state: resets consecutive failures, increments window successes. In half-open state: increments consecutive successes.

Source

pub fn record_failure(&mut self)

Record a failed operation.

Increments consecutive failures and window failures. In half-open state, resets consecutive successes.

Source

pub fn should_open(&self, config: &CircuitBreakerConfig) -> bool

Check if the circuit should open based on config thresholds.

Returns true if:

  • Consecutive failures exceed threshold, OR
  • Error rate exceeds threshold (with minimum sample size)
Source

pub fn should_half_open(&self, config: &CircuitBreakerConfig) -> bool

Check if the circuit should transition to half-open.

Returns true if the circuit is open and the cooldown period has elapsed.

Source

pub fn should_close(&self, config: &CircuitBreakerConfig) -> bool

Check if the circuit should close.

Returns true if in half-open state and consecutive successes meet or exceed the success threshold.

Source

pub fn can_probe(&self, config: &CircuitBreakerConfig) -> bool

Check if a probe request can be made in half-open state.

Returns true if active probes are below the maximum allowed.

Source

pub fn start_probe(&mut self, config: &CircuitBreakerConfig) -> bool

Start a probe request in half-open state.

Returns true if the probe was started, false if already at max probes.

Source

pub fn open(&mut self)

Transition the circuit to open state.

Source

pub fn half_open(&mut self)

Transition the circuit to half-open state.

Source

pub fn close(&mut self)

Transition the circuit to closed state.

Source

pub fn reset_window(&mut self)

Reset the rolling window counters.

Called periodically to ensure the window reflects recent activity.

Source

pub fn recent_results(&self) -> &[bool]

Get recent health check results for history visualization.

Returns a slice of recent results (true=success, false=failure), with the most recent result at the end.

Source

pub fn recovery_remaining_secs( &self, config: &CircuitBreakerConfig, ) -> Option<u64>

Calculate seconds remaining until circuit auto-transitions to half-open.

Returns None if circuit is not open or cooldown has already elapsed.

Trait Implementations§

Source§

impl Clone for CircuitStats

Source§

fn clone(&self) -> CircuitStats

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CircuitStats

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CircuitStats

Source§

fn default() -> CircuitStats

Returns the “default value” for a type. Read more

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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