-
Notifications
You must be signed in to change notification settings - Fork 3
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
fa55e1e
commit 2d7726f
Showing
1 changed file
with
32 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Created on 20.08.2024 | ||
@author: wf | ||
""" | ||
|
||
from functools import wraps | ||
|
||
from ratelimit import limits, sleep_and_retry | ||
|
||
|
||
class RateLimiter: | ||
""" | ||
Wrap the @limits decorator in a new decorator | ||
""" | ||
|
||
def __init__(self, calls_per_minute: int = None): | ||
if calls_per_minute is None: | ||
calls_per_minute = 60 * 1000 * 1000 # use an irrationally high value | ||
self.calls_per_minute = calls_per_minute | ||
|
||
def rate_limited(self, f: callable): | ||
@wraps(f) | ||
def wrapper(*args, **kwargs): | ||
@sleep_and_retry | ||
@limits(calls=self.calls_per_minute, period=60) | ||
def rate_limited_function(): | ||
return f(*args, **kwargs) | ||
|
||
return rate_limited_function() | ||
|
||
return wrapper |