limitra 1.0.0__tar.gz

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-1.0.0/LICENSE ADDED
@@ -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.
limitra-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,66 @@
1
+ Metadata-Version: 2.4
2
+ Name: limitra
3
+ Version: 1.0.0
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="docs/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/python-package.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 configure, rate_limit
58
+
59
+ configure(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,35 @@
1
+ <p align="center">
2
+ <img src="docs/logo.png" alt="Limitra" width="120" />
3
+ </p>
4
+
5
+ <h1 align="center">Limitra</h1>
6
+
7
+ <p align="center">
8
+ <img src="https://github.com/leo-kling/limitra/actions/workflows/python-package.yml/badge.svg" alt="CI" />
9
+ <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" />
10
+ <img src="https://img.shields.io/badge/license-MIT-lightgrey" alt="MIT license" />
11
+ </p>
12
+
13
+ <p align="center">Simple, framework-agnostic rate limiting for Python.</p>
14
+
15
+ ---
16
+
17
+ ## Install
18
+
19
+ ```bash
20
+ pip install limitra
21
+ ```
22
+
23
+ ## Quick start
24
+
25
+ ```python
26
+ from limitra import configure, rate_limit
27
+
28
+ configure(redis_url="redis://localhost:6379", project="my-service")
29
+
30
+ @rate_limit(requests=10, window=60, key="user_id")
31
+ def create_post(user_id: str, content: str):
32
+ ...
33
+ ```
34
+
35
+ → Full documentation: **[limitra.leok.dev](https://limitra.leok.dev)**
@@ -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
+ __version__ = "1.0.0"
16
+
17
+ __all__ = [
18
+ "LimitraConfig",
19
+ "rate_limit",
20
+ "RateLimitExceeded",
21
+ "BaseRateLimiter",
22
+ "TokenBucketLimiter",
23
+ "LeakyBucketLimiter",
24
+ "FixedWindowRateLimiter",
25
+ "SlidingWindowRateLimiter",
26
+ "LIMITERS",
27
+ ]
@@ -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()
@@ -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 configure() 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
@@ -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
+ }