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 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

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.17-cp313-cp313-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.13Windows x86-64

flux_limiter-0.1.17-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (427.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.17-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (460.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flux_limiter-0.1.17-cp313-cp313-macosx_11_0_arm64.whl (322.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

flux_limiter-0.1.17-cp312-cp312-win_amd64.whl (826.3 kB view details)

Uploaded CPython 3.12Windows x86-64

flux_limiter-0.1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (427.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.17-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (460.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flux_limiter-0.1.17-cp312-cp312-macosx_11_0_arm64.whl (322.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

flux_limiter-0.1.17-cp311-cp311-win_amd64.whl (633.6 kB view details)

Uploaded CPython 3.11Windows x86-64

flux_limiter-0.1.17-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (428.2 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.17-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (460.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

flux_limiter-0.1.17-cp311-cp311-macosx_11_0_arm64.whl (322.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ef463263bddc9fe7ed5381f7fa9fdac5e887894cc7a5ae03b3b3fd89b5385122
MD5 8fb8813d95569096df851fe4ee3d6328
BLAKE2b-256 0459e7ccf8ad98135eba8cf4306ab8e89dee2b000bcbcc56ee66211bb79cb5ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78a238d1e5a22126ee22625ac10dc1ff0eb5979ebf410f8ed127f42cf4789b78
MD5 1077be9397f9c9a1cfa68ce1df94f635
BLAKE2b-256 3c3e27d09ab3436339c79cd08b37c6fa5b73db9069caebca4c0afffcb752ab4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 4b6420d8d1c088bc265b72a4e0215978e0caa6939a54343821f582790c5b64a3
MD5 9358460a55eec9c23c47bf4d80fa6f5a
BLAKE2b-256 c28e7bab194bb1f281d0b8b92acee26ed4d748cc39e0b844a4e24bfc34fd4a02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9e64ed4490f75b08180393c806e165bf3a3993405faa32e04a333b0d2f3f83e
MD5 bc9eee114d2f2c03ce88ec9129c2ef0d
BLAKE2b-256 cc21ff72e28d4d847d0bf2e1d011a916771476d7eafc2d971f034a39fbbd0219

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 87885e0fe5fe2d84e68ac9b95e8ab58760014aa5d6b98e3cc3670aa649feeca0
MD5 134a5a66e1e38981b85b5e50aefa96ef
BLAKE2b-256 4da21db072769b0649ef12387a914fe2744f858cb6658c49e8ee6ca3a12eaa7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa4d15a44b0b8be21d5b7a17be42661861f31ddfc6083d242e4cbd67e80485a5
MD5 7a21fd11571aa2551fe18f7b041e458e
BLAKE2b-256 35b3e96a1b41e6f2498617dd25d03d5ac286f087cf6012414bdb8c12323ee1f4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 bd50b5a94b173c09ee5e0c5ca3d449d8c24fd83b3d6d502b417d21b5abe1d21d
MD5 04c86c38a131ee219a3f4d1877a55e62
BLAKE2b-256 4fce7e8fcde81106e01e147830bb43aa761543eefc9af1f928900c598fbbc28f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6d851adbf4bc8c58aca521f83eb16388bcea19a6fcfde92b01c00a027e09753b
MD5 41ba251392a33adb7ae1a169297ca09d
BLAKE2b-256 fbabbe55cc24c115622d3bdbab915228d30f4c0750e41bb04289f8560337e2fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2c80cb4d53dd51b2ca34754468594f074bcca7bef79215cf3809b7f7cbbc785d
MD5 4e80ff05d054b1236a31f22e7dc91c67
BLAKE2b-256 fc2ee599b8013899143567ed40267182fe18392c41956609d367e70a138624d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9793d59488ebf8e21d9451f1048ee7f754151537534a808a11c5c8d12d301836
MD5 7e2ed8b8890c98b1083f2b0f7554a1a0
BLAKE2b-256 ec9e08904bc45f07cc40cc708551ab960691c67501d02b494f8639a9744b78a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 03b01850e3044c609be1d93d604f1c8d2743817a2e047e6111cfb216c5421023
MD5 504869a53c61ecaa1d507189ffbd3e5e
BLAKE2b-256 7c0eaaf5229c25dcb2b687e20343304e8a10bda2c0e1a770e38b9caa63ead015

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.17-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88ec9ed08bdc19de9fe0e6556f4912672dbe5f2833e225bc672102805d23e12c
MD5 2a3a56bb80ae6a1fffedfff89d21eef3
BLAKE2b-256 93fca5f02c17966d0d9889cccca63dc71aba622dc8b34ff66e0ba9d7bc60b853

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