pub struct CircuitBreaker<L, C = SystemClock>where
C: Clock,{ /* private fields */ }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>
impl CircuitBreaker<Infallible>
Sourcepub fn builder() -> CircuitBreakerBuilder
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>
impl<L, C> CircuitBreaker<L, C>
Sourcepub fn with_clock<C2>(self, clock: C2) -> CircuitBreaker<L, C2>
pub fn with_clock<C2>(self, clock: C2) -> CircuitBreaker<L, C2>
Replaces the time source (the cooldown clock), for deterministic tests. The breaker is reset to closed around the new clock.
Sourcepub fn state(&self) -> BreakerState
pub fn state(&self) -> BreakerState
The current state (a momentary snapshot).
Sourcepub fn record_success(&self)
pub fn record_success(&self)
Reports a successful protected call. Prefer settling a Permit.
Sourcepub fn record_failure(&self)
pub fn record_failure(&self)
Reports a failed protected call. Prefer settling a Permit.
Sourcepub fn try_acquire(&self) -> Result<Option<Permit<'_, L, C>>, ThrottleError>
pub fn try_acquire(&self) -> Result<Option<Permit<'_, L, C>>, ThrottleError>
Attempts to admit a request without waiting, returning a Permit on
success.
§Errors
ThrottleError::CircuitOpenwhen the breaker is open (or its half-open trials are full): the request is shed without touching the wrapped limiter.ThrottleError::CostExceedsCapacitywhen the wrapped limiter can never grant a single unit.
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>
impl<L, C> CircuitBreaker<L, C>
Sourcepub async fn acquire(&self) -> Result<Permit<'_, L, C>, ThrottleError>
Available on crate feature runtime only.
pub async fn acquire(&self) -> Result<Permit<'_, L, C>, ThrottleError>
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
ThrottleError::CircuitOpenwhen the breaker is open or its trials are full — returned without waiting.ThrottleError::CostExceedsCapacitywhen the wrapped limiter can never grant the request.