use parking_lot::Mutex;
use std::time::{Duration, Instant};
#[derive(Debug)]
pub struct ActionTracker {
actions: Mutex<Vec<Instant>>,
window_secs: u64,
}
impl ActionTracker {
pub fn new() -> Self {
Self {
actions: Mutex::new(Vec::new()),
window_secs: 3600,
}
}
pub fn with_window(window_secs: u64) -> Self {
Self {
actions: Mutex::new(Vec::new()),
window_secs,
}
}
pub fn record(&self) -> usize {
let mut actions = self.actions.lock();
self.cleanup(&mut actions);
actions.push(Instant::now());
actions.len()
}
pub fn count(&self) -> usize {
let mut actions = self.actions.lock();
self.cleanup(&mut actions);
actions.len()
}
pub fn is_rate_limited(&self, max_actions: u32) -> bool {
self.count() >= max_actions as usize
}
pub fn try_record(&self, max_actions: u32) -> bool {
let mut actions = self.actions.lock();
self.cleanup(&mut actions);
if actions.len() >= max_actions as usize {
false
} else {
actions.push(Instant::now());
true
}
}
fn cleanup(&self, actions: &mut Vec<Instant>) {
let Some(cutoff) = Instant::now().checked_sub(Duration::from_secs(self.window_secs)) else {
return;
};
actions.retain(|t| *t > cutoff);
}
pub fn window_duration(&self) -> Duration {
Duration::from_secs(self.window_secs)
}
pub fn reset(&self) {
let mut actions = self.actions.lock();
actions.clear();
}
}
impl Default for ActionTracker {
fn default() -> Self {
Self::new()
}
}
impl Clone for ActionTracker {
fn clone(&self) -> Self {
let actions = self.actions.lock();
Self {
actions: Mutex::new(actions.clone()),
window_secs: self.window_secs,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn test_action_tracker_basic() {
let tracker = ActionTracker::with_window(1);
assert_eq!(tracker.record(), 1);
assert_eq!(tracker.record(), 2);
assert_eq!(tracker.record(), 3);
assert_eq!(tracker.count(), 3);
thread::sleep(Duration::from_secs(2));
assert_eq!(tracker.count(), 0);
}
#[test]
fn test_rate_limiting() {
let tracker = ActionTracker::with_window(3600);
for i in 0..5 {
assert!(
!tracker.is_rate_limited(5),
"Should not be rate limited at action {}",
i
);
tracker.record();
}
assert!(
tracker.is_rate_limited(5),
"Should be rate limited after 5 actions with limit of 5"
);
assert_eq!(tracker.count(), 5, "Count should be 5");
assert!(
!tracker.try_record(5),
"Should not be able to record when rate limited"
);
assert!(
tracker.try_record(6),
"Should be able to record when limit is 6"
);
}
#[test]
fn test_clone() {
let tracker = ActionTracker::with_window(3600);
tracker.record();
tracker.record();
assert_eq!(tracker.count(), 2, "Original tracker should have 2 actions");
let cloned = tracker.clone();
assert_eq!(cloned.count(), 2, "Cloned tracker should have 2 actions");
cloned.record();
assert_eq!(
cloned.count(),
3,
"Cloned tracker should have 3 actions after record"
);
assert_eq!(
tracker.count(),
2,
"Original tracker should still have 2 actions"
);
}
}