1
0
Fork 0
mirror of https://gitlab.com/hagrid-keyserver/hagrid.git synced 2023-02-13 20:55:02 -05:00
hagrid-keyserver--hagrid/src/rate_limiter.rs

83 lines
2.3 KiB
Rust
Raw Normal View History

2019-05-05 07:29:10 -04:00
use std::collections::HashMap;
2022-02-26 10:54:07 -05:00
use std::sync::Mutex;
use std::time::{Duration, Instant};
2019-05-05 07:29:10 -04:00
pub struct RateLimiter {
locked_map: Mutex<HashMap<String, Instant>>,
cleanup_last: Mutex<Instant>,
cleanup_delay: Duration,
timeout: Duration,
}
impl RateLimiter {
pub fn new(timeout_secs: u64) -> Self {
RateLimiter {
locked_map: Mutex::new(HashMap::new()),
timeout: Duration::from_secs(timeout_secs),
cleanup_last: Mutex::new(Instant::now()),
cleanup_delay: Duration::from_secs(timeout_secs * 10),
}
}
pub fn action_perform(&self, identifier: String) -> bool {
self.maybe_cleanup();
let mut locked_map = self.locked_map.lock().unwrap();
2022-02-26 10:54:07 -05:00
let action_ok = locked_map
.get(&identifier)
2019-05-05 07:29:10 -04:00
.map(|instant| instant.elapsed())
.map(|duration| duration >= self.timeout)
.unwrap_or(true);
2022-02-26 10:54:07 -05:00
if action_ok {
2019-05-05 07:29:10 -04:00
locked_map.insert(identifier, Instant::now());
}
action_ok
}
2019-05-20 17:17:50 -04:00
pub fn action_check(&self, identifier: String) -> bool {
let locked_map = self.locked_map.lock().unwrap();
2022-02-26 10:54:07 -05:00
locked_map
.get(&identifier)
2019-05-20 17:17:50 -04:00
.map(|instant| instant.elapsed())
.map(|duration| duration >= self.timeout)
.unwrap_or(true)
}
2019-05-05 07:29:10 -04:00
fn maybe_cleanup(&self) {
let mut cleanup_last = self.cleanup_last.lock().unwrap();
if cleanup_last.elapsed() > self.cleanup_delay {
return;
}
*cleanup_last = Instant::now();
let mut locked_map = self.locked_map.lock().unwrap();
locked_map.retain(|_, instant| instant.elapsed() < self.timeout);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn perform_check_immediate() {
let rate_limiter = RateLimiter::new(1);
assert!(rate_limiter.action_perform("action".to_owned()));
2022-02-28 11:52:51 -05:00
assert!(!rate_limiter.action_perform("action".to_owned()));
2019-05-05 07:29:10 -04:00
}
#[test]
fn perform_check_after_timeout() {
let rate_limiter = RateLimiter::new(1);
assert!(rate_limiter.action_perform("action".to_owned()));
thread::sleep(Duration::from_secs(1));
assert!(rate_limiter.action_perform("action".to_owned()));
}
}