pub struct NetworkCircuitBreaker { /* private fields */ }Expand description
Production-quality circuit breaker for network connections.
§Thread Safety
This type is deliberately not Send + Sync — wrap it in a Mutex or
RwLock if you need shared mutable access from multiple threads or tasks.
§Timestamps
All current_ts parameters are in microseconds on a caller-supplied
monotonic clock. The implementation never calls SystemTime or any other
OS API for time.
Implementations§
Source§impl NetworkCircuitBreaker
impl NetworkCircuitBreaker
Sourcepub fn new(config: CircuitConfig) -> Result<Self, BreakerError>
pub fn new(config: CircuitConfig) -> Result<Self, BreakerError>
Create a new circuit breaker with the given configuration.
Returns a BreakerError::ConfigurationError if the configuration is
logically inconsistent.
Sourcepub fn call(
&mut self,
current_ts: u64,
) -> Result<CircuitCallGuard, BreakerError>
pub fn call( &mut self, current_ts: u64, ) -> Result<CircuitCallGuard, BreakerError>
Try to acquire a call slot.
- Closed → always succeeds.
- Open → fails with
BreakerError::CircuitOpenunlesscurrent_ts >= opened_at + retry_after_us, in which case the circuit automatically transitions to HalfOpen. - HalfOpen → succeeds if
probe_count < half_open_probes, otherwise fails withBreakerError::MaxProbesExceeded.
Sourcepub fn record_outcome(
&mut self,
outcome: CircuitOutcome,
response_time_us: u64,
current_ts: u64,
) -> Option<CircuitEvent>
pub fn record_outcome( &mut self, outcome: CircuitOutcome, response_time_us: u64, current_ts: u64, ) -> Option<CircuitEvent>
Record the outcome of a permitted call and advance the state machine.
Returns a CircuitEvent if a notable state transition occurred.
response_time_us is the elapsed time in microseconds; it is used only
for metrics and does not affect the state machine directly.
Sourcepub fn force_open(&mut self, current_ts: u64)
pub fn force_open(&mut self, current_ts: u64)
Force the circuit into the Open state regardless of current state.
Sourcepub fn force_close(&mut self)
pub fn force_close(&mut self)
Force the circuit into the Closed state regardless of current state.
Sourcepub fn state(&self) -> &CircuitState
pub fn state(&self) -> &CircuitState
Return a reference to the current circuit state.
Sourcepub fn reset_metrics(&mut self)
pub fn reset_metrics(&mut self)
Reset all accumulated metrics (success/failure/rejection counters and response-time average). The state machine and event history are unaffected.
Sourcepub fn metrics(&self, _current_ts: u64) -> CircuitMetrics
pub fn metrics(&self, _current_ts: u64) -> CircuitMetrics
Return a point-in-time metrics snapshot.
Sourcepub fn event_history(&self) -> Vec<CircuitEvent>
pub fn event_history(&self) -> Vec<CircuitEvent>
Return the last up-to-50 circuit events.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for NetworkCircuitBreaker
impl RefUnwindSafe for NetworkCircuitBreaker
impl Send for NetworkCircuitBreaker
impl Sync for NetworkCircuitBreaker
impl Unpin for NetworkCircuitBreaker
impl UnsafeUnpin for NetworkCircuitBreaker
impl UnwindSafe for NetworkCircuitBreaker
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more