limitra 0.0.1__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.
- limitra/__init__.py +27 -0
- limitra/_config.py +95 -0
- limitra/_decorator.py +279 -0
- limitra/_version.py +24 -0
- limitra/algorithms/__init__.py +23 -0
- limitra/algorithms/fixed_window.py +133 -0
- limitra/algorithms/leaky_bucket.py +139 -0
- limitra/algorithms/sliding_window.py +144 -0
- limitra/algorithms/token_bucket.py +136 -0
- limitra/base.py +152 -0
- limitra/exceptions.py +15 -0
- limitra/integrations/__init__.py +0 -0
- limitra/py.typed +0 -0
- limitra-0.0.1.dist-info/METADATA +66 -0
- limitra-0.0.1.dist-info/RECORD +18 -0
- limitra-0.0.1.dist-info/WHEEL +5 -0
- limitra-0.0.1.dist-info/licenses/LICENSE +21 -0
- limitra-0.0.1.dist-info/top_level.txt +1 -0
limitra/__init__.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""limitra — simple, framework-agnostic rate limiting for Python."""
|
|
2
|
+
|
|
3
|
+
from ._config import LimitraConfig
|
|
4
|
+
from ._decorator import rate_limit
|
|
5
|
+
from .algorithms import (
|
|
6
|
+
LIMITERS,
|
|
7
|
+
FixedWindowRateLimiter,
|
|
8
|
+
LeakyBucketLimiter,
|
|
9
|
+
SlidingWindowRateLimiter,
|
|
10
|
+
TokenBucketLimiter,
|
|
11
|
+
)
|
|
12
|
+
from .base import BaseRateLimiter
|
|
13
|
+
from .exceptions import RateLimitExceeded
|
|
14
|
+
|
|
15
|
+
from ._version import __version__
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"LimitraConfig",
|
|
19
|
+
"rate_limit",
|
|
20
|
+
"RateLimitExceeded",
|
|
21
|
+
"BaseRateLimiter",
|
|
22
|
+
"TokenBucketLimiter",
|
|
23
|
+
"LeakyBucketLimiter",
|
|
24
|
+
"FixedWindowRateLimiter",
|
|
25
|
+
"SlidingWindowRateLimiter",
|
|
26
|
+
"LIMITERS",
|
|
27
|
+
]
|
limitra/_config.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Global configuration singleton for limitra."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
import redis as _redis_lib
|
|
8
|
+
except ImportError:
|
|
9
|
+
_redis_lib = None # type: ignore[assignment]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class _Config:
|
|
14
|
+
"""Holds global defaults applied to every rate limiter."""
|
|
15
|
+
|
|
16
|
+
redis_client: Optional[Any] = None
|
|
17
|
+
project: Optional[str] = None
|
|
18
|
+
prefix: Optional[str] = None
|
|
19
|
+
suffix: Optional[str] = None
|
|
20
|
+
default_algorithm: str = "sliding_window"
|
|
21
|
+
default_backend: str = "memory"
|
|
22
|
+
fail_open: bool = False
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _ConfigStore:
|
|
26
|
+
"""Mutable holder for the active configuration (avoids module-level global)."""
|
|
27
|
+
|
|
28
|
+
_current: _Config = _Config()
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def get(cls) -> _Config:
|
|
32
|
+
"""Return the active configuration."""
|
|
33
|
+
return cls._current
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def set(cls, config: _Config) -> None:
|
|
37
|
+
"""Replace the active configuration."""
|
|
38
|
+
cls._current = config
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def reset(cls) -> None:
|
|
42
|
+
"""Reset to default configuration."""
|
|
43
|
+
cls._current = _Config()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def LimitraConfig( # pylint: disable=invalid-name
|
|
47
|
+
redis_url: Optional[str] = None,
|
|
48
|
+
redis_client: Optional[Any] = None,
|
|
49
|
+
redis_db: int = 0,
|
|
50
|
+
project: Optional[str] = None,
|
|
51
|
+
prefix: Optional[str] = None,
|
|
52
|
+
suffix: Optional[str] = None,
|
|
53
|
+
default_algorithm: str = "sliding_window",
|
|
54
|
+
default_backend: str = "redis",
|
|
55
|
+
fail_open: bool = False,
|
|
56
|
+
) -> None:
|
|
57
|
+
"""Configure global defaults for all rate limiters.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
redis_url: Redis connection URL (e.g. "redis://localhost:6379")
|
|
61
|
+
redis_client: Pre-built Redis client (alternative to redis_url)
|
|
62
|
+
redis_db: Redis database index when using redis_url
|
|
63
|
+
project: Namespace prefix for all keys — use to isolate microservices
|
|
64
|
+
sharing the same Redis instance (e.g. "user-service")
|
|
65
|
+
prefix: Optional key prefix added before project (e.g. "rl")
|
|
66
|
+
suffix: Optional key suffix added after identifier (e.g. "v2")
|
|
67
|
+
default_algorithm: Algorithm used when none is specified in @rate_limit
|
|
68
|
+
default_backend: "redis" or "memory"
|
|
69
|
+
"""
|
|
70
|
+
if redis_url is not None:
|
|
71
|
+
if _redis_lib is None:
|
|
72
|
+
raise ImportError("redis package required: pip install limitra")
|
|
73
|
+
redis_client = _redis_lib.from_url(redis_url, db=redis_db)
|
|
74
|
+
|
|
75
|
+
_ConfigStore.set(
|
|
76
|
+
_Config(
|
|
77
|
+
redis_client=redis_client or _ConfigStore.get().redis_client,
|
|
78
|
+
project=project,
|
|
79
|
+
prefix=prefix,
|
|
80
|
+
suffix=suffix,
|
|
81
|
+
default_algorithm=default_algorithm,
|
|
82
|
+
default_backend=default_backend,
|
|
83
|
+
fail_open=fail_open,
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def get_config() -> _Config:
|
|
89
|
+
"""Return the active global configuration."""
|
|
90
|
+
return _ConfigStore.get()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def reset_config() -> None:
|
|
94
|
+
"""Reset global configuration to defaults (used in tests)."""
|
|
95
|
+
_ConfigStore.reset()
|
limitra/_decorator.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"""The @rate_limit decorator — works with any sync or async Python function."""
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
from inspect import iscoroutinefunction, signature
|
|
5
|
+
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, Union, cast
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
from ._config import get_config
|
|
9
|
+
from .algorithms import LIMITERS
|
|
10
|
+
from .base import BaseRateLimiter
|
|
11
|
+
from .exceptions import RateLimitExceeded
|
|
12
|
+
|
|
13
|
+
# Window-based algorithms derive fill_rate as 1/window; others use requests/window.
|
|
14
|
+
_WINDOW_BASED = {"sliding_window", "fixed_window"}
|
|
15
|
+
|
|
16
|
+
F = TypeVar("F", bound=Callable[..., Any])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _build_limiter(
|
|
20
|
+
requests: int,
|
|
21
|
+
window: float,
|
|
22
|
+
algorithm: Optional[str],
|
|
23
|
+
scope: str,
|
|
24
|
+
backend: Optional[str],
|
|
25
|
+
redis_client: Optional[Any],
|
|
26
|
+
project: Optional[str],
|
|
27
|
+
prefix: Optional[str],
|
|
28
|
+
suffix: Optional[str],
|
|
29
|
+
fail_open: Optional[bool] = None,
|
|
30
|
+
) -> BaseRateLimiter:
|
|
31
|
+
"""Instantiate the appropriate limiter class with resolved configuration."""
|
|
32
|
+
config = get_config()
|
|
33
|
+
algorithm = algorithm or config.default_algorithm
|
|
34
|
+
backend = backend or config.default_backend
|
|
35
|
+
redis_client = redis_client if redis_client is not None else config.redis_client
|
|
36
|
+
project = project if project is not None else config.project
|
|
37
|
+
prefix = prefix if prefix is not None else config.prefix
|
|
38
|
+
suffix = suffix if suffix is not None else config.suffix
|
|
39
|
+
resolved_fail_open: bool = config.fail_open if fail_open is None else fail_open
|
|
40
|
+
|
|
41
|
+
if backend == "redis" and redis_client is None:
|
|
42
|
+
raise ValueError(
|
|
43
|
+
"Redis backend requires a client. "
|
|
44
|
+
"Call limitra.LimitraConfig(redis_url='redis://...') first."
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
limiter_cls = LIMITERS.get(algorithm)
|
|
48
|
+
if limiter_cls is None:
|
|
49
|
+
raise ValueError(f"Unknown algorithm '{algorithm}'. Options: {list(LIMITERS)}")
|
|
50
|
+
|
|
51
|
+
fill_rate = (1 / window) if algorithm in _WINDOW_BASED else (requests / window)
|
|
52
|
+
|
|
53
|
+
kwargs: Dict[str, Any] = {
|
|
54
|
+
"capacity": requests,
|
|
55
|
+
"fill_rate": fill_rate,
|
|
56
|
+
"scope": scope,
|
|
57
|
+
"backend": backend,
|
|
58
|
+
"project": project,
|
|
59
|
+
"prefix": prefix,
|
|
60
|
+
"suffix": suffix,
|
|
61
|
+
"fail_open": resolved_fail_open,
|
|
62
|
+
}
|
|
63
|
+
if backend == "redis":
|
|
64
|
+
kwargs["redis_client"] = redis_client
|
|
65
|
+
|
|
66
|
+
return limiter_cls(**kwargs) # type: ignore[no-any-return]
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _extract_identifier(
|
|
70
|
+
key: Union[str, int, Callable[..., str], None],
|
|
71
|
+
func: Callable[..., Any],
|
|
72
|
+
args: Tuple[Any, ...],
|
|
73
|
+
kwargs: Dict[str, Any],
|
|
74
|
+
) -> Optional[str]:
|
|
75
|
+
"""Extract the rate-limit identifier from the decorated function's arguments."""
|
|
76
|
+
if key is None:
|
|
77
|
+
return None
|
|
78
|
+
if callable(key):
|
|
79
|
+
return str(key(*args, **kwargs))
|
|
80
|
+
if isinstance(key, int):
|
|
81
|
+
return str(args[key])
|
|
82
|
+
if isinstance(key, str):
|
|
83
|
+
if key in kwargs:
|
|
84
|
+
return str(kwargs[key])
|
|
85
|
+
params = list(signature(func).parameters.keys())
|
|
86
|
+
if key in params:
|
|
87
|
+
idx = params.index(key)
|
|
88
|
+
if idx < len(args):
|
|
89
|
+
return str(args[idx])
|
|
90
|
+
raise ValueError(f"rate_limit key='{key}' not found in function arguments")
|
|
91
|
+
raise TypeError(f"rate_limit key must be str, int, or callable — got {type(key)}")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def rate_limit(
|
|
95
|
+
requests: Union[int, Callable[..., int]] = 0,
|
|
96
|
+
window: Union[float, Callable[..., float]] = 0,
|
|
97
|
+
algorithm: Optional[str] = None,
|
|
98
|
+
key: Union[str, int, Callable[..., str], None] = None,
|
|
99
|
+
scope: str = "user",
|
|
100
|
+
backend: Optional[str] = None,
|
|
101
|
+
redis_client: Optional[Any] = None,
|
|
102
|
+
project: Optional[str] = None,
|
|
103
|
+
prefix: Optional[str] = None,
|
|
104
|
+
suffix: Optional[str] = None,
|
|
105
|
+
on_exceeded: Optional[Callable[..., Any]] = None,
|
|
106
|
+
limits: Optional[List[Tuple[int, float]]] = None,
|
|
107
|
+
block: bool = True,
|
|
108
|
+
exempt_when: Optional[Callable[..., bool]] = None,
|
|
109
|
+
fail_open: Optional[bool] = None,
|
|
110
|
+
) -> Callable[[F], F]:
|
|
111
|
+
"""Decorator that rate-limits any Python function (sync or async).
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
requests: Maximum calls allowed within the window. Accepts a callable
|
|
115
|
+
evaluated per-request as ``requests(*args, **kwargs) → int``
|
|
116
|
+
for dynamic per-caller limits (e.g. tiered plans).
|
|
117
|
+
window: Time window in seconds. Accepts a callable evaluated
|
|
118
|
+
per-request as ``window(*args, **kwargs) → float``.
|
|
119
|
+
algorithm: One of "sliding_window" (default), "token_bucket",
|
|
120
|
+
"leaky_bucket", "fixed_window".
|
|
121
|
+
key: How to extract the per-caller identifier from the function args:
|
|
122
|
+
- None → single global counter for this function
|
|
123
|
+
- "arg_name" → use the named argument's value
|
|
124
|
+
- 0, 1, ... → use positional argument by index
|
|
125
|
+
- callable → called as key(*args, **kwargs), return a str
|
|
126
|
+
scope: Label stored in the Redis key (e.g. "user", "ip", "team").
|
|
127
|
+
backend: "redis" or "memory". Falls back to LimitraConfig() default.
|
|
128
|
+
redis_client: Override the global Redis client for this limiter only.
|
|
129
|
+
project: Override the global project namespace for this limiter only.
|
|
130
|
+
prefix: Override the global key prefix for this limiter only.
|
|
131
|
+
suffix: Override the global key suffix for this limiter only.
|
|
132
|
+
on_exceeded: Optional callable(exc: RateLimitExceeded) — called instead of
|
|
133
|
+
raising when the limit is hit.
|
|
134
|
+
limits: List of (requests, window) tuples. When given, ALL limits must
|
|
135
|
+
pass. Takes precedence over requests/window.
|
|
136
|
+
block: When False, the decorated function always runs. If over limit,
|
|
137
|
+
on_exceeded is called as a side effect only.
|
|
138
|
+
exempt_when: Optional callable(*args, **kwargs) → bool. When it returns
|
|
139
|
+
True, the request bypasses rate limiting entirely.
|
|
140
|
+
fail_open: When True, allow requests on backend errors instead of
|
|
141
|
+
falling back to memory.
|
|
142
|
+
|
|
143
|
+
Raises:
|
|
144
|
+
RateLimitExceeded: When the limit is hit and on_exceeded is not set.
|
|
145
|
+
"""
|
|
146
|
+
actual_scope = "global" if key is None else scope
|
|
147
|
+
_is_dynamic = (callable(requests) or callable(window)) and limits is None
|
|
148
|
+
|
|
149
|
+
def decorator(func: F) -> F:
|
|
150
|
+
"""Wrap func with rate-limiting logic."""
|
|
151
|
+
_static_limiters: Optional[List[BaseRateLimiter]] = None
|
|
152
|
+
_dynamic_cache: Dict[Tuple[int, float], BaseRateLimiter] = {}
|
|
153
|
+
|
|
154
|
+
def _build_static() -> List[BaseRateLimiter]:
|
|
155
|
+
nonlocal _static_limiters
|
|
156
|
+
if _static_limiters is not None:
|
|
157
|
+
return _static_limiters
|
|
158
|
+
result: List[BaseRateLimiter] = []
|
|
159
|
+
if limits is not None:
|
|
160
|
+
use_suffix = len(limits) > 1
|
|
161
|
+
for req, win in limits:
|
|
162
|
+
lim_scope = (
|
|
163
|
+
f"{actual_scope}_{int(win)}" if use_suffix else actual_scope
|
|
164
|
+
)
|
|
165
|
+
result.append(
|
|
166
|
+
_build_limiter(
|
|
167
|
+
requests=req,
|
|
168
|
+
window=win,
|
|
169
|
+
algorithm=algorithm,
|
|
170
|
+
scope=lim_scope,
|
|
171
|
+
backend=backend,
|
|
172
|
+
redis_client=redis_client,
|
|
173
|
+
project=project,
|
|
174
|
+
prefix=prefix,
|
|
175
|
+
suffix=suffix,
|
|
176
|
+
fail_open=fail_open,
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
else:
|
|
180
|
+
result = [
|
|
181
|
+
_build_limiter(
|
|
182
|
+
requests=int(requests), # type: ignore[arg-type]
|
|
183
|
+
window=float(window), # type: ignore[arg-type]
|
|
184
|
+
algorithm=algorithm,
|
|
185
|
+
scope=actual_scope,
|
|
186
|
+
backend=backend,
|
|
187
|
+
redis_client=redis_client,
|
|
188
|
+
project=project,
|
|
189
|
+
prefix=prefix,
|
|
190
|
+
suffix=suffix,
|
|
191
|
+
fail_open=fail_open,
|
|
192
|
+
)
|
|
193
|
+
]
|
|
194
|
+
_static_limiters = result
|
|
195
|
+
return result
|
|
196
|
+
|
|
197
|
+
def _get_dynamic(req: int, win: float) -> BaseRateLimiter:
|
|
198
|
+
cache_key = (req, win)
|
|
199
|
+
if cache_key not in _dynamic_cache:
|
|
200
|
+
_dynamic_cache[cache_key] = _build_limiter(
|
|
201
|
+
requests=req,
|
|
202
|
+
window=win,
|
|
203
|
+
algorithm=algorithm,
|
|
204
|
+
scope=actual_scope,
|
|
205
|
+
backend=backend,
|
|
206
|
+
redis_client=redis_client,
|
|
207
|
+
project=project,
|
|
208
|
+
prefix=prefix,
|
|
209
|
+
suffix=suffix,
|
|
210
|
+
fail_open=fail_open,
|
|
211
|
+
)
|
|
212
|
+
return _dynamic_cache[cache_key]
|
|
213
|
+
|
|
214
|
+
def _check(
|
|
215
|
+
identifier: Optional[str],
|
|
216
|
+
args: Tuple[Any, ...],
|
|
217
|
+
kwargs_inner: Dict[str, Any],
|
|
218
|
+
) -> Optional[Any]:
|
|
219
|
+
if exempt_when is not None and exempt_when(*args, **kwargs_inner):
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
lims: List[BaseRateLimiter]
|
|
223
|
+
pairs: List[Tuple[int, float]]
|
|
224
|
+
|
|
225
|
+
if _is_dynamic:
|
|
226
|
+
actual_req = (
|
|
227
|
+
int(requests(*args, **kwargs_inner))
|
|
228
|
+
if callable(requests)
|
|
229
|
+
else requests
|
|
230
|
+
)
|
|
231
|
+
actual_win = (
|
|
232
|
+
float(window(*args, **kwargs_inner)) if callable(window) else window
|
|
233
|
+
)
|
|
234
|
+
lims = [_get_dynamic(actual_req, actual_win)]
|
|
235
|
+
pairs = [(actual_req, actual_win)]
|
|
236
|
+
else:
|
|
237
|
+
lims = _build_static()
|
|
238
|
+
pairs = (
|
|
239
|
+
list(limits)
|
|
240
|
+
if limits is not None
|
|
241
|
+
else [(int(requests), float(window))] # type: ignore[arg-type]
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
for i, limiter in enumerate(lims):
|
|
245
|
+
if not limiter.allow_request(identifier):
|
|
246
|
+
req, win = pairs[i]
|
|
247
|
+
wait = limiter.get_wait_time(identifier)
|
|
248
|
+
exc = RateLimitExceeded(requests=req, window=win, retry_after=wait)
|
|
249
|
+
if not block:
|
|
250
|
+
if on_exceeded is not None:
|
|
251
|
+
on_exceeded(exc)
|
|
252
|
+
return None
|
|
253
|
+
if on_exceeded is not None:
|
|
254
|
+
return on_exceeded(exc)
|
|
255
|
+
raise exc
|
|
256
|
+
return None
|
|
257
|
+
|
|
258
|
+
@functools.wraps(func)
|
|
259
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
260
|
+
"""Async rate-limited wrapper."""
|
|
261
|
+
identifier = _extract_identifier(key, func, args, kwargs)
|
|
262
|
+
result = _check(identifier, args, kwargs)
|
|
263
|
+
if result is not None:
|
|
264
|
+
return result
|
|
265
|
+
return await func(*args, **kwargs)
|
|
266
|
+
|
|
267
|
+
@functools.wraps(func)
|
|
268
|
+
def sync_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
269
|
+
"""Sync rate-limited wrapper."""
|
|
270
|
+
identifier = _extract_identifier(key, func, args, kwargs)
|
|
271
|
+
result = _check(identifier, args, kwargs)
|
|
272
|
+
if result is not None:
|
|
273
|
+
return result
|
|
274
|
+
return func(*args, **kwargs)
|
|
275
|
+
|
|
276
|
+
wrapper = async_wrapper if iscoroutinefunction(func) else sync_wrapper
|
|
277
|
+
return cast(F, wrapper)
|
|
278
|
+
|
|
279
|
+
return decorator
|
limitra/_version.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '0.0.1'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 0, 1)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = None
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Rate-limiting algorithm implementations."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict
|
|
4
|
+
|
|
5
|
+
from .fixed_window import FixedWindowRateLimiter
|
|
6
|
+
from .leaky_bucket import LeakyBucketLimiter
|
|
7
|
+
from .sliding_window import SlidingWindowRateLimiter
|
|
8
|
+
from .token_bucket import TokenBucketLimiter
|
|
9
|
+
|
|
10
|
+
LIMITERS: Dict[str, Any] = {
|
|
11
|
+
"token_bucket": TokenBucketLimiter,
|
|
12
|
+
"leaky_bucket": LeakyBucketLimiter,
|
|
13
|
+
"fixed_window": FixedWindowRateLimiter,
|
|
14
|
+
"sliding_window": SlidingWindowRateLimiter,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"FixedWindowRateLimiter",
|
|
19
|
+
"LeakyBucketLimiter",
|
|
20
|
+
"SlidingWindowRateLimiter",
|
|
21
|
+
"TokenBucketLimiter",
|
|
22
|
+
"LIMITERS",
|
|
23
|
+
]
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Fixed window rate limiter: counts requests per discrete time slot."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Dict, Optional
|
|
5
|
+
|
|
6
|
+
from ..base import BaseRateLimiter
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from redis import RedisError as _RedisError
|
|
10
|
+
except ImportError:
|
|
11
|
+
_RedisError = Exception # type: ignore[assignment,misc]
|
|
12
|
+
|
|
13
|
+
_LUA_SCRIPT = """
|
|
14
|
+
local window_key = KEYS[1]
|
|
15
|
+
local capacity = tonumber(ARGV[1])
|
|
16
|
+
local ttl = tonumber(ARGV[2])
|
|
17
|
+
local count = redis.call('INCR', window_key)
|
|
18
|
+
if count == 1 then
|
|
19
|
+
redis.call('EXPIRE', window_key, ttl)
|
|
20
|
+
end
|
|
21
|
+
return count <= capacity and 1 or 0
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FixedWindowRateLimiter(BaseRateLimiter):
|
|
26
|
+
"""Fastest algorithm. Counts requests per fixed time slot.
|
|
27
|
+
|
|
28
|
+
Known caveat: allows 2× capacity at window boundaries.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
capacity: int,
|
|
34
|
+
fill_rate: float,
|
|
35
|
+
scope: str = "user",
|
|
36
|
+
backend: str = "memory",
|
|
37
|
+
redis_client: Optional[Any] = None,
|
|
38
|
+
project: Optional[str] = None,
|
|
39
|
+
prefix: Optional[str] = None,
|
|
40
|
+
suffix: Optional[str] = None,
|
|
41
|
+
fail_open: bool = False,
|
|
42
|
+
):
|
|
43
|
+
super().__init__(
|
|
44
|
+
capacity,
|
|
45
|
+
fill_rate,
|
|
46
|
+
scope,
|
|
47
|
+
backend,
|
|
48
|
+
redis_client,
|
|
49
|
+
project,
|
|
50
|
+
prefix,
|
|
51
|
+
suffix,
|
|
52
|
+
fail_open,
|
|
53
|
+
)
|
|
54
|
+
self._window = 1 / fill_rate
|
|
55
|
+
self._ttl = int(self._window * 2) + 60
|
|
56
|
+
|
|
57
|
+
def allow_request(self, identifier: Optional[str] = None) -> bool:
|
|
58
|
+
"""Return True if the request count for the current window slot is below capacity."""
|
|
59
|
+
key = self.get_key(identifier)
|
|
60
|
+
now = time.time()
|
|
61
|
+
slot = int(now / self._window)
|
|
62
|
+
if self.backend == "redis":
|
|
63
|
+
try:
|
|
64
|
+
window_key = f"{key}:{slot}"
|
|
65
|
+
result = self._redis.eval(
|
|
66
|
+
_LUA_SCRIPT, 1, window_key, self.capacity, self._ttl
|
|
67
|
+
)
|
|
68
|
+
return bool(result)
|
|
69
|
+
except _RedisError:
|
|
70
|
+
if self.fail_open:
|
|
71
|
+
return True
|
|
72
|
+
return self._allow_memory(key, slot)
|
|
73
|
+
|
|
74
|
+
def _allow_memory(self, key: str, slot: int) -> bool:
|
|
75
|
+
"""In-memory fixed window logic protected by a per-key lock."""
|
|
76
|
+
with self._get_key_lock(key):
|
|
77
|
+
data = self._get_from_backend(key)
|
|
78
|
+
if data is None or int(data["slot"]) < slot:
|
|
79
|
+
self._set_to_backend(key, {"count": 1, "slot": slot}, self._ttl)
|
|
80
|
+
return True
|
|
81
|
+
if data["count"] < self.capacity:
|
|
82
|
+
self._set_to_backend(
|
|
83
|
+
key, {"count": data["count"] + 1, "slot": slot}, self._ttl
|
|
84
|
+
)
|
|
85
|
+
return True
|
|
86
|
+
return False
|
|
87
|
+
|
|
88
|
+
def get_wait_time(self, identifier: Optional[str] = None) -> float:
|
|
89
|
+
"""Return seconds until the current window slot resets (0 if requests are allowed)."""
|
|
90
|
+
key = self.get_key(identifier)
|
|
91
|
+
now = time.time()
|
|
92
|
+
slot = int(now / self._window)
|
|
93
|
+
if self.backend == "redis":
|
|
94
|
+
try:
|
|
95
|
+
raw = self._redis.get(f"{key}:{slot}")
|
|
96
|
+
count = int(raw) if raw else 0
|
|
97
|
+
except Exception: # pylint: disable=broad-except
|
|
98
|
+
return 0.0
|
|
99
|
+
else:
|
|
100
|
+
data = self._get_from_backend(key)
|
|
101
|
+
count = int(data["count"]) if (data and int(data["slot"]) == slot) else 0
|
|
102
|
+
if count < self.capacity:
|
|
103
|
+
return 0.0
|
|
104
|
+
return self._window - (now % self._window)
|
|
105
|
+
|
|
106
|
+
def reset(self, identifier: Optional[str] = None) -> None:
|
|
107
|
+
"""Reset counters. For Redis, deletes the window-specific slot key."""
|
|
108
|
+
key = self.get_key(identifier)
|
|
109
|
+
if self.backend == "redis":
|
|
110
|
+
slot = int(time.time() / self._window)
|
|
111
|
+
self._redis.delete(f"{key}:{slot}")
|
|
112
|
+
else:
|
|
113
|
+
self._delete_from_backend(key)
|
|
114
|
+
|
|
115
|
+
def get_status(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
116
|
+
"""Return request count and remaining capacity for the current window slot."""
|
|
117
|
+
key = self.get_key(identifier)
|
|
118
|
+
now = time.time()
|
|
119
|
+
slot = int(now / self._window)
|
|
120
|
+
if self.backend == "redis":
|
|
121
|
+
try:
|
|
122
|
+
raw = self._redis.get(f"{key}:{slot}")
|
|
123
|
+
count = int(raw) if raw else 0
|
|
124
|
+
except _RedisError:
|
|
125
|
+
count = 0
|
|
126
|
+
else:
|
|
127
|
+
_, _, data = self._get_state(identifier)
|
|
128
|
+
count = int(data["count"]) if (data and int(data["slot"]) == slot) else 0
|
|
129
|
+
return {
|
|
130
|
+
"count": count,
|
|
131
|
+
"capacity": self.capacity,
|
|
132
|
+
"available": max(0, self.capacity - count),
|
|
133
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Leaky bucket rate limiter: smooths traffic by draining at a constant rate."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Dict, Optional
|
|
5
|
+
|
|
6
|
+
from ..base import BaseRateLimiter
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from redis import RedisError as _RedisError
|
|
10
|
+
except ImportError:
|
|
11
|
+
_RedisError = Exception # type: ignore[assignment,misc]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
_LUA_SCRIPT = """
|
|
15
|
+
local key = KEYS[1]
|
|
16
|
+
local capacity = tonumber(ARGV[1])
|
|
17
|
+
local fill_rate = tonumber(ARGV[2])
|
|
18
|
+
local now = tonumber(ARGV[3])
|
|
19
|
+
local ttl = tonumber(ARGV[4])
|
|
20
|
+
local data = redis.call('GET', key)
|
|
21
|
+
local level, last
|
|
22
|
+
if data then
|
|
23
|
+
local s = cjson.decode(data)
|
|
24
|
+
level = tonumber(s.level)
|
|
25
|
+
last = tonumber(s.last)
|
|
26
|
+
else
|
|
27
|
+
level = 0
|
|
28
|
+
last = now
|
|
29
|
+
end
|
|
30
|
+
level = math.max(0, level - (now - last) * fill_rate)
|
|
31
|
+
local allowed = 0
|
|
32
|
+
if level + 1 <= capacity then
|
|
33
|
+
level = level + 1
|
|
34
|
+
allowed = 1
|
|
35
|
+
end
|
|
36
|
+
redis.call('SETEX', key, ttl, cjson.encode({level=level, last=now}))
|
|
37
|
+
return {allowed, level}
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class LeakyBucketLimiter(BaseRateLimiter):
|
|
42
|
+
"""Smooths traffic: requests drain at fill_rate/sec, bursts are absorbed up to capacity."""
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
capacity: int,
|
|
47
|
+
fill_rate: float,
|
|
48
|
+
scope: str = "user",
|
|
49
|
+
backend: str = "memory",
|
|
50
|
+
redis_client: Optional[Any] = None,
|
|
51
|
+
project: Optional[str] = None,
|
|
52
|
+
prefix: Optional[str] = None,
|
|
53
|
+
suffix: Optional[str] = None,
|
|
54
|
+
fail_open: bool = False,
|
|
55
|
+
):
|
|
56
|
+
super().__init__(
|
|
57
|
+
capacity,
|
|
58
|
+
fill_rate,
|
|
59
|
+
scope,
|
|
60
|
+
backend,
|
|
61
|
+
redis_client,
|
|
62
|
+
project,
|
|
63
|
+
prefix,
|
|
64
|
+
suffix,
|
|
65
|
+
fail_open,
|
|
66
|
+
)
|
|
67
|
+
self._ttl = int((capacity / fill_rate) * 2) + 60
|
|
68
|
+
|
|
69
|
+
def _current_level(self, data: Any, now: float) -> float:
|
|
70
|
+
return max(0.0, float(data["level"]) - (now - float(data["last"])) * self.fill_rate)
|
|
71
|
+
|
|
72
|
+
def allow_request(self, identifier: Optional[str] = None) -> bool:
|
|
73
|
+
"""Return True if the bucket has room; add one unit and return False otherwise."""
|
|
74
|
+
key = self.get_key(identifier)
|
|
75
|
+
if self.backend == "redis":
|
|
76
|
+
try:
|
|
77
|
+
result = self._redis.eval(
|
|
78
|
+
_LUA_SCRIPT,
|
|
79
|
+
1,
|
|
80
|
+
key,
|
|
81
|
+
self.capacity,
|
|
82
|
+
self.fill_rate,
|
|
83
|
+
time.time(),
|
|
84
|
+
self._ttl,
|
|
85
|
+
)
|
|
86
|
+
return bool(result[0])
|
|
87
|
+
except _RedisError:
|
|
88
|
+
if self.fail_open:
|
|
89
|
+
return True
|
|
90
|
+
return self._allow_memory(key)
|
|
91
|
+
|
|
92
|
+
def _allow_memory(self, key: str) -> bool:
|
|
93
|
+
"""In-memory leaky bucket logic protected by a per-key lock."""
|
|
94
|
+
now = time.time()
|
|
95
|
+
with self._get_key_lock(key):
|
|
96
|
+
data = self._get_from_backend(key)
|
|
97
|
+
if data is None:
|
|
98
|
+
self._set_to_backend(key, {"level": 1.0, "last": now}, self._ttl)
|
|
99
|
+
return True
|
|
100
|
+
level = self._current_level(data, now)
|
|
101
|
+
if level + 1 <= self.capacity:
|
|
102
|
+
self._set_to_backend(key, {"level": level + 1, "last": now}, self._ttl)
|
|
103
|
+
return True
|
|
104
|
+
self._set_to_backend(key, {"level": level, "last": now}, self._ttl)
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
def get_wait_time(self, identifier: Optional[str] = None) -> float:
|
|
108
|
+
"""Return seconds until the bucket drains enough to accept a request."""
|
|
109
|
+
_, now, data = self._get_state(identifier)
|
|
110
|
+
if data is None:
|
|
111
|
+
return 0.0
|
|
112
|
+
level = self._current_level(data, now)
|
|
113
|
+
if level + 1 <= self.capacity:
|
|
114
|
+
return 0.0
|
|
115
|
+
return max(0.0, (level + 1 - self.capacity) / self.fill_rate)
|
|
116
|
+
|
|
117
|
+
def get_status(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
118
|
+
"""Return current water level and available capacity for the given identifier."""
|
|
119
|
+
_, now, data = self._get_state(identifier)
|
|
120
|
+
if data is None:
|
|
121
|
+
return {"water_level": 0.0, "capacity": self.capacity, "available": float(self.capacity)}
|
|
122
|
+
level = self._current_level(data, now)
|
|
123
|
+
return {
|
|
124
|
+
"water_level": round(level, 2),
|
|
125
|
+
"capacity": self.capacity,
|
|
126
|
+
"available": round(max(0.0, self.capacity - level), 2),
|
|
127
|
+
"utilization_pct": round((level / self.capacity) * 100, 1),
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
def get_usage(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
131
|
+
"""Return usage information in a consistent format."""
|
|
132
|
+
_, now, data = self._get_state(identifier)
|
|
133
|
+
water_level = self._current_level(data, now) if data is not None else 0.0
|
|
134
|
+
return {
|
|
135
|
+
"count": int(water_level),
|
|
136
|
+
"limit": self.capacity,
|
|
137
|
+
"remaining": max(0, int(self.capacity - water_level)),
|
|
138
|
+
"limited": water_level + 1 > self.capacity,
|
|
139
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Sliding window rate limiter: weighted count prevents boundary bursts."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Dict, Optional
|
|
5
|
+
|
|
6
|
+
from ..base import BaseRateLimiter
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from redis import RedisError as _RedisError
|
|
10
|
+
except ImportError:
|
|
11
|
+
_RedisError = Exception # type: ignore[assignment,misc]
|
|
12
|
+
|
|
13
|
+
_LUA_SCRIPT = """
|
|
14
|
+
local key = KEYS[1]
|
|
15
|
+
local capacity = tonumber(ARGV[1])
|
|
16
|
+
local window = tonumber(ARGV[2])
|
|
17
|
+
local now = tonumber(ARGV[3])
|
|
18
|
+
local ttl = tonumber(ARGV[4])
|
|
19
|
+
local data = redis.call('GET', key)
|
|
20
|
+
local slot = math.floor(now / window)
|
|
21
|
+
local curr, prev, stored_slot
|
|
22
|
+
if data then
|
|
23
|
+
local s = cjson.decode(data)
|
|
24
|
+
curr = tonumber(s.curr)
|
|
25
|
+
prev = tonumber(s.prev)
|
|
26
|
+
stored_slot = tonumber(s.slot)
|
|
27
|
+
else
|
|
28
|
+
curr = 0
|
|
29
|
+
prev = 0
|
|
30
|
+
stored_slot = slot
|
|
31
|
+
end
|
|
32
|
+
if stored_slot < slot then
|
|
33
|
+
prev = (stored_slot == slot - 1) and curr or 0
|
|
34
|
+
curr = 0
|
|
35
|
+
stored_slot = slot
|
|
36
|
+
end
|
|
37
|
+
local elapsed_pct = (now % window) / window
|
|
38
|
+
local weighted = curr + (1 - elapsed_pct) * prev
|
|
39
|
+
local allowed = 0
|
|
40
|
+
if weighted < capacity then
|
|
41
|
+
curr = curr + 1
|
|
42
|
+
allowed = 1
|
|
43
|
+
end
|
|
44
|
+
redis.call('SETEX', key, ttl, cjson.encode({slot=stored_slot, curr=curr, prev=prev}))
|
|
45
|
+
return allowed
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class SlidingWindowRateLimiter(BaseRateLimiter):
|
|
50
|
+
"""Best general-purpose choice: fast and prevents boundary bursts via weighted count."""
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
capacity: int,
|
|
55
|
+
fill_rate: float,
|
|
56
|
+
scope: str = "user",
|
|
57
|
+
backend: str = "memory",
|
|
58
|
+
redis_client: Optional[Any] = None,
|
|
59
|
+
project: Optional[str] = None,
|
|
60
|
+
prefix: Optional[str] = None,
|
|
61
|
+
suffix: Optional[str] = None,
|
|
62
|
+
fail_open: bool = False,
|
|
63
|
+
):
|
|
64
|
+
super().__init__(
|
|
65
|
+
capacity,
|
|
66
|
+
fill_rate,
|
|
67
|
+
scope,
|
|
68
|
+
backend,
|
|
69
|
+
redis_client,
|
|
70
|
+
project,
|
|
71
|
+
prefix,
|
|
72
|
+
suffix,
|
|
73
|
+
fail_open,
|
|
74
|
+
)
|
|
75
|
+
self._window = 1 / fill_rate
|
|
76
|
+
self._ttl = int(self._window * 3) + 60
|
|
77
|
+
|
|
78
|
+
def _resolve_window(self, data: Optional[Any], now: float) -> "tuple[int, int, int, float]":
|
|
79
|
+
"""Return (slot, curr, prev, weighted_count) with slot-advance applied."""
|
|
80
|
+
slot = int(now / self._window)
|
|
81
|
+
if data is None:
|
|
82
|
+
return slot, 0, 0, 0.0
|
|
83
|
+
stored_slot = int(data["slot"])
|
|
84
|
+
curr = int(data["curr"])
|
|
85
|
+
prev = int(data["prev"])
|
|
86
|
+
if stored_slot < slot:
|
|
87
|
+
prev = curr if stored_slot == slot - 1 else 0
|
|
88
|
+
curr = 0
|
|
89
|
+
elapsed_pct = (now % self._window) / self._window
|
|
90
|
+
return slot, curr, prev, curr + (1 - elapsed_pct) * prev
|
|
91
|
+
|
|
92
|
+
def allow_request(self, identifier: Optional[str] = None) -> bool:
|
|
93
|
+
"""Return True if the weighted request count is below capacity."""
|
|
94
|
+
key = self.get_key(identifier)
|
|
95
|
+
now = time.time()
|
|
96
|
+
if self.backend == "redis":
|
|
97
|
+
try:
|
|
98
|
+
result = self._redis.eval(
|
|
99
|
+
_LUA_SCRIPT, 1, key, self.capacity, self._window, now, self._ttl
|
|
100
|
+
)
|
|
101
|
+
return bool(result)
|
|
102
|
+
except _RedisError:
|
|
103
|
+
if self.fail_open:
|
|
104
|
+
return True
|
|
105
|
+
return self._allow_memory(key, now)
|
|
106
|
+
|
|
107
|
+
def _allow_memory(self, key: str, now: float) -> bool:
|
|
108
|
+
"""In-memory sliding window logic protected by a per-key lock."""
|
|
109
|
+
with self._get_key_lock(key):
|
|
110
|
+
data = self._get_from_backend(key)
|
|
111
|
+
slot, curr, prev, weighted = self._resolve_window(data, now)
|
|
112
|
+
allowed = weighted < self.capacity
|
|
113
|
+
if allowed:
|
|
114
|
+
curr += 1
|
|
115
|
+
self._set_to_backend(key, {"slot": slot, "curr": curr, "prev": prev}, self._ttl)
|
|
116
|
+
return allowed
|
|
117
|
+
|
|
118
|
+
def get_wait_time(self, identifier: Optional[str] = None) -> float:
|
|
119
|
+
"""Return seconds until the weighted count will drop below capacity (0 if allowed)."""
|
|
120
|
+
_, now, data = self._get_state(identifier)
|
|
121
|
+
if data is None:
|
|
122
|
+
return 0.0
|
|
123
|
+
_, curr, prev, weighted = self._resolve_window(data, now)
|
|
124
|
+
if weighted < self.capacity:
|
|
125
|
+
return 0.0
|
|
126
|
+
if prev > 0 and curr < self.capacity:
|
|
127
|
+
needed_elapsed = self._window * (1 - (self.capacity - curr) / prev)
|
|
128
|
+
wait = needed_elapsed - (now % self._window)
|
|
129
|
+
if wait > 0:
|
|
130
|
+
return wait
|
|
131
|
+
return self._window - (now % self._window)
|
|
132
|
+
|
|
133
|
+
def get_status(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
134
|
+
"""Return weighted request count and remaining capacity."""
|
|
135
|
+
_, now, data = self._get_state(identifier)
|
|
136
|
+
if data is None:
|
|
137
|
+
return {"count": 0, "capacity": self.capacity, "available": self.capacity}
|
|
138
|
+
_, _, _, weighted = self._resolve_window(data, now)
|
|
139
|
+
weighted_int = int(weighted)
|
|
140
|
+
return {
|
|
141
|
+
"count": weighted_int,
|
|
142
|
+
"capacity": self.capacity,
|
|
143
|
+
"available": max(0, self.capacity - weighted_int),
|
|
144
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"""Token bucket rate limiter: allows bursts up to capacity, then throttles."""
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Dict, Optional
|
|
5
|
+
|
|
6
|
+
from ..base import BaseRateLimiter
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
from redis import RedisError as _RedisError
|
|
10
|
+
except ImportError:
|
|
11
|
+
_RedisError = Exception # type: ignore[assignment,misc]
|
|
12
|
+
|
|
13
|
+
_LUA_SCRIPT = """
|
|
14
|
+
local key = KEYS[1]
|
|
15
|
+
local capacity = tonumber(ARGV[1])
|
|
16
|
+
local fill_rate = tonumber(ARGV[2])
|
|
17
|
+
local now = tonumber(ARGV[3])
|
|
18
|
+
local ttl = tonumber(ARGV[4])
|
|
19
|
+
local data = redis.call('GET', key)
|
|
20
|
+
local tokens, last_fill
|
|
21
|
+
if data then
|
|
22
|
+
local s = cjson.decode(data)
|
|
23
|
+
tokens = tonumber(s.tokens)
|
|
24
|
+
last_fill = tonumber(s.last_fill)
|
|
25
|
+
tokens = math.min(capacity, tokens + (now - last_fill) * fill_rate)
|
|
26
|
+
else
|
|
27
|
+
redis.call('SETEX', key, ttl, cjson.encode({tokens=capacity-1, last_fill=now}))
|
|
28
|
+
return 1
|
|
29
|
+
end
|
|
30
|
+
local allowed = 0
|
|
31
|
+
if tokens >= 1 then
|
|
32
|
+
tokens = tokens - 1
|
|
33
|
+
allowed = 1
|
|
34
|
+
end
|
|
35
|
+
redis.call('SETEX', key, ttl, cjson.encode({tokens=tokens, last_fill=now}))
|
|
36
|
+
return allowed
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TokenBucketLimiter(BaseRateLimiter):
|
|
41
|
+
"""Allows bursts up to capacity, then throttles to fill_rate tokens/sec."""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
capacity: int,
|
|
46
|
+
fill_rate: float,
|
|
47
|
+
scope: str = "user",
|
|
48
|
+
backend: str = "memory",
|
|
49
|
+
redis_client: Optional[Any] = None,
|
|
50
|
+
project: Optional[str] = None,
|
|
51
|
+
prefix: Optional[str] = None,
|
|
52
|
+
suffix: Optional[str] = None,
|
|
53
|
+
fail_open: bool = False,
|
|
54
|
+
):
|
|
55
|
+
super().__init__(
|
|
56
|
+
capacity,
|
|
57
|
+
fill_rate,
|
|
58
|
+
scope,
|
|
59
|
+
backend,
|
|
60
|
+
redis_client,
|
|
61
|
+
project,
|
|
62
|
+
prefix,
|
|
63
|
+
suffix,
|
|
64
|
+
fail_open,
|
|
65
|
+
)
|
|
66
|
+
self._ttl = int((capacity / fill_rate) * 2) + 60
|
|
67
|
+
|
|
68
|
+
def _current_tokens(self, data: Any, now: float) -> float:
|
|
69
|
+
return min(
|
|
70
|
+
self.capacity,
|
|
71
|
+
float(data["tokens"]) + (now - float(data["last_fill"])) * self.fill_rate,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def allow_request(self, identifier: Optional[str] = None) -> bool:
|
|
75
|
+
"""Return True if a token is available; consume it and return False otherwise."""
|
|
76
|
+
key = self.get_key(identifier)
|
|
77
|
+
now = time.time()
|
|
78
|
+
if self.backend == "redis":
|
|
79
|
+
try:
|
|
80
|
+
result = self._redis.eval(
|
|
81
|
+
_LUA_SCRIPT, 1, key, self.capacity, self.fill_rate, now, self._ttl
|
|
82
|
+
)
|
|
83
|
+
return bool(result)
|
|
84
|
+
except _RedisError:
|
|
85
|
+
if self.fail_open:
|
|
86
|
+
return True
|
|
87
|
+
return self._allow_memory(key, now)
|
|
88
|
+
|
|
89
|
+
def _allow_memory(self, key: str, now: float) -> bool:
|
|
90
|
+
"""In-memory token bucket logic protected by a per-key lock."""
|
|
91
|
+
with self._get_key_lock(key):
|
|
92
|
+
data = self._get_from_backend(key)
|
|
93
|
+
if data is None:
|
|
94
|
+
self._set_to_backend(
|
|
95
|
+
key, {"tokens": self.capacity - 1, "last_fill": now}, self._ttl
|
|
96
|
+
)
|
|
97
|
+
return True
|
|
98
|
+
tokens = self._current_tokens(data, now)
|
|
99
|
+
if tokens >= 1:
|
|
100
|
+
self._set_to_backend(
|
|
101
|
+
key, {"tokens": tokens - 1, "last_fill": now}, self._ttl
|
|
102
|
+
)
|
|
103
|
+
return True
|
|
104
|
+
self._set_to_backend(key, {"tokens": tokens, "last_fill": now}, self._ttl)
|
|
105
|
+
return False
|
|
106
|
+
|
|
107
|
+
def get_wait_time(self, identifier: Optional[str] = None) -> float:
|
|
108
|
+
"""Return seconds until the next token is available (0 if tokens remain)."""
|
|
109
|
+
_, now, data = self._get_state(identifier)
|
|
110
|
+
if data is None:
|
|
111
|
+
return 0.0
|
|
112
|
+
tokens = self._current_tokens(data, now)
|
|
113
|
+
return max(0.0, (1 - tokens) / self.fill_rate) if tokens < 1 else 0.0
|
|
114
|
+
|
|
115
|
+
def get_status(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
116
|
+
"""Return current token count and utilisation for the given identifier."""
|
|
117
|
+
_, now, data = self._get_state(identifier)
|
|
118
|
+
if data is None:
|
|
119
|
+
return {"tokens_remaining": self.capacity, "capacity": self.capacity}
|
|
120
|
+
tokens = self._current_tokens(data, now)
|
|
121
|
+
return {
|
|
122
|
+
"tokens_remaining": round(tokens, 2),
|
|
123
|
+
"capacity": self.capacity,
|
|
124
|
+
"utilization_pct": round((1 - tokens / self.capacity) * 100, 1),
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
def get_usage(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
128
|
+
"""Return usage information in a consistent format."""
|
|
129
|
+
_, now, data = self._get_state(identifier)
|
|
130
|
+
tokens = self._current_tokens(data, now) if data is not None else float(self.capacity)
|
|
131
|
+
return {
|
|
132
|
+
"count": int(self.capacity - tokens),
|
|
133
|
+
"limit": self.capacity,
|
|
134
|
+
"remaining": int(tokens),
|
|
135
|
+
"limited": tokens < 1,
|
|
136
|
+
}
|
limitra/base.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Base class for all limitra rate limiter implementations."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import Any, Dict, Optional, Tuple
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BaseRateLimiter(ABC):
|
|
11
|
+
"""Abstract base for all rate limiters.
|
|
12
|
+
|
|
13
|
+
Provides backend storage helpers, key generation, and thread-safe
|
|
14
|
+
per-key locking used by every concrete algorithm.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
capacity: int,
|
|
20
|
+
fill_rate: float,
|
|
21
|
+
scope: str = "user",
|
|
22
|
+
backend: str = "memory",
|
|
23
|
+
redis_client: Optional[Any] = None,
|
|
24
|
+
project: Optional[str] = None,
|
|
25
|
+
prefix: Optional[str] = None,
|
|
26
|
+
suffix: Optional[str] = None,
|
|
27
|
+
fail_open: bool = False,
|
|
28
|
+
):
|
|
29
|
+
if capacity <= 0:
|
|
30
|
+
raise ValueError("capacity must be positive")
|
|
31
|
+
if fill_rate <= 0:
|
|
32
|
+
raise ValueError("fill_rate must be positive")
|
|
33
|
+
if backend not in ("memory", "redis"):
|
|
34
|
+
raise ValueError("backend must be 'memory' or 'redis'")
|
|
35
|
+
if backend == "redis" and redis_client is None:
|
|
36
|
+
raise ValueError("redis_client is required when backend='redis'")
|
|
37
|
+
|
|
38
|
+
self.capacity = capacity
|
|
39
|
+
self.fill_rate = fill_rate
|
|
40
|
+
self.scope = scope
|
|
41
|
+
self.backend = backend
|
|
42
|
+
self.redis_client = redis_client
|
|
43
|
+
self.project = project
|
|
44
|
+
self.prefix = prefix
|
|
45
|
+
self.suffix = suffix
|
|
46
|
+
self.fail_open = fail_open
|
|
47
|
+
|
|
48
|
+
self._memory_storage: Dict[str, Any] = {}
|
|
49
|
+
self._lock = threading.Lock()
|
|
50
|
+
self._key_locks: Dict[str, threading.Lock] = {}
|
|
51
|
+
self._key_locks_lock = threading.Lock()
|
|
52
|
+
|
|
53
|
+
def _get_key_lock(self, key: str) -> threading.Lock:
|
|
54
|
+
"""Return a per-key Lock, creating it lazily."""
|
|
55
|
+
with self._key_locks_lock:
|
|
56
|
+
if key not in self._key_locks:
|
|
57
|
+
self._key_locks[key] = threading.Lock()
|
|
58
|
+
return self._key_locks[key]
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def _redis(self) -> Any:
|
|
62
|
+
"""Narrowed, non-optional access to redis_client (only valid when backend='redis')."""
|
|
63
|
+
assert self.redis_client is not None
|
|
64
|
+
return self.redis_client
|
|
65
|
+
|
|
66
|
+
def get_key(self, identifier: Optional[str] = None) -> str:
|
|
67
|
+
"""Build the storage key for the given identifier.
|
|
68
|
+
|
|
69
|
+
Format: [prefix:][ project:]scope[:identifier][:suffix]
|
|
70
|
+
"""
|
|
71
|
+
parts = []
|
|
72
|
+
if self.prefix:
|
|
73
|
+
parts.append(self.prefix)
|
|
74
|
+
if self.project:
|
|
75
|
+
parts.append(self.project)
|
|
76
|
+
parts.append(self.scope)
|
|
77
|
+
if self.scope != "global" and identifier is not None:
|
|
78
|
+
parts.append(str(identifier))
|
|
79
|
+
if self.suffix:
|
|
80
|
+
parts.append(self.suffix)
|
|
81
|
+
return ":".join(parts)
|
|
82
|
+
|
|
83
|
+
def _get_state(self, identifier: Optional[str]) -> Tuple[str, float, Any]:
|
|
84
|
+
"""Return (key, now, stored_data) — shared prelude for status/wait methods."""
|
|
85
|
+
key = self.get_key(identifier)
|
|
86
|
+
return key, time.time(), self._get_from_backend(key)
|
|
87
|
+
|
|
88
|
+
def _get_from_backend(self, key: str) -> Optional[Any]:
|
|
89
|
+
"""Fetch the stored value for key from the active backend."""
|
|
90
|
+
if self.backend == "memory":
|
|
91
|
+
with self._lock:
|
|
92
|
+
return self._memory_storage.get(key)
|
|
93
|
+
data = self._redis.get(key)
|
|
94
|
+
return json.loads(data) if data else None
|
|
95
|
+
|
|
96
|
+
def _set_to_backend(self, key: str, value: Any, ttl: Optional[int] = None) -> None:
|
|
97
|
+
"""Persist value for key in the active backend."""
|
|
98
|
+
if self.backend == "memory":
|
|
99
|
+
with self._lock:
|
|
100
|
+
self._memory_storage[key] = value
|
|
101
|
+
else:
|
|
102
|
+
self._redis.set(key, json.dumps(value))
|
|
103
|
+
if ttl:
|
|
104
|
+
self._redis.expire(key, int(ttl))
|
|
105
|
+
|
|
106
|
+
def _delete_from_backend(self, key: str) -> None:
|
|
107
|
+
"""Delete key from the active backend."""
|
|
108
|
+
if self.backend == "memory":
|
|
109
|
+
with self._lock:
|
|
110
|
+
self._memory_storage.pop(key, None)
|
|
111
|
+
else:
|
|
112
|
+
self._redis.delete(key)
|
|
113
|
+
|
|
114
|
+
@abstractmethod
|
|
115
|
+
def allow_request(self, identifier: Optional[str] = None) -> bool:
|
|
116
|
+
"""Return True if the request is allowed, False if the limit is exceeded."""
|
|
117
|
+
|
|
118
|
+
def reset(self, identifier: Optional[str] = None) -> None:
|
|
119
|
+
"""Reset rate limit counters for the given identifier."""
|
|
120
|
+
self._delete_from_backend(self.get_key(identifier))
|
|
121
|
+
|
|
122
|
+
def get_wait_time(self, _identifier: Optional[str] = None) -> float:
|
|
123
|
+
"""Return seconds until the next request is allowed (0 if allowed now)."""
|
|
124
|
+
return 0.0
|
|
125
|
+
|
|
126
|
+
def get_usage(self, identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
127
|
+
"""Return usage in a consistent format: count, limit, remaining, limited."""
|
|
128
|
+
status = self.get_status(identifier)
|
|
129
|
+
count: int = status["count"]
|
|
130
|
+
available: int = status["available"]
|
|
131
|
+
return {
|
|
132
|
+
"count": count,
|
|
133
|
+
"limit": self.capacity,
|
|
134
|
+
"remaining": available,
|
|
135
|
+
"limited": count >= self.capacity,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
def get_status(self, _identifier: Optional[str] = None) -> Dict[str, Any]:
|
|
139
|
+
"""Return algorithm-specific status for the given identifier."""
|
|
140
|
+
return {"count": 0, "capacity": self.capacity, "available": self.capacity}
|
|
141
|
+
|
|
142
|
+
def get_config(self) -> Dict[str, Any]:
|
|
143
|
+
"""Return a dictionary of the limiter's configuration."""
|
|
144
|
+
return {
|
|
145
|
+
"capacity": self.capacity,
|
|
146
|
+
"fill_rate": self.fill_rate,
|
|
147
|
+
"scope": self.scope,
|
|
148
|
+
"backend": self.backend,
|
|
149
|
+
"project": self.project,
|
|
150
|
+
"prefix": self.prefix,
|
|
151
|
+
"suffix": self.suffix,
|
|
152
|
+
}
|
limitra/exceptions.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Exceptions raised by limitra rate limiters."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RateLimitExceeded(Exception):
|
|
5
|
+
"""Raised when a rate limit is exceeded and no on_exceeded handler is set."""
|
|
6
|
+
|
|
7
|
+
def __init__(self, requests: int, window: float, retry_after: float = 0.0):
|
|
8
|
+
self.requests = requests
|
|
9
|
+
self.window = window
|
|
10
|
+
self.retry_after = retry_after
|
|
11
|
+
self.remaining = 0
|
|
12
|
+
msg = f"Rate limit exceeded: {requests} requests per {window}s"
|
|
13
|
+
if retry_after:
|
|
14
|
+
msg += f". Retry after {retry_after:.2f}s"
|
|
15
|
+
super().__init__(msg)
|
|
File without changes
|
limitra/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: limitra
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Simple, framework-agnostic rate limiting for Python — sliding window, token bucket, leaky bucket, fixed window, memory and Redis backends
|
|
5
|
+
Author-email: Léo Kling <contact@leok.dev>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/leo-kling/limitra
|
|
8
|
+
Project-URL: Repository, https://github.com/leo-kling/limitra
|
|
9
|
+
Project-URL: Issues, https://github.com/leo-kling/limitra/issues
|
|
10
|
+
Keywords: rate-limit,rate-limiter,rate-limiting,ratelimit,throttle,throttling,sliding-window,token-bucket,leaky-bucket,fixed-window,fastapi,django,flask,redis,api
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Classifier: Framework :: AsyncIO
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
<p align="center">
|
|
33
|
+
<img src="https://limitra.pages.dev/logo.png" alt="Limitra" width="120" />
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
<h1 align="center">Limitra</h1>
|
|
37
|
+
|
|
38
|
+
<p align="center">
|
|
39
|
+
<img src="https://github.com/leo-kling/limitra/actions/workflows/ci.yml/badge.svg" alt="CI" />
|
|
40
|
+
<img src="https://img.shields.io/badge/python-3.10%20|%203.11%20|%203.12%20|%203.13%20|%203.14-blue" alt="Python versions" />
|
|
41
|
+
<img src="https://img.shields.io/badge/license-MIT-lightgrey" alt="MIT license" />
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
<p align="center">Simple, framework-agnostic rate limiting for Python.</p>
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install limitra
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Quick start
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from limitra import LimitraConfig, rate_limit
|
|
58
|
+
|
|
59
|
+
LimitraConfig(redis_url="redis://localhost:6379", project="my-service")
|
|
60
|
+
|
|
61
|
+
@rate_limit(requests=10, window=60, key="user_id")
|
|
62
|
+
def create_post(user_id: str, content: str):
|
|
63
|
+
...
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
→ Full documentation: **[limitra.leok.dev](https://limitra.leok.dev)**
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
limitra/__init__.py,sha256=mJGJ9QJ4fpX6JEwKCRkC1Eml6iVp3SNSczIr15rTg1E,633
|
|
2
|
+
limitra/_config.py,sha256=3fCeOU16Ao1dAF3Dj1Qk4Als4l7d36vIFkcGkjGz8xY,2944
|
|
3
|
+
limitra/_decorator.py,sha256=-Ox-JsU15Jh3vFPaEGwx6H4bg72S_eIYnzjcLdT3Da4,11373
|
|
4
|
+
limitra/_version.py,sha256=8OsTLsIVB9D0HdPTmt5rVwyVUBe9xTVGkRslXicxzkM,520
|
|
5
|
+
limitra/base.py,sha256=NUsw2URLlnufU-W5HJ5J-FB29cwimuDK1gOfD6hnw1I,5639
|
|
6
|
+
limitra/exceptions.py,sha256=Fkyc04Nm7WbntyOSVc8tmXWSJpceKOkl0oO-fMeyh5k,560
|
|
7
|
+
limitra/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
limitra/algorithms/__init__.py,sha256=zH1c2Q0ywECpYh9tcIa875Kk22WLbWl_CQkMP6YwlsM,619
|
|
9
|
+
limitra/algorithms/fixed_window.py,sha256=EoliEXOmpdE8OtsBdUPk60cijYsEuSS0SyO0oYFjV4M,4625
|
|
10
|
+
limitra/algorithms/leaky_bucket.py,sha256=xJ8-7qbXpHM4IKYDhmiilyzSxDfJpG8Dxi_xAvkCvBM,4809
|
|
11
|
+
limitra/algorithms/sliding_window.py,sha256=U94yEHXj_tw4XShU8soKBX91ttzA7r2ulSZ1WqGFeT8,4938
|
|
12
|
+
limitra/algorithms/token_bucket.py,sha256=O2rt5HygMUAwmAzMwhwJ18fLVg2n7ZIvFNOMRJfsqzo,4761
|
|
13
|
+
limitra/integrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
+
limitra-0.0.1.dist-info/licenses/LICENSE,sha256=oK6JyCyjjebB4Mvy4PFdOfY8x50ilr07XzC0Wg2AH9I,1067
|
|
15
|
+
limitra-0.0.1.dist-info/METADATA,sha256=LE71rhL21YWGzMqaKhAQtm7PENjDEbmHWPxYrJ1uWII,2419
|
|
16
|
+
limitra-0.0.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
17
|
+
limitra-0.0.1.dist-info/top_level.txt,sha256=QmzUUCVtKj3s7E3wmm72Ms5B9TGTFJ5XH9-vBGvi5QM,8
|
|
18
|
+
limitra-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Léo Kling
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
limitra
|