use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};
use parking_lot::Mutex;
const FAILURE_THRESHOLD: u32 = 5;
const COOLDOWN_PERIOD: Duration = Duration::from_secs(60);
pub struct CircuitBreaker {
failures: AtomicU32,
last_failure: Mutex<Option<Instant>>,
}
impl CircuitBreaker {
pub fn new() -> Self {
Self {
failures: AtomicU32::new(0),
last_failure: Mutex::new(None),
}
}
pub fn is_open(&self) -> bool {
let count = self.failures.load(Ordering::Relaxed);
if count >= FAILURE_THRESHOLD {
let last = self.last_failure.lock();
if let Some(t) = *last {
if t.elapsed() < COOLDOWN_PERIOD {
return true;
}
}
}
false
}
pub fn report_success(&self) {
self.failures.store(0, Ordering::Relaxed);
}
pub fn report_failure(&self) {
self.failures.fetch_add(1, Ordering::Relaxed);
*self.last_failure.lock() = Some(Instant::now());
}
}