mirror of
https://github.com/Mailu/Mailu.git
synced 2024-12-20 20:22:38 +02:00
8e88f1b8c3
Rate limiting was already redesigned to use Python limits. This introduced some unexpected behavior, including the fact that only one criteria is supported per limiter. Docs and setup utility are updated with this in mind. Also, the code was made more generic, so limiters can be delivered for something else than authentication. Authentication-specific code was moved directly to the authentication routine.
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
import limits
|
|
import limits.storage
|
|
import limits.strategies
|
|
|
|
|
|
class LimitWrapper(object):
|
|
""" Wraps a limit by providing the storage, item and identifiers
|
|
"""
|
|
|
|
def __init__(self, limiter, limit, *identifiers):
|
|
self.limiter = limiter
|
|
self.limit = limit
|
|
self.base_identifiers = identifiers
|
|
|
|
def test(self, *args):
|
|
return self.limiter.test(self.limit, *(self.base_identifiers + args))
|
|
|
|
def hit(self, *args):
|
|
return self.limiter.hit(self.limit, *(self.base_identifiers + args))
|
|
|
|
def get_window_stats(self, *args):
|
|
return self.limiter.get_window_stats(self.limit, *(self.base_identifiers + args))
|
|
|
|
|
|
class LimitWraperFactory(object):
|
|
""" Global limiter, to be used as a factory
|
|
"""
|
|
|
|
def init_app(self, app):
|
|
self.storage = limits.storage.storage_from_string(app.config["RATELIMIT_STORAGE_URL"])
|
|
self.limiter = limits.strategies.MovingWindowRateLimiter(self.storage)
|
|
|
|
def get_limiter(self, limit, *args):
|
|
return LimitWrapper(self.limiter, limits.parse(limit), *args) |