A High performance framework-agnostic rate limiter for Django, FastAPI, and Flask
Project description
flux-limiter
Flux is a high-performance rate limiter for Python that just works. It's built on a C++ core to respect your latency budget, and it plugs straight into Django, FastAPI, and Flask.
Whether you're protecting a public API or just trying to stop a single user from spamming your login form, Flux handles it without the boilerplate.
Installation
pip install flux-limiter
or
uv add flux-limiter
(You'll need a Redis server running, but you probably already have one of those.)
How to use it
The easiest way is the decorator. It figures out what framework you're using and sends the right 429 response automatically.
from flux import rate_limit
# Allow 100 requests every minute
@rate_limit(requests=100, period=60)
def my_api_endpoint(request):
return {"data": "This is protected"}
That's it. No middleware to configure, no exception handlers to write.
Framework Examples
FastAPI
from fastapi import FastAPI, Request
from flux import rate_limit
app = FastAPI()
@app.get("/")
@rate_limit(requests=5, period=10)
async def root(request: Request):
return {"message": "Hello World"}
Django
from django.http import JsonResponse
from flux import rate_limit
@rate_limit(requests=20, period=60)
def my_view(request):
return JsonResponse({"ok": True})
Flask
from flask import Flask
from flux import rate_limit
app = Flask(__name__)
@app.route("/login")
@rate_limit(requests=5, period=300) # 5 tries every 5 minutes
def login():
return "Login Page"
Configuration
You don't need a config file to get started, but when you're ready to tweak things, run:
python -m flux.cli init
This generates a flux.toml file where you can adjust everything:
flux.toml Reference
The flux.toml file is where you configure the behavior of the rate limiter. Here are all the available options:
[redis]
host = "127.0.0.1"
port = 6379
pool_size = 10
timeout_ms = 200
[flux]
# Prefix for all keys stored in Redis
key_prefix = "flux:"
# If Redis is unreachable, fail_silently=true allows the request to proceed (Fail Open).
# fail_silently=false raises a ConnectionError (Fail Closed).
fail_silently = true
# Enable debug logging to stdout for development
console_logging = false
# Jitter adds random variance to the Retry-After header calculations.
# This prevents "thundering herd" issues where all clients retry at the exacte same millisecond.
jitter_enabled = true
jitter_max_ms = 500 # Max jitter in milliseconds
[rate_limit]
# Default policy for rate limiters that don't specify one
# Options: "gcra", "token_bucket", "leaky_bucket", "fixed_window"
policy = "gcra"
requests = 100
period = 60
Named Limits
Instead of hardcoding numbers in your code, you can define them in flux.toml:
[rate_limits.api_tier_1]
requests = 1000
period = 3600
policy = "gcra"
And then use them by name:
@rate_limit(name="api_tier_1")
def expensive_call():
pass
Why isn't this just Python?
Because speed matters. The core logic of Flux is written in C++ and communicates directly with Redis using optimized Lua scripts.
It supports multiple algorithms depending on your needs:
- GCRA: The default. Smooth, leaky-bucket style limiting. Great for APIs.
- Token Bucket: Allows for bursts (e.g., let users traverse a paginated list quickly) but enforces a long-term average.
- Fixed Window: Simple counters. Good for "N actions per day".
Read More
👉 Check out blog for more details
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
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 flux_limiter-0.1.15-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: flux_limiter-0.1.15-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 624.8 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f9cac6672af2bc3df1349a10e9a413b680fe3b506e47b7eb3deba584e5367dd
|
|
| MD5 |
1b4332d5c7eb5e9794400da6f359b9f1
|
|
| BLAKE2b-256 |
50703cc2c22ecfeb2390746f7b1fbacdb150ac0f8cc2d9da0d43e665932b8c40
|
File details
Details for the file flux_limiter-0.1.15-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: flux_limiter-0.1.15-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 420.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7daea09a7d7daa96ee57aebc1580aff8ff0c0cb271986506cbb1ad3e2a55296
|
|
| MD5 |
5e3115161569b08d00ebc65767e848db
|
|
| BLAKE2b-256 |
7efa2baa2d9e4db592a2e3d3bce0d34d51bda288bf5a838577ecd015ef28ea22
|
File details
Details for the file flux_limiter-0.1.15-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: flux_limiter-0.1.15-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 452.4 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b62a2871bb937bb41813835b13b93dcfe5b3a6dcd51b6f044f82c6a057e92c9
|
|
| MD5 |
e2a2cde7306cb0b11c34e4978734acef
|
|
| BLAKE2b-256 |
4c7a05d78696f78eb7b168687cf3c82f0a5590dd939cbb884adf900409ae7f6b
|
File details
Details for the file flux_limiter-0.1.15-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: flux_limiter-0.1.15-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 313.4 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f761b4f32e1d45705af8f3b256b3896cc080ab43d12d9f2270014a6e0b740c3
|
|
| MD5 |
c4db6465681dad5eb521b3ccffda9dc5
|
|
| BLAKE2b-256 |
1500a75f638e6f9ea367a64e55db08864ecb9d4044484ed2dc1cd45a0f2f054c
|