From 4683eadc89cb783982a97a99ca3e24d2bb94d170 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Fri, 23 Dec 2022 16:14:10 -0800 Subject: [PATCH] fix overflow --- deferred-rate-limiter/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deferred-rate-limiter/src/lib.rs b/deferred-rate-limiter/src/lib.rs index 2ed891c8..eef1640d 100644 --- a/deferred-rate-limiter/src/lib.rs +++ b/deferred-rate-limiter/src/lib.rs @@ -192,7 +192,9 @@ where // if close to max_per_period, wait for redis // TODO: how close should we allow? depends on max expected concurent requests from one user - if expected_key_count > max_requests_per_period * 99 / 100 { + let limit: f64 = (max_requests_per_period as f64 * 0.99) + .min(max_requests_per_period as f64 - 1.0); + if expected_key_count > limit as u64 { // close to period. don't risk it. wait on redis Ok(rate_limit_f.await) } else {