pub struct CircuitBreaker { /* private fields */ }Expand description
Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(failure_threshold: u32, recovery_secs: u64) -> Self
pub fn new(failure_threshold: u32, recovery_secs: u64) -> Self
Create a new circuit breaker.
failure_threshold— how many consecutive failures open the circuit.recovery_secs— how long the circuit stays Open before testing again.
Defaults to letting exactly one probe through while HalfOpen — see
CircuitBreaker::max_half_open_probes to change that.
Sourcepub fn max_half_open_probes(self, n: u32) -> Self
pub fn max_half_open_probes(self, n: u32) -> Self
Override how many concurrent probe requests are let through while a
backend is HalfOpen (default: 1). Chainable — call before the
breaker is put behind a shared Mutex/Arc.
Sourcepub fn is_available(&mut self, backend: &str) -> bool
pub fn is_available(&mut self, backend: &str) -> bool
Returns true if a request should be forwarded to backend.
Transitions Open → HalfOpen when the recovery window has elapsed.
While HalfOpen, at most max_half_open_probes concurrent calls
return true — further calls are rejected like Open until the
in-flight probe(s) resolve via record_success/record_failure.
Sourcepub fn record_success(&mut self, backend: &str)
pub fn record_success(&mut self, backend: &str)
Record a successful response for backend.
Transitions HalfOpen → Closed and resets the failure counter and
the in-flight half-open probe count.
Sourcepub fn record_failure(&mut self, backend: &str)
pub fn record_failure(&mut self, backend: &str)
Record a failed response for backend.
In Closed state, increments the counter and opens the circuit when
failure_threshold is reached. In HalfOpen state, immediately
re-opens the circuit and resets the recovery timer.
Sourcepub fn state(&self, backend: &str) -> BreakerState
pub fn state(&self, backend: &str) -> BreakerState
Return the current state for backend (defaults to Closed if unseen).
Sourcepub fn all_states(&self) -> Vec<(String, BreakerState)>
pub fn all_states(&self) -> Vec<(String, BreakerState)>
Snapshot of every backend this breaker has ever seen, with its
current state. Powers the rws_circuit_breaker_state{backend}
metric (crate::metrics::prometheus_text) — there is no
RedisCircuitBreaker equivalent, since enumerating keys isn’t
something the minimal hand-rolled RESP client supports (no
SCAN/KEYS array-reply decoding).