use std::time::Duration;
use crate::breaker::HalfOpenMode;
use crate::breaker::constants::{DEFAULT_BREAK_DURATION, DEFAULT_FAILURE_THRESHOLD, DEFAULT_MIN_THROUGHPUT, DEFAULT_SAMPLING_DURATION};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(any(feature = "serde", test), derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct BreakerConfig {
pub enabled: bool,
pub failure_threshold: f32,
pub min_throughput: u32,
#[cfg_attr(
any(feature = "serde", test),
serde(with = "jiff::fmt::serde::unsigned_duration::friendly::compact::required")
)]
pub sampling_duration: Duration,
#[cfg_attr(
any(feature = "serde", test),
serde(with = "jiff::fmt::serde::unsigned_duration::friendly::compact::required")
)]
pub break_duration: Duration,
pub half_open_mode: HalfOpenMode,
}
impl Default for BreakerConfig {
fn default() -> Self {
Self {
enabled: true,
failure_threshold: DEFAULT_FAILURE_THRESHOLD,
min_throughput: DEFAULT_MIN_THROUGHPUT,
sampling_duration: DEFAULT_SAMPLING_DURATION,
break_duration: DEFAULT_BREAK_DURATION,
half_open_mode: HalfOpenMode::progressive(None),
}
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
#[cfg_attr(miri, ignore)]
#[test]
fn default_snapshot() {
let config = BreakerConfig::default();
insta::assert_json_snapshot!(config);
}
#[cfg_attr(miri, ignore)]
#[test]
fn quick_half_open_mode_snapshot() {
let config = BreakerConfig {
half_open_mode: HalfOpenMode::quick(),
..BreakerConfig::default()
};
insta::assert_json_snapshot!(config);
}
#[cfg_attr(miri, ignore)]
#[test]
fn progressive_no_duration_half_open_mode_snapshot() {
let config = BreakerConfig {
half_open_mode: HalfOpenMode::progressive(None),
..BreakerConfig::default()
};
insta::assert_json_snapshot!(config);
}
#[cfg_attr(miri, ignore)]
#[test]
fn progressive_with_duration_half_open_mode_snapshot() {
let config = BreakerConfig {
half_open_mode: HalfOpenMode::progressive(Duration::from_secs(45)),
..BreakerConfig::default()
};
insta::assert_json_snapshot!(config);
}
}