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
6 changes: 3 additions & 3 deletions src/regression_model_template/controller/kafka_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, max_requests: int = 100, window_seconds: int = 60, max_tracke
self.max_requests = max_requests
self.window_seconds = window_seconds
self.max_tracked_ips = max_tracked_ips
self.tracked_ips: collections.OrderedDict[str, list[float]] = collections.OrderedDict()
self.tracked_ips: collections.OrderedDict[str, collections.deque[float]] = collections.OrderedDict()

def is_allowed(self, ip: str) -> bool:
"""Check if the given IP is allowed to make a request."""
Expand All @@ -98,15 +98,15 @@ def is_allowed(self, ip: str) -> bool:
if ip not in self.tracked_ips:
if len(self.tracked_ips) >= self.max_tracked_ips:
self.tracked_ips.popitem(last=False) # Evict oldest
self.tracked_ips[ip] = []
self.tracked_ips[ip] = collections.deque()

# Mark as recently used
self.tracked_ips.move_to_end(ip)

timestamps = self.tracked_ips[ip]
# Remove old timestamps
while timestamps and current_time - timestamps[0] > self.window_seconds:
timestamps.pop(0)
timestamps.popleft()

if len(timestamps) >= self.max_requests:
return False
Expand Down
Loading