From eda569ee64c5d7864d635fac0df4121f8d9861de Mon Sep 17 00:00:00 2001 From: Tanishka Kuwar Date: Wed, 5 Aug 2026 10:08:54 +0530 Subject: [PATCH] Avoid resetting Redis rate limit TTL --- backend/secuscan/rate_limiter.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/secuscan/rate_limiter.py b/backend/secuscan/rate_limiter.py index 6809d08ab..8344212d8 100644 --- a/backend/secuscan/rate_limiter.py +++ b/backend/secuscan/rate_limiter.py @@ -187,7 +187,11 @@ async def check(self, request: Request) -> None: pipe = self._redis.pipeline() pipe.incr(minute_key) - pipe.expire(minute_key, self._rate_window * 2) # 2x TTL for safety + pipe.expire( + minute_key, + self._rate_window * 2, + nx=True, + ) # Set TTL only if one does not already exist. results = await pipe.execute() minute_count = results[0] @@ -218,7 +222,11 @@ async def check(self, request: Request) -> None: pipe2 = self._redis.pipeline() pipe2.incr(hour_key) - pipe2.expire(hour_key, self._burst_window * 2) + pipe2.expire( + hour_key, + self._burst_window * 2, + nx=True, + ) # Set TTL only if one does not already exist. results2 = await pipe2.execute() hour_count = results2[0]