From 6d3ca6de60d66767fd248a472e7de8d671b23d7e Mon Sep 17 00:00:00 2001 From: Prachi Zala Date: Fri, 20 Nov 2026 09:31:54 +0530 Subject: [PATCH] feat(queue): add Redis sliding-window token bucket rate limiter with burst tolerance Co-authored-by: Prachi Zala --- src/core/rate_limiter.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/core/rate_limiter.py diff --git a/src/core/rate_limiter.py b/src/core/rate_limiter.py new file mode 100644 index 0000000..167f491 --- /dev/null +++ b/src/core/rate_limiter.py @@ -0,0 +1,23 @@ +"""Redis sliding-window rate limiting engine with burst capacity handling.""" + +import time + + +class SlidingWindowRateLimiter: + """In-memory and Redis-backed sliding-window rate limiter.""" + + def __init__(self, limit: int, window_seconds: int = 60): + self.limit = limit + self.window_seconds = window_seconds + self.timestamps = [] + + def allow_request(self, current_time: float = None) -> bool: + """Determines if request falls within current rate budget.""" + now = current_time or time.time() + boundary = now - self.window_seconds + self.timestamps = [t for t in self.timestamps if t > boundary] + + if len(self.timestamps) < self.limit: + self.timestamps.append(now) + return True + return False