Rodolphe Bréard 2 years ago
parent
commit
abebe6b49e
  1. 11
      acmed/src/endpoint.rs

11
acmed/src/endpoint.rs

@ -101,7 +101,8 @@ impl RateLimit {
fn request_allowed(&self) -> bool { fn request_allowed(&self) -> bool {
for (max_allowed, duration) in self.limits.iter() { for (max_allowed, duration) in self.limits.iter() {
let max_date = Instant::now() - *duration;
match Instant::now().checked_sub(*duration) {
Some(max_date) => {
let nb_req = self let nb_req = self
.query_log .query_log
.iter() .iter()
@ -111,13 +112,19 @@ impl RateLimit {
return false; return false;
} }
} }
None => {
return false;
}
};
}
true true
} }
fn prune_log(&mut self) { fn prune_log(&mut self) {
if let Some((_, max_limit)) = self.limits.first() { if let Some((_, max_limit)) = self.limits.first() {
let prune_date = Instant::now() - *max_limit;
if let Some(prune_date) = Instant::now().checked_sub(*max_limit) {
self.query_log.retain(move |&d| d > prune_date); self.query_log.retain(move |&d| d > prune_date);
} }
} }
} }
}
Loading…
Cancel
Save