Skip to main content

CircuitBreaker

Struct CircuitBreaker 

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

Circuit breaker implementation to prevent cascading failures

The circuit breaker tracks failures and “trips” after a threshold is reached, preventing further calls and allowing the system to recover.

Implementations§

Source§

impl CircuitBreaker

Source

pub fn new(name: impl Into<String>) -> Self

Create a new circuit breaker with the given name and default configuration

Source

pub fn with_config( name: impl Into<String>, config: CircuitBreakerConfig, ) -> Self

Create a new circuit breaker with custom configuration

Source

pub fn state(&self) -> CircuitState

Get the current state of the circuit breaker.

An open circuit whose reset timeout has elapsed reports CircuitState::HalfOpen: the next execute call is admitted as the probe. Reading the state does not change it.

A half-open probe that has run longer than the probe timeout (see CircuitBreakerConfig::probe_timeout_ms) counts as failed, so the state reads CircuitState::Open again until the next reset timeout elapses.

Source

pub fn is_probing(&self) -> bool

Whether a half-open probe is running right now, so every other call fails fast until it returns or times out.

state reports CircuitState::HalfOpen both while the probe runs and when the next call will be admitted as the probe; this tells the two apart.

§Example
use error_forge::recovery::CircuitBreaker;

let breaker = CircuitBreaker::new("db");
assert!(!breaker.is_probing());
Source

pub fn name(&self) -> &str

Get the name of the circuit breaker

Source

pub fn execute<F, T, E>(&self, f: F) -> RecoveryResult<T>
where F: FnOnce() -> Result<T, E>, E: Error + Send + Sync + 'static,

Execute a function protected by the circuit breaker.

While the circuit is open the call fails fast with CircuitOpenError. Once the reset timeout elapses exactly one call is admitted as a half-open probe; concurrent calls fail fast until it resolves. A successful probe closes the circuit, and a failed or panicking probe reopens it.

Only the probe decides how the circuit leaves half-open. A call that was admitted while the circuit was still closed and finishes after it tripped does not affect the new state. A probe that runs longer than the probe timeout counts as failed: the circuit reopens, and the probe’s own result, when it comes, is ignored.

The closure runs on the calling thread and the breaker cannot interrupt it, so give it its own I/O timeouts. A panic in the closure counts as a failure.

The closure’s error is boxed into RecoveryResult; downcast it to tell a fail-fast rejection from the closure’s own error.

§Example
use error_forge::recovery::{CircuitBreaker, CircuitOpenError};

let breaker = CircuitBreaker::new("inventory-service");

let value = breaker.execute(|| Ok::<u32, std::io::Error>(42));
assert_eq!(value.unwrap(), 42);

let failed = breaker.execute(|| Err::<u32, _>(std::io::Error::other("down")));
let error = failed.unwrap_err();
assert!(error.downcast_ref::<CircuitOpenError>().is_none());
assert!(error.downcast_ref::<std::io::Error>().is_some());
Source

pub fn reset(&self)

Manually reset the circuit breaker to closed state

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> 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, 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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
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