Redis transaction based rate limiter
Project description
PyRedLight
Rate limiting for Python with fast Redis transactions.
Simply put, instead of running multiple individual operations leading to multiple round-trips to your Redis server, we
execute a small transaction consisting of the following commands (for a limit of 60/10s
)
SET key 60 EX 10 NX # Create key and set expiration time, if it does not exist
DECR key # Decrement value and return
TTL key # Return time until key expires
Then from the results we parse how many requests you're still allowed to perform within the limit (if any), and what is the expiration time for the request window.
Also contains utilities and examples for convenient use with FastAPI.
Installation
It's a Python library, what do you expect?
pip install pyredlight
# OR
poetry add pyredlight
Usage
If you want to test the examples make sure you replace redis://your.redis.server
with an actual connect string.
Limits can be defined using the format {requests}/{time}[hms]
, so 100/10s
= 100 requests / 10 seconds, 60/1m
= 60 requests / minute, 5000/6h
= 5000 requests / 6 hours.
Small example of how you can use this library (also in example.py, test
with poetry install && poetry run python example.py
):
import asyncio
import redis.asyncio as redis
from pyredlight import limit, set_redis
requests_per_minute = limit("60/60s")
def get_key(request):
return f"rate_limit_example_{request['client_ip']}"
async def handle_request(request):
key = get_key(request)
ok, remaining, expires = await requests_per_minute.is_ok(key)
if not ok:
return {
"status": 429,
"rate_limit_remaining": remaining, # Always 0
"rate_limit_expires": expires,
}
else:
return {
"status": 200,
"rate_limit_remaining": remaining,
"rate_limit_expires": expires,
}
async def main():
for _ in range(10):
print(await handle_request({"client_ip": "127.0.0.1"}))
if __name__ == "__main__":
set_redis(redis.from_url("redis://your.redis.server"))
asyncio.run(main())
Or in FastAPI (also in fastapi_example, test
with cd fastapi_example && poetry install && poetry run python example.py
):
from fastapi import FastAPI, Request, APIRouter, Depends
from starlette.responses import JSONResponse
from pyredlight import limit, set_redis
from pyredlight.fastapi import make_depends
import redis.asyncio as redis
per_minute_limit = limit("60/60s")
def get_rate_limit_key(request: Request):
return request.client.host + "custom"
per_minute_depend = make_depends(per_minute_limit)
custom_key_example = make_depends(per_minute_limit, get_key=get_rate_limit_key)
router = APIRouter()
@router.get("/")
async def get_data(_=Depends(per_minute_depend)):
return JSONResponse({"status": "ok"})
@router.post("/")
async def set_data(_=Depends(custom_key_example)):
return JSONResponse({"status": "ok"})
app = FastAPI()
app.include_router(router)
@app.middleware("http")
async def rate_limit_headers(request: Request, call_next):
response = await call_next(request)
rate_limit_remaining = request.scope.get("rate_limit_remaining", None)
if rate_limit_remaining is not None:
response.headers["X-Rate-Limit-Remains"] = str(request.scope["rate_limit_remaining"])
response.headers["X-Rate-Limit-Expires"] = str(request.scope["rate_limit_expires"])
return response
@app.on_event("startup")
async def setup():
set_redis(redis.from_url("redis://127.0.0.1"))
Development
Issues and PRs are welcome!
Please open an issue first to discuss the idea before sending a PR so that you know if it would be wanted or needs re-thinking or if you should just make a fork for yourself.
For local development, make sure you install pre-commit, then run:
pre-commit install
poetry install
poetry run pytest
poetry run python example.py
cd fastapi_example
poetry run python example.py
License
The code is released under the BSD 3-Clause license. Details in the LICENSE.md file.
Financial support
This project has been made possible thanks to Cocreators and Lietu. You can help us continue our open source work by supporting us on Buy me a coffee.
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
Hashes for pyredlight-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d75ca84245327f5796a203dabeebcb36d4a6a60c5c26e4d3f63c0bad2a06991b |
|
MD5 | dffa4f5d75c20a079888eaab8a37c6c5 |
|
BLAKE2b-256 | 8ee4c1c64b32ec95f1ea31e5a9e743807ca099afcf1ec9edd92d6c39f807c4b2 |