Files
Arcaea-server/latest version/core/limiter.py
Lost-MSth ba36190f30 [Enhance][Bug fix] Improve setting file & ...
- Fix a small bug that `best30` of API cannot have scores whose songs are not in database
- At present the setting file can be a module or a file with some of options
- Limiter can have multiple rules together now
2022-10-16 15:49:49 +08:00

32 lines
915 B
Python

from limits import parse_many, strategies
from limits.storage import storage_from_string
class ArcLimiter:
storage = storage_from_string("memory://")
strategy = strategies.FixedWindowRateLimiter(storage)
def __init__(self, limit_str: str = None, namespace: str = None):
self._limits = None
self.limits = limit_str
self.namespace = namespace
@property
def limits(self):
return self._limits
@limits.setter
def limits(self, value: str):
if value is None:
return
self._limits = parse_many(value)
def hit(self, key: str, cost: int = 1) -> bool:
flag = True
for limit in self.limits:
flag &= self.strategy.hit(limit, self.namespace, key, cost)
return flag
def test(self, key: str) -> bool:
return all(self.strategy.test(limit, self.namespace, key) for limit in self.limits)