#[non_exhaustive]pub struct CircuitBreakerConfig {
pub failure_threshold: usize,
pub failure_window_ms: u64,
pub reset_timeout_ms: u64,
pub probe_timeout_ms: Option<u64>,
}Expand description
Configuration for a circuit breaker.
Marked #[non_exhaustive] so future minor releases can add new
tuning knobs without breaking callers. Construct via
CircuitBreakerConfig::default then mutate the fields you
care about.
The breaker counts failures, not a failure rate: it opens once
failure_threshold calls have failed within the last
failure_window_ms, however many calls succeeded in between. Every
Err returned by the protected closure counts as a failure.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.failure_threshold: usizeNumber of failures required to open the circuit
failure_window_ms: u64Time window in milliseconds to count failures
reset_timeout_ms: u64Time in milliseconds that the circuit stays open before trying again
probe_timeout_ms: Option<u64>Longest time in milliseconds a half-open probe may run before it
counts as failed. None (the default) uses reset_timeout_ms.
While the probe runs every other call fails fast, so a probe that
never returns (a dependency that accepts connections but never
answers) would otherwise keep the circuit failing until
CircuitBreaker::reset. Once the probe is older than this, the
circuit reopens as if the probe had failed, and a new probe is
admitted after the next reset timeout. The late result of the old
probe is then ignored. Closures should still set their own I/O
timeouts: the breaker cannot interrupt a call that hangs.
Implementations§
Source§impl CircuitBreakerConfig
impl CircuitBreakerConfig
Sourcepub fn new(
failure_threshold: usize,
failure_window_ms: u64,
reset_timeout_ms: u64,
) -> Self
pub fn new( failure_threshold: usize, failure_window_ms: u64, reset_timeout_ms: u64, ) -> Self
Construct a CircuitBreakerConfig from its three core knobs.
Provided so external callers (tests, custom circuit-breaker
wiring) can build the struct without depending on its field
list, which may grow over the 1.x line. For tuning only a
subset, start from Self::default and use the
with_* builder methods.
Sourcepub fn with_failure_threshold(self, threshold: usize) -> Self
pub fn with_failure_threshold(self, threshold: usize) -> Self
Override the failure-count threshold.
Sourcepub fn with_failure_window_ms(self, window_ms: u64) -> Self
pub fn with_failure_window_ms(self, window_ms: u64) -> Self
Override the failure-counting window in milliseconds.
Sourcepub fn with_reset_timeout_ms(self, reset_ms: u64) -> Self
pub fn with_reset_timeout_ms(self, reset_ms: u64) -> Self
Override the reset-timeout in milliseconds.
Sourcepub fn with_probe_timeout_ms(self, probe_ms: u64) -> Self
pub fn with_probe_timeout_ms(self, probe_ms: u64) -> Self
Override the longest time a half-open probe may run before it
counts as failed (see Self::probe_timeout_ms).
§Example
use error_forge::recovery::CircuitBreakerConfig;
let config = CircuitBreakerConfig::default().with_probe_timeout_ms(2_000);
assert_eq!(config.probe_timeout_ms, Some(2_000));