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