A simple and lightweight rate limiter decorator for Python
Project description
SLIDEGUARD
A small, open-source Python rate limiter.
slideguard provides a thread-safe decorator for limiting function calls in a rolling time window, plus a low-level helper for custom workflows.
Features
- Simple
@rate_limit(calls=N, per=seconds)decorator - Per-function in-memory timestamp tracking
- Thread-safe enforcement using
threading.Lock - Custom
RateLimitExceededexception - Low-level
is_allowed(...)helper
Requirements
- Python 3.9+
Installation
Install from PyPI:
pip install slideguard
Install locally from source:
git clone https://github.com/mujtabaalmas/slideguard.git
cd slideguard
pip install .
For development (editable install):
pip install -r requirements.txt
pip install -e .
Quick Start
from slideguard import RateLimitExceeded, rate_limit
@rate_limit(calls=5, per=60)
def send_event(payload: dict) -> str:
return f"sent {payload['id']}"
for i in range(6):
try:
print(send_event({"id": i}))
except RateLimitExceeded as exc:
print(f"blocked: {exc}")
Usage
Decorator API
from slideguard import rate_limit
@rate_limit(calls=10, per=60)
def process_job(job_id: str) -> str:
return f"processed {job_id}"
If the call limit is exceeded in the current window, RateLimitExceeded is raised.
Exception Handling
from slideguard import RateLimitExceeded, rate_limit
@rate_limit(calls=2, per=5)
def ping() -> str:
return "pong"
try:
print(ping())
except RateLimitExceeded:
print("Too many requests. Try again soon.")
Low-Level Helper (is_allowed)
Use this when you need custom control outside the decorator.
import time
from slideguard import is_allowed
calls: list[float] = []
allowed, calls = is_allowed(calls, max_calls=3, window=10)
if allowed:
calls.append(time.time())
print("accepted")
else:
print("blocked")
is_allowed removes expired timestamps from the provided list and returns (allowed, cleaned_calls).
Behavior Notes
- Limits are per decorated function (closure-based state).
- Storage is in-memory and process-local.
- The decorator is thread-safe for concurrent calls in one process.
Development
Run tests:
python -m pytest -q
Contributing
Contributions are welcome.
- Fork the repository.
- Create a feature branch.
- Add or update tests.
- Open a pull request.
License
MIT. See the LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slideguard-0.1.2.tar.gz.
File metadata
- Download URL: slideguard-0.1.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
501fc7cceb2cb388bba9fc54d890a0a8825aeb8dee054d719a47927f27f58c9e
|
|
| MD5 |
fe0e1483df1096a7ce071c9120b84875
|
|
| BLAKE2b-256 |
a666997079e5eea86ff96f6fb51fa1082a8cc8c18192cd60ef78c54947f453f5
|
File details
Details for the file slideguard-0.1.2-py3-none-any.whl.
File metadata
- Download URL: slideguard-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20599481e6b38bdb6e8d88bbeff7a69001d644e65816de46c557489f36ee06c8
|
|
| MD5 |
55bd44f175101e0e6b843ddd58823709
|
|
| BLAKE2b-256 |
6e4ee389a592fffff8293680a5babecd2de86393b7a30e9e0b76695b8d4af251
|