-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from SimerusM/core-backend-utils
Rate limiter functionality
- Loading branch information
Showing
7 changed files
with
157 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
bidict==0.23.1 | ||
blinker==1.8.2 | ||
click==8.1.7 | ||
dnspython==2.6.1 | ||
eventlet==0.36.1 | ||
Flask==3.0.3 | ||
Flask-Cors==4.0.1 | ||
Flask-SocketIO==5.3.6 | ||
greenlet==3.0.3 | ||
h11==0.14.0 | ||
itsdangerous==2.2.0 | ||
Jinja2==3.1.4 | ||
MarkupSafe==2.1.5 | ||
python-engineio==4.9.1 | ||
python-socketio==5.11.3 | ||
simple-websocket==1.0.0 | ||
six==1.16.0 | ||
uuid==1.30 | ||
Werkzeug==3.0.4 | ||
wsproto==1.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from collections import defaultdict, deque | ||
from .Debugger import Debugger | ||
import time | ||
|
||
class RateLimiter: | ||
__user_chat_timestamps = defaultdict(deque) # Dict to log chat request timestamps per user for rate limiting --> {user_id: [timestamp1, timestamp2, ...]} | ||
__meeting_user_count = defaultdict(int) # A dictionary to track the number of users in each meeting currently --> {meeting_id: user_count} | ||
|
||
def __init__(self, max_chat_requests_per_user: int, max_users_per_meeting: int, rate_limit_time_window: int): | ||
self.max_chat_requests_per_user = max_chat_requests_per_user # The maximum number of chat requests allowed per user within a specific time window | ||
self.max_users_per_meeting = max_users_per_meeting # The maximum number of users allowed to join a meeting at once | ||
self.rate_limit_time_window = rate_limit_time_window # The time frame within which the rate limiting is applied for chat requests (seconds) | ||
|
||
def updateMeetingCount(self, meeting_id: str, action: str) -> None: | ||
""" | ||
Function to update in-memory data structure on head counts in meetings | ||
""" | ||
if action == "join": | ||
self.__meeting_user_count[meeting_id] += 1 | ||
Debugger.log_message(Debugger.DEBUG, f"Meeting count: {self.__meeting_user_count[meeting_id]}") | ||
elif action == "leave": | ||
self.__meeting_user_count[meeting_id] -= 1 | ||
Debugger.log_message(Debugger.DEBUG, f"Meeting count: {self.__meeting_user_count[meeting_id]}") | ||
else: | ||
raise ValueError(f"Invalid action: {action}. Expected 'join' or 'leave'.") | ||
|
||
def rateLimitChat(self, username: str) -> bool: | ||
""" | ||
Checks if a user has exceeded their chat rate limit. | ||
This method: | ||
1. Adds the current timestamp to the user's chat history. | ||
2. Removes outdated timestamps outside the rate limit window. | ||
3. Checks if the number of recent messages exceeds the allowed limit. | ||
Args: | ||
username (str): The username to check. | ||
Returns: | ||
bool: True if the user is within the rate limit, False otherwise. | ||
""" | ||
curr_time = time.time() | ||
self.__user_chat_timestamps[username].append(curr_time) | ||
|
||
Debugger.log_message(Debugger.DEBUG, f"User chat timestamps: {self.__user_chat_timestamps[username]}") | ||
# Remove redundant time stamps | ||
while curr_time - self.__user_chat_timestamps[username][0] > self.rate_limit_time_window: | ||
self.__user_chat_timestamps[username].popleft() | ||
Debugger.log_message(Debugger.DEBUG, f"User chat timestamps: {self.__user_chat_timestamps[username]}") | ||
|
||
# Chat rate limit check | ||
if len(self.__user_chat_timestamps[username]) > self.max_chat_requests_per_user: | ||
return True | ||
|
||
return False | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters