Skip to content

Commit

Permalink
adds rate_limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Aug 20, 2024
1 parent fa55e1e commit 2d7726f
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lodstorage/rate_limiter.py
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

0 comments on commit 2d7726f

Please sign in to comment.