A lightweight, modular rate limiting library for python
Project description
RateGuard Minimal Usage Guide
RateGuard is a framework-agnostic rate limiting library. While it includes a built-in decorator for FastAPI, its core RateLimiter class can be used in any Python framework (Flask, Django, Celery, or pure Python scripts).
1. Framework-Agnostic Usage (Any Python App)
You can use the core RateLimiter class directly anywhere in your code.
from rateguard import RateLimiter
# Create a limiter: Allow 100 requests every 60 seconds
limiter = RateLimiter(
max_retries=100,
ttl=60 #-----> total time limit
)
def some_action(user_id: str):
# Check the rate limit for this specific user
result = limiter.check(user_id)
if result["allowed"]:
print(f"Action allowed! Remaining requests: {result['remaining']}")
# Perform the action...
else:
print(f"Rate limited. Try again in {result['retry_after']} seconds")
# Return an error to the user
2. FastAPI Usage (Built-in Decorator)
If you are using FastAPI, RateGuard provides a convenient @limit decorator that automatically handles key resolution (like IP addresses or authenticated users) and throws standard 429 Too Many Requests HTTP errors.
from fastapi import FastAPI, Request
from rateguard import limit
app = FastAPI()
# Limit this endpoint to 5 requests per 60 seconds
@limit(max_retries=5, ttl=60)
def my_endpoint(request: Request):
return {"message": "Hello!"}
@app.get("/hello")
def hello_route(request: Request):
return my_endpoint(request)
Project details
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 requestguard-0.1.1.tar.gz.
File metadata
- Download URL: requestguard-0.1.1.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cc09d8e780f1ccef95ebd95933af872db19531fce26f733422a3b98d72650d1
|
|
| MD5 |
48998cc27b7753ea590c1e7a9acd498c
|
|
| BLAKE2b-256 |
6053c9a064bf27c85fc4dbafcde783b4f3e51a789719f06e88d177a0781d773a
|
File details
Details for the file requestguard-0.1.1-py3-none-any.whl.
File metadata
- Download URL: requestguard-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c81a8db0b21f0d56f58e0968b4759611c1bf63788fc62bd2027c99f3c5e33b4
|
|
| MD5 |
1348300c697095973c4f2123c22ba8e5
|
|
| BLAKE2b-256 |
4c2ff523bf31699632105ffe44b615ec0598d5688b30a1955619d60c0bcb2d19
|