From d9fb111a93511534dcced22ba4b313a2b38d4816 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Thu, 2 May 2024 19:50:12 -0400 Subject: [PATCH] Only re-enqueue expired cache entries, rather than re-init at 0 --- limitador/src/storage/redis/counters_cache.rs | 4 +++- limitador/src/storage/redis/redis_cached.rs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/limitador/src/storage/redis/counters_cache.rs b/limitador/src/storage/redis/counters_cache.rs index caa9a06d..b940ad83 100644 --- a/limitador/src/storage/redis/counters_cache.rs +++ b/limitador/src/storage/redis/counters_cache.rs @@ -128,7 +128,9 @@ impl CachedCounterValue { } pub fn requires_fast_flush(&self, within: &Duration) -> bool { - self.from_authority.load(Ordering::Acquire).not() || &self.value.ttl() <= within + self.from_authority.load(Ordering::Acquire).not() + || self.expired_at(SystemTime::now()) + || &self.value.ttl() <= within } } diff --git a/limitador/src/storage/redis/redis_cached.rs b/limitador/src/storage/redis/redis_cached.rs index 5a092f61..df492d7d 100644 --- a/limitador/src/storage/redis/redis_cached.rs +++ b/limitador/src/storage/redis/redis_cached.rs @@ -75,11 +75,10 @@ impl AsyncCounterStorage for CachedRedisStorage { let mut not_cached: Vec<&mut Counter> = vec![]; let mut first_limited = None; - let now = SystemTime::now(); // Check cached counters for counter in counters.iter_mut() { match self.cached_counters.get(counter) { - Some(val) if !val.expired_at(now) => { + Some(val) => { if first_limited.is_none() && val.is_limited(counter, delta) { let a = Authorization::Limited(counter.limit().name().map(|n| n.to_owned()));