A framework-agnostic rate limiter for Python backends (Flask, Django, FastAPI, etc.)
Project description
Documentation
Installation
pip install meow-redis-ratelimiter
Requires Python 3.7+. If using Redis, make sure you have it installed and running.
Basic Usage (In-Memory Store)
from redis-rate_limiter import RateLimiter
limiter = RateLimiter(max_requests=5, period=60)
@limiter
def my_function(req=None):
return "Allowed!"
# Example
print(my_function()) # Allowed
This version keeps counters in memory. Works for single-process apps (e.g., local dev, small deployments).
Usage with Redis (Distributed Store)
import redis
from redis-rate_limiter import RateLimiter
redis_client = redis.StrictRedis.from_url("redis://localhost:6379/0")
limiter = RateLimiter(
max_requests=5,
period=60,
redis_client=redis_client
)
@limiter
def my_function(req=None):
return "Allowed!"
Use this for production, since Redis synchronizes counters across workers/servers.
Framework Integration
Flask
from flask import Flask, request, jsonify
import redis
from redis-rate_limiter import RateLimiter
app = Flask(__name__)
redis_client = redis.StrictRedis.from_url("redis://localhost:6379/0")
limiter = RateLimiter(
max_requests=5,
period=10,
redis_client=redis_client,
identifier_func=lambda req: req.remote_addr,
error_handler=lambda: (jsonify({"error": "Too Many Requests"}), 429)
)
@app.route("/api/data")
@limiter
def data(req=None):
return {"message": "Success"}
Django
from django.http import JsonResponse
from redis-rate_limiter import RateLimiter
limiter = RateLimiter(
max_requests=5,
period=10,
identifier_func=lambda req: req.META.get("REMOTE_ADDR"),
error_handler=lambda: JsonResponse({"error": "Too Many Requests"}, status=429)
)
@limiter
def my_view(request):
return JsonResponse({"message": "Success"})
FastAPI
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import redis
from redis-rate_limiter import RateLimiter
app = FastAPI()
redis_client = redis.StrictRedis.from_url("redis://localhost:6379/0")
limiter = RateLimiter(
max_requests=5,
period=10,
redis_client=redis_client,
identifier_func=lambda req: req.client.host,
error_handler=lambda: JSONResponse({"error": "Too Many Requests"}, status_code=429)
)
@app.get("/api/data")
@limiter
async def data(request: Request):
return {"message": "Success"}
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
max_requests |
int |
5 |
Max requests allowed in period |
period |
int |
60 |
Time window in seconds |
redis_client |
redis.Redis or None |
None |
Redis client for distributed limit |
identifier_func |
Callable |
lambda req: "global" |
Extracts a unique identifier (IP, user_id, API key) |
error_handler |
Callable |
returns (msg, 429) |
Handles blocked requests (custom error response) |
How It Works
-
Keeps a sliding window of request timestamps.
-
On each request:
- Expired timestamps are removed.
- If count ≥
max_requests, request is blocked. - Otherwise, request is allowed.
-
With Redis, counters are shared across all workers.
Example with API Key Throttling
limiter = RateLimiter(
max_requests=100,
period=60,
identifier_func=lambda req: req.headers.get("X-API-Key", "anonymous"),
error_handler=lambda: {"error": "Too Many Requests"}, 429
)
Roadmap
- Async support for asyncio/Starlette
- Token bucket mode
- Middleware for direct integration (Flask, Django, FastAPI)
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 meow_redis_ratelimiter-0.1.0.tar.gz.
File metadata
- Download URL: meow_redis_ratelimiter-0.1.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd1dae668d628b41990993626b291039d61d3eac30e0af8a0771bd501a1a3110
|
|
| MD5 |
e219ee3af3d0aafc264a69bebd127d88
|
|
| BLAKE2b-256 |
169e8b5f39466fb64ec5796aa27474cba9b1603c54145256985c16b8fcd0fcf0
|
File details
Details for the file meow_redis_ratelimiter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: meow_redis_ratelimiter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e47a40e235ddff7c9402530e9fdc26b1953c8f1528276372a7ff9d995fee84b7
|
|
| MD5 |
3c85a51797977cf363d37a33ae338407
|
|
| BLAKE2b-256 |
b6b0ae2c28cd1d64f02d5e4f506bde6b7485b850175967e0c99c00065736df9d
|