Skip to main content

A High performance framework-agnostic rate limiter for Django, FastAPI, and Flask

Project description

flux-limiter

Flux is a high-performance framework-agnostic 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 exact 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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flux_limiter-0.1.18-cp313-cp313-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.13Windows x86-64

flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (430.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (463.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flux_limiter-0.1.18-cp313-cp313-macosx_11_0_arm64.whl (325.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

flux_limiter-0.1.18-cp312-cp312-win_amd64.whl (829.4 kB view details)

Uploaded CPython 3.12Windows x86-64

flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (430.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (463.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flux_limiter-0.1.18-cp312-cp312-macosx_11_0_arm64.whl (325.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

flux_limiter-0.1.18-cp311-cp311-win_amd64.whl (636.6 kB view details)

Uploaded CPython 3.11Windows x86-64

flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (431.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (463.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

flux_limiter-0.1.18-cp311-cp311-macosx_11_0_arm64.whl (325.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file flux_limiter-0.1.18-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 d6e4ceb3b888e12b1c1369f22e5d65dca4590d7fd3ec881c50f190a94c6ed609
MD5 babce39b8e68048b65caf38aa87d8f06
BLAKE2b-256 6dfaef27b95f99b7d36ca2e09c33ac77140aa2e1d5167de18a045d482feeeb03

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 02cba5cb36099e76df9a99ca5b94da6100f509bfdaf8eca742913dc42f50049a
MD5 277107dec8704a9e11a13152fa20d636
BLAKE2b-256 4b8b1af8cb3352b876de074405a2cde870f448d40eebd4bcba15ae4fb2f8ae6a

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 081782ec24a024c1b2181af3f7919a729ecf12ba4d080889fdb8f66828e4ac11
MD5 af2cd391ccb36b597536f1d5edf6a365
BLAKE2b-256 be9d2bcc84ce7716ec02d917d8d58123f1da85e6559eeef97e4cd7281952d929

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4a6259ca69dbab43f5edbe5bcdae9e2a4f1bb35d74ee1e02f7b2567125bcdb07
MD5 7bcabe03d43a0e84bd8d1d2b005acdfb
BLAKE2b-256 9d69a9e41d29d4b970912219cdd4d5ee515f1f545fff82444a71267c312110b5

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 045f2bb9188b2a2d35497b28345aafca5bb585ddf3b2882583d833a40d7396dc
MD5 738325a8670d2f69065cad7f591a5967
BLAKE2b-256 901f6a0a62a3b25998de1a17a4cd078461d71ad407160b7dd51e439ed7b879be

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f330e5c44dc80b5dfef0abf4e02c25037b7698590d863e2221fec36ddd8a76e
MD5 9e5ffe32356910d13d7eb686ce714237
BLAKE2b-256 0c8192a748468818dec132171003fbe9091ba3274ea09aef58db656a92de07da

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 eaa8784066ce2f43731be97dd883a5a47f106cf7d25e29cbe3243474785d5499
MD5 f4304f15130a92bed3657f087dc45d7a
BLAKE2b-256 45cb9c9017fcf87d1b030f6160e38055b28ada98bb93ba6b5fa1993cf8bb1940

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9a42d7b51023aafc42c27de96cba8893230f5c8754a9d479c69c82647f014028
MD5 2fd679756f07bab092a7c4a37f5d4a28
BLAKE2b-256 1689b8cd956a0aeb3de8516f50197adfb2228c5c7bfb3a8fa7d09f35f623b103

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ad36f966b6a60a9881097682e9659b18f0e76acb6ffab612dc2ddae076187c28
MD5 c052950c5102382d920010c59b68e321
BLAKE2b-256 8752bbd0f69a70bcb2f5ae4e52973f99e3d0835eff2f2d6b04ccdfab8678ee33

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 76ae44744c70444b540612defa04b38b96d20d3db1d9e268f87e10a2abbb170a
MD5 dd9304c6034af741ebba2813623800b7
BLAKE2b-256 8a937e18b9e3a10985d9e244284f9e64efb0f14d1fd94224e7c519efea93db33

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b0f89b1c6455bbf53037104a682103b6c3f62d8ced3e83bead7e45be7818c524
MD5 1cf3064a8e3b06d18bf99a913f4fc621
BLAKE2b-256 b6dca09f3451b8f3fe5cd19290e827b0b5e2db34cb15167c95b101103c9a84f9

See more details on using hashes here.

File details

Details for the file flux_limiter-0.1.18-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for flux_limiter-0.1.18-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8bc39a2d2531a06c3d5c747b80e0b21f66164c261a44bd46d9039f99d9a5e8e9
MD5 b583c2a12a1517d3d17cad3d393ac394
BLAKE2b-256 4b01d45a23719c1b8002bf09aea83be81f6da907f43cc0cca5da158b236479d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page