mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-09 09:17:26 +08:00
[Enhance] Add limiter & Building API
- Add a custom limiter and use it for limiting users' download instead of using database > So add a requirement `limits`. - Fix a character's value - Change the email max length to 64 - Change something about API's roles and powers - Add an API endpoint for getting users' roles and powers
This commit is contained in:
28
latest version/core/limiter.py
Normal file
28
latest version/core/limiter.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from limits import parse, strategies
|
||||
from limits.storage import storage_from_string
|
||||
|
||||
|
||||
class ArcLimiter:
|
||||
storage = storage_from_string("memory://")
|
||||
strategy = strategies.FixedWindowRateLimiter(storage)
|
||||
|
||||
def __init__(self, limit: str = None, namespace: str = None):
|
||||
self._limit = None
|
||||
self.limit = limit
|
||||
self.namespace = namespace
|
||||
|
||||
@property
|
||||
def limit(self):
|
||||
return self._limit
|
||||
|
||||
@limit.setter
|
||||
def limit(self, value):
|
||||
if value is None:
|
||||
return
|
||||
self._limit = parse(value)
|
||||
|
||||
def hit(self, key: str, cost: int = 1) -> bool:
|
||||
return self.strategy.hit(self.limit, self.namespace, key, cost=cost)
|
||||
|
||||
def test(self, key: str) -> bool:
|
||||
return self.strategy.test(self.limit, self.namespace, key)
|
||||
Reference in New Issue
Block a user