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 consecutive_command_failures(&self) -> u32

Consecutive command-class failures observed since the last command-class success (or circuit close).

Source

pub fn reopen_streak(&self) -> u32

Open transitions since the circuit last closed (0 when it has not opened since recovery). Exposed for diagnostics.

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

Computed over the bounded recent-results window (the last CIRCUIT_HISTORY_SIZE health checks) rather than the unbounded lifetime window_successes/window_failures counters. A long-lived daemon polling distant workers accumulates occasional transient failures over days; with a lifetime ratio a currently-healthy worker could stay gated (circuit kept open, depressed health score) until the daemon was restarted. A recency window lets the rate self-heal as fresh successes age out old failures.

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, consecutive successes meet or exceed the success threshold, AND no command-class failure is outstanding. A worker whose last real command timed out is not recovered no matter how many liveness probes it answers; it stays HalfOpen (degraded, probe-limited) until a command-class success clears the evidence.

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. Also clears the recent-results history that error_rate() is computed from, so a reset truly zeroes the observed error rate.

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.

Source

pub fn apply_health_outcome( &mut self, healthy: bool, transient: bool, config: &CircuitBreakerConfig, ) -> CircuitState

Drive the circuit through a single health-check outcome and return the resulting CircuitState.

This is the single, self-contained transition engine shared by every circuit consumer (the daemon health monitor via WorkerState::record_health_check, and the diagnostic WorkerHealth panel via WorkerHealth::observe). Keeping the transition logic in ONE place is what makes WorkerState.circuit (which selection reads) and the diagnostic WorkerHealth.circuit agree.

Behaviour:

  • On a healthy outcome: if the circuit is Open and its cooldown has elapsed it first transitions Open → HalfOpen before crediting the success, so recovery needs exactly success_threshold clean probes (crediting a success against an Open circuit would be lost). Then the success is recorded and the circuit closes once the threshold is met.
  • On an unhealthy outcome: the failure is recorded, the circuit opens if the closed-state open condition is met, and a HalfOpen probe failure reopens the circuit — UNLESS the failure was transient (a retryable transport blip that survived the in-check retries). A transient half-open failure neither reopens nor resets the cooldown, so a single network hiccup during recovery cannot bounce a worker back to Open.
  • After either branch, an Open circuit past its cooldown is nudged to HalfOpen so probing resumes on the next poll even for a still-failing worker.
Source

pub fn apply_command_outcome( &mut self, success: bool, config: &CircuitBreakerConfig, ) -> CircuitState

Drive the circuit through a single command-class outcome and return the resulting CircuitState.

Command-class evidence comes from an operation of the same shape as dispatched work: a remote binary that must run to completion (the telemetry poll, or a health probe whose SSH session connected but whose command never finished). It is the signal the plain liveness probe cannot provide — a host under fork exhaustion or swap thrash keeps answering echo over a warm connection while every real command hangs, and on that evidence alone the breaker would happily re-admit it.

Behaviour:

  • Success: clears the outstanding command-failure count and then counts as an ordinary healthy outcome (may advance Open -> HalfOpen -> Closed exactly like a liveness success).
  • Failure: increments the command-failure count and records a plain failure. The circuit opens when either the ordinary open condition or command_failure_threshold is met. In HalfOpen a command failure ALWAYS reopens — there is no transient exemption, because “connected but the command did not complete” is precisely the condition the half-open trial exists to detect. While any command failure is outstanding, Self::should_close refuses to close on liveness successes alone.

Trait Implementations§

Source§

impl Clone for CircuitStats

Source§

fn clone(&self) -> Self

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() -> Self

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 = !

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

fn try_from(value: U) -> Result<T, !>

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