use std::time::Duration;
#[derive(Debug, Clone)]
pub struct DistributedConfig {
pub retry_count: u32,
pub retry_base_delay: Duration,
pub circuit_breaker_threshold: u32,
pub circuit_breaker_reset_timeout: Duration,
pub health_check_interval: Duration,
}
impl Default for DistributedConfig {
fn default() -> Self {
Self {
retry_count: 3,
retry_base_delay: Duration::from_millis(100),
circuit_breaker_threshold: 5,
circuit_breaker_reset_timeout: Duration::from_secs(30),
health_check_interval: Duration::from_secs(60),
}
}
}
impl DistributedConfig {
pub fn builder() -> DistributedConfigBuilder {
DistributedConfigBuilder::default()
}
}
#[derive(Debug, Default)]
pub struct DistributedConfigBuilder {
config: DistributedConfig,
}
impl DistributedConfigBuilder {
pub fn retry_count(mut self, count: u32) -> Self {
self.config.retry_count = count;
self
}
pub fn retry_base_delay(mut self, delay: Duration) -> Self {
self.config.retry_base_delay = delay;
self
}
pub fn circuit_breaker_threshold(mut self, threshold: u32) -> Self {
self.config.circuit_breaker_threshold = threshold;
self
}
pub fn circuit_breaker_reset_timeout(mut self, timeout: Duration) -> Self {
self.config.circuit_breaker_reset_timeout = timeout;
self
}
pub fn health_check_interval(mut self, interval: Duration) -> Self {
self.config.health_check_interval = interval;
self
}
pub fn build(self) -> DistributedConfig {
self.config
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_values() {
let config = DistributedConfig::default();
assert_eq!(config.retry_count, 3);
assert_eq!(config.retry_base_delay, Duration::from_millis(100));
assert_eq!(config.circuit_breaker_threshold, 5);
assert_eq!(config.circuit_breaker_reset_timeout, Duration::from_secs(30));
assert_eq!(config.health_check_interval, Duration::from_secs(60));
}
#[test]
fn test_builder_chain() {
let config = DistributedConfig::builder()
.retry_count(5)
.retry_base_delay(Duration::from_millis(200))
.circuit_breaker_threshold(10)
.circuit_breaker_reset_timeout(Duration::from_secs(60))
.health_check_interval(Duration::from_secs(120))
.build();
assert_eq!(config.retry_count, 5);
assert_eq!(config.retry_base_delay, Duration::from_millis(200));
assert_eq!(config.circuit_breaker_threshold, 10);
assert_eq!(config.circuit_breaker_reset_timeout, Duration::from_secs(60));
assert_eq!(config.health_check_interval, Duration::from_secs(120));
}
#[test]
fn test_builder_partial_override() {
let config = DistributedConfig::builder().retry_count(0).build();
assert_eq!(config.retry_count, 0);
assert_eq!(config.retry_base_delay, Duration::from_millis(100));
assert_eq!(config.circuit_breaker_threshold, 5);
}
#[test]
fn test_clone_and_debug() {
let config = DistributedConfig::default();
let cloned = config.clone();
assert_eq!(cloned.retry_count, config.retry_count);
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("DistributedConfig"));
assert!(debug_str.contains("retry_count"));
}
}