-
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.
- Loading branch information
1 parent
7755a21
commit 460b7e5
Showing
3 changed files
with
57 additions
and
31 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sys | ||
sys.path.append("") | ||
|
||
from asyncio import Lock | ||
import time | ||
|
||
class RateLimiter: | ||
def __init__(self, max_requests_per_min): | ||
self.max_requests = max_requests_per_min | ||
self.request_times = [] | ||
self.lock = Lock() | ||
|
||
async def is_allowed(self) -> bool: | ||
async with self.lock: | ||
current_time = time.time() | ||
# Remove requests older than 1 minute | ||
self.request_times = [t for t in self.request_times if current_time - t < 60] | ||
# Check if rate limit exceeded | ||
if len(self.request_times) >= self.max_requests: | ||
return False | ||
# Record current request time | ||
self.request_times.append(current_time) | ||
return True |