ratelimiter_lite 0.1.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.
@@ -0,0 +1,7 @@
1
+ Copyright <2026> <JackCizon>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.4
2
+ Name: ratelimiter_lite
3
+ Version: 0.1.0
4
+ Summary: Rate Limit Components
5
+ License: MIT
6
+ License-File: LICENSE.txt
7
+ Author: jackcizon
8
+ Author-email: jack20021213cn@gmail.com
9
+ Requires-Python: >=3.12
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Requires-Dist: coverage (>=7.13.2,<8.0.0)
16
+ Requires-Dist: mypy (>=1.19.1,<2.0.0)
17
+ Requires-Dist: pre-commit (>=4.5.1,<5.0.0)
18
+ Requires-Dist: pytest (>=9.0.2,<10.0.0)
19
+ Requires-Dist: ruff (>=0.14.14,<0.15.0)
20
+ Requires-Dist: sphinx (>=9.1.0,<10.0.0)
21
+ Requires-Dist: sphinx-autobuild (>=2025.8.25,<2026.0.0)
@@ -0,0 +1,22 @@
1
+ [project]
2
+ name = "ratelimiter_lite"
3
+ version = "0.1.0"
4
+ description = "Rate Limit Components"
5
+ authors = [
6
+ { name = "jackcizon", email = "jack20021213cn@gmail.com" }
7
+ ]
8
+ license = { text = "MIT" }
9
+ requires-python = ">=3.12"
10
+ dependencies = [
11
+ "pytest (>=9.0.2,<10.0.0)",
12
+ "coverage (>=7.13.2,<8.0.0)",
13
+ "ruff (>=0.14.14,<0.15.0)",
14
+ "mypy (>=1.19.1,<2.0.0)",
15
+ "pre-commit (>=4.5.1,<5.0.0)",
16
+ "sphinx (>=9.1.0,<10.0.0)",
17
+ "sphinx-autobuild (>=2025.8.25,<2026.0.0)",
18
+ ]
19
+
20
+ [build-system]
21
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
22
+ build-backend = "poetry.core.masonry.api"
File without changes
@@ -0,0 +1,61 @@
1
+ class Baseratelimiter_lite: # pragma: no cover
2
+ """single-process rate limiting"""
3
+
4
+ def __init__(self, limit: int, period: float) -> None:
5
+ """
6
+ This is a state machine that maintains ratelimiter_lite invariants.
7
+
8
+ Rate limiter algorithms are widely used, but they are not difficult to implement.
9
+
10
+ The algorithm for a rate limiter is much simpler than that for a red-black tree,
11
+ if you have implemented a red-black tree before.
12
+ """
13
+ self._limit = limit
14
+ self._period = period
15
+
16
+ def allow(self) -> bool:
17
+ """
18
+ Do Not Use `__call__`.
19
+
20
+ e.g: A ratelimiter_lite prototype via `__call__`.
21
+
22
+ class ratelimiter_lite:
23
+ def __init__(self, func: Callable = None, limit=2, period=1.0):
24
+ self._func = func
25
+ self._counter = 0
26
+ self._last_reset = time.monotonic()
27
+ self._lock = threading.RLock()
28
+ self._limit = limit
29
+ self._period = period
30
+
31
+ def __call__(self, *args, **kwargs):
32
+ with self._lock:
33
+ now = time.monotonic()
34
+ if now - self._last_reset >= self._period:
35
+ self._counter = 0
36
+ self._last_reset = now
37
+ if self._counter >= self._limit:
38
+ print(">>> [Rate Limited!]")
39
+ raise Exception
40
+ self._counter += 1
41
+ return self._func(*args, **kwargs)
42
+
43
+
44
+ @ratelimiter_lite(limit=3, period=0.5)
45
+ def aaa(args):
46
+ print(args)
47
+ time.sleep(0.1)
48
+
49
+ issues:
50
+ 1. limiter params cannot change.
51
+ 2. where is the function?(can slove it by writing function into `__call__`)
52
+ 3. if write function into `__call__`, the code logic is bad.
53
+
54
+ If you cram all your algorithmic logic into `__call__`,
55
+ your code will become increasingly bloated.
56
+
57
+ click internal uses class to define Command, Parameter, etc....
58
+ then use a wrapper function, like commond(args, ...) factory,
59
+ used by decorator @commond()
60
+ """
61
+ raise NotImplementedError
@@ -0,0 +1,22 @@
1
+ from collections.abc import Callable
2
+ from functools import wraps
3
+ from typing import Any
4
+
5
+ from ratelimiter_lite.base import Baseratelimiter_lite
6
+
7
+
8
+ def ratelimiter_lite_factory[T: Baseratelimiter_lite](cls: type[T], limit: int, period: float) -> Callable:
9
+ limiter = cls(limit, period)
10
+
11
+ def decorator(func: Callable) -> Callable:
12
+ @wraps(func)
13
+ def wrapper(*args: Any, **kwargs: Any) -> Callable | None:
14
+ if limiter.allow():
15
+ return func(*args, **kwargs)
16
+ else:
17
+ print(f"[{func.__name__}] Rate limit exceeded")
18
+ return None
19
+
20
+ return wrapper
21
+
22
+ return decorator
@@ -0,0 +1,28 @@
1
+ import time
2
+ import threading
3
+
4
+ from ratelimiter_lite.base import Baseratelimiter_lite
5
+
6
+
7
+ class FixedWindowratelimiter_lite(Baseratelimiter_lite):
8
+ """only valid in"""
9
+
10
+ def __init__(self, limit: int, period: float):
11
+ super().__init__(limit, period)
12
+ self._counter = 0
13
+ self._last_reset = time.monotonic()
14
+ self._lock = threading.Lock()
15
+
16
+ def allow(self) -> bool:
17
+ with self._lock:
18
+ now = time.monotonic()
19
+
20
+ if now - self._last_reset >= self._period:
21
+ self._counter = 0
22
+ self._last_reset = now
23
+
24
+ if self._counter < self._limit:
25
+ self._counter += 1
26
+ return True
27
+
28
+ return False