muffin-rest 13.0.1__py3-none-any.whl → 13.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- muffin_rest/limits.py +11 -6
- {muffin_rest-13.0.1.dist-info → muffin_rest-13.0.2.dist-info}/METADATA +1 -1
- {muffin_rest-13.0.1.dist-info → muffin_rest-13.0.2.dist-info}/RECORD +5 -5
- {muffin_rest-13.0.1.dist-info → muffin_rest-13.0.2.dist-info}/LICENSE +0 -0
- {muffin_rest-13.0.1.dist-info → muffin_rest-13.0.2.dist-info}/WHEEL +0 -0
muffin_rest/limits.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import abc
|
|
2
|
+
from asyncio import Lock
|
|
2
3
|
from time import time
|
|
3
4
|
from typing import Any
|
|
4
5
|
|
|
@@ -13,13 +14,19 @@ class RateLimiter(abc.ABC):
|
|
|
13
14
|
limit (int): The limit of requests.
|
|
14
15
|
period (int): The period of time in seconds.
|
|
15
16
|
"""
|
|
17
|
+
self.lock = Lock()
|
|
16
18
|
self.limit = limit
|
|
17
19
|
self.period = period
|
|
18
20
|
|
|
19
|
-
@abc.abstractmethod
|
|
20
21
|
async def check(self, key: str) -> bool:
|
|
21
22
|
"""Check the request."""
|
|
22
|
-
|
|
23
|
+
async with self.lock:
|
|
24
|
+
return await self._check(key)
|
|
25
|
+
|
|
26
|
+
@abc.abstractmethod
|
|
27
|
+
async def _check(self, key: str) -> bool:
|
|
28
|
+
"""Check the request."""
|
|
29
|
+
raise NotImplementedError("Subclasses must implement this method.")
|
|
23
30
|
|
|
24
31
|
|
|
25
32
|
class MemoryRateLimiter(RateLimiter):
|
|
@@ -29,7 +36,7 @@ class MemoryRateLimiter(RateLimiter):
|
|
|
29
36
|
super().__init__(limit, **opts)
|
|
30
37
|
self.storage: dict[Any, tuple[float, int]] = {}
|
|
31
38
|
|
|
32
|
-
async def
|
|
39
|
+
async def _check(self, key: str) -> bool:
|
|
33
40
|
"""Check the request."""
|
|
34
41
|
now = time()
|
|
35
42
|
storage = self.storage
|
|
@@ -53,8 +60,6 @@ class MemoryRateLimiter(RateLimiter):
|
|
|
53
60
|
class RedisRateLimiter(RateLimiter):
|
|
54
61
|
"""Redis rate limiter."""
|
|
55
62
|
|
|
56
|
-
# TODO: Asyncio lock
|
|
57
|
-
|
|
58
63
|
def __init__(self, limit: int, *, redis, **opts):
|
|
59
64
|
"""Initialize the rate limiter.
|
|
60
65
|
|
|
@@ -66,7 +71,7 @@ class RedisRateLimiter(RateLimiter):
|
|
|
66
71
|
super().__init__(limit, **opts)
|
|
67
72
|
self.redis = redis
|
|
68
73
|
|
|
69
|
-
async def
|
|
74
|
+
async def _check(self, key: str) -> bool:
|
|
70
75
|
"""Check the request."""
|
|
71
76
|
value = await self.redis.get(key)
|
|
72
77
|
if value is None:
|
|
@@ -3,7 +3,7 @@ muffin_rest/api.py,sha256=bdbXMPiZi5yXf7_u3XB_gaGDHZKzDPqnnFnrwHoO1Rs,3799
|
|
|
3
3
|
muffin_rest/errors.py,sha256=Todg7CrCE1ufOsyyARI2Bv-RqNPH4Rl1p-Tkpq7VYrA,1151
|
|
4
4
|
muffin_rest/filters.py,sha256=nWlwDLuId-mmP3tgZjp1joYCVx0k9ekV42fPIMMKphM,5920
|
|
5
5
|
muffin_rest/handler.py,sha256=97nkx5rGB2CE_7oPG4UOLVw7b4UcGbubkSt1m_LrHhk,10762
|
|
6
|
-
muffin_rest/limits.py,sha256=
|
|
6
|
+
muffin_rest/limits.py,sha256=DnTa5DOtRUaZHP00HxqChzkARA53rKzv-ypG1K9s77c,2229
|
|
7
7
|
muffin_rest/marshmallow.py,sha256=-MyMKiaMTfiWw4y-4adpfoQd05HV1vEKqSXJh8eD3xg,548
|
|
8
8
|
muffin_rest/mongo/__init__.py,sha256=dck5zDEv0P3v_2CdlPI7Nbg8xay-EIzy9Y0yksEZkxw,4665
|
|
9
9
|
muffin_rest/mongo/filters.py,sha256=yIxIDVqMn6SoDgVhCqiTxYetw0hoaf_3jIvX2Vnizok,964
|
|
@@ -33,7 +33,7 @@ muffin_rest/sqlalchemy/types.py,sha256=Exm-zAQCtPAwXvYcCTtPRqSa-wTEWRcH_v2YSsJkB
|
|
|
33
33
|
muffin_rest/swagger.html,sha256=2uGLu_KpkYf925KnDKHBJmV9pm6OHn5C3BWScESsUS8,1736
|
|
34
34
|
muffin_rest/types.py,sha256=5NY9gPTIptv6U2Qab2xMXlS_jZuTuR0bquFFwWRURcA,510
|
|
35
35
|
muffin_rest/utils.py,sha256=c08E4HJ4SLYC-91GKPEbsyKTZ4sZbTN4qDqJbNg_HTE,2076
|
|
36
|
-
muffin_rest-13.0.
|
|
37
|
-
muffin_rest-13.0.
|
|
38
|
-
muffin_rest-13.0.
|
|
39
|
-
muffin_rest-13.0.
|
|
36
|
+
muffin_rest-13.0.2.dist-info/LICENSE,sha256=xHPkOZhjyKBMOwXpWn9IB_BVLjrrMxv2M9slKkHj2hM,1082
|
|
37
|
+
muffin_rest-13.0.2.dist-info/METADATA,sha256=-910IUUuufLuVb5NjT5MV1UwIbe2pCXWKWL4pIXI5Yo,4912
|
|
38
|
+
muffin_rest-13.0.2.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
|
39
|
+
muffin_rest-13.0.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|