use std::time::{Duration, Instant};
use tower_resilience_core::ResilienceEvent;
#[derive(Debug, Clone)]
pub enum TimeLimiterEvent {
Success {
pattern_name: String,
timestamp: Instant,
duration: Duration,
},
Error {
pattern_name: String,
timestamp: Instant,
duration: Duration,
},
Timeout {
pattern_name: String,
timestamp: Instant,
timeout_duration: Duration,
},
}
impl ResilienceEvent for TimeLimiterEvent {
fn event_type(&self) -> &'static str {
match self {
TimeLimiterEvent::Success { .. } => "success",
TimeLimiterEvent::Error { .. } => "error",
TimeLimiterEvent::Timeout { .. } => "timeout",
}
}
fn timestamp(&self) -> Instant {
match self {
TimeLimiterEvent::Success { timestamp, .. }
| TimeLimiterEvent::Error { timestamp, .. }
| TimeLimiterEvent::Timeout { timestamp, .. } => *timestamp,
}
}
fn pattern_name(&self) -> &str {
match self {
TimeLimiterEvent::Success { pattern_name, .. }
| TimeLimiterEvent::Error { pattern_name, .. }
| TimeLimiterEvent::Timeout { pattern_name, .. } => pattern_name,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_event_types() {
let now = Instant::now();
let success = TimeLimiterEvent::Success {
pattern_name: "test".to_string(),
timestamp: now,
duration: Duration::from_millis(100),
};
assert_eq!(success.event_type(), "success");
assert_eq!(success.pattern_name(), "test");
let error = TimeLimiterEvent::Error {
pattern_name: "test".to_string(),
timestamp: now,
duration: Duration::from_millis(50),
};
assert_eq!(error.event_type(), "error");
let timeout = TimeLimiterEvent::Timeout {
pattern_name: "test".to_string(),
timestamp: now,
timeout_duration: Duration::from_secs(5),
};
assert_eq!(timeout.event_type(), "timeout");
}
}