Skip to content
Merged
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
23 changes: 23 additions & 0 deletions src/core/rate_limiter.py
Original file line number Diff line number Diff line change
@@ -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
Loading