Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions backend/secuscan/rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand Down
Loading