Skip to main content

CircuitBreaker

Struct CircuitBreaker 

Source
pub struct CircuitBreaker<L, C = SystemClock>
where C: Clock,
{ /* private fields */ }
Available on crate feature circuit-breaker only.
Expand description

A circuit breaker wrapping a limiter L, timed by clock C.

Construct one with CircuitBreaker::builder. Build requires the circuit-breaker feature.

§Examples

use std::time::Duration;
use throttle_net::{CircuitBreaker, Throttle, Trip};

let breaker = CircuitBreaker::builder()
    .trip(Trip::Consecutive(5))
    .cooldown(Duration::from_secs(10))
    .build(Throttle::per_second(100));

match breaker.acquire().await {
    Ok(permit) => {
        // ... call the downstream ...
        let ok = true;
        if ok { permit.success() } else { permit.failure() }
    }
    Err(_shed) => {
        // breaker open (or limiter exhausted): fail fast
    }
}

Implementations§

Source§

impl CircuitBreaker<Infallible>

Source

pub fn builder() -> CircuitBreakerBuilder

Starts building a breaker. Defaults: Trip::Consecutive(5), a 30-second cooldown, and a single trial that must succeed to close.

Source§

impl<L, C> CircuitBreaker<L, C>
where L: Limiter, C: Clock + Clone,

Source

pub fn with_clock<C2>(self, clock: C2) -> CircuitBreaker<L, C2>
where C2: Clock + Clone,

Replaces the time source (the cooldown clock), for deterministic tests. The breaker is reset to closed around the new clock.

Source

pub fn state(&self) -> BreakerState

The current state (a momentary snapshot).

Source

pub fn inner(&self) -> &L

A shared reference to the wrapped limiter.

Source

pub fn record_success(&self)

Reports a successful protected call. Prefer settling a Permit.

Source

pub fn record_failure(&self)

Reports a failed protected call. Prefer settling a Permit.

Source

pub fn try_acquire(&self) -> Result<Option<Permit<'_, L, C>>, ThrottleError>

Attempts to admit a request without waiting, returning a Permit on success.

§Errors

Returns Ok(None) when the breaker would admit but the wrapped limiter has no token available right now (normal rate-limiting, not a breaker fault).

Source§

impl<L, C> CircuitBreaker<L, C>
where L: Limiter, C: Clock + Clone,

Source

pub async fn acquire(&self) -> Result<Permit<'_, L, C>, ThrottleError>

Available on crate feature runtime only.

Admits a request, failing fast if the breaker is open and otherwise pacing on the wrapped limiter until a token is free.

A circuit-open condition returns immediately (load shedding); a plain rate-limit waits. Returns a Permit to settle with the call’s outcome.

§Errors

Auto Trait Implementations§

§

impl<L, C = SystemClock> !Freeze for CircuitBreaker<L, C>

§

impl<L, C> RefUnwindSafe for CircuitBreaker<L, C>

§

impl<L, C> Send for CircuitBreaker<L, C>
where L: Send,

§

impl<L, C> Sync for CircuitBreaker<L, C>
where L: Sync,

§

impl<L, C> Unpin for CircuitBreaker<L, C>
where L: Unpin, C: Unpin,

§

impl<L, C> UnsafeUnpin for CircuitBreaker<L, C>
where L: UnsafeUnpin, C: UnsafeUnpin,

§

impl<L, C> UnwindSafe for CircuitBreaker<L, C>
where L: UnwindSafe, C: UnwindSafe,

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