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

Uploaded CPython 3.13Windows x86-64

flux_limiter-0.1.16-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (419.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.16-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (452.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flux_limiter-0.1.16-cp313-cp313-macosx_11_0_arm64.whl (313.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

flux_limiter-0.1.16-cp312-cp312-win_amd64.whl (817.0 kB view details)

Uploaded CPython 3.12Windows x86-64

flux_limiter-0.1.16-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (419.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.16-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (452.1 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flux_limiter-0.1.16-cp312-cp312-macosx_11_0_arm64.whl (313.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

flux_limiter-0.1.16-cp311-cp311-win_amd64.whl (624.8 kB view details)

Uploaded CPython 3.11Windows x86-64

flux_limiter-0.1.16-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (419.8 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flux_limiter-0.1.16-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (452.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

flux_limiter-0.1.16-cp311-cp311-macosx_11_0_arm64.whl (313.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6ebd610767e3faaa594312d7b0628292e94f17d35ecf9d42ec0c618dab60fd07
MD5 324e2a1a816cfbc59960548f252f9a9e
BLAKE2b-256 2f0bb20d97b19f581d1f49fbb0b3c00b2ad46f920eeedb6d39c7637765b88201

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b0ef5a72f556af7929b6c3b6c5ab2b68fc9ef8f12b344a31306afa2b2f62f1e
MD5 63d8e3d875a5397cb26bdc1fd3ebfba0
BLAKE2b-256 25744e916757702114d4c77224b936a89a2e993afe443d27491f9459d274deb9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 ee7d144610050b0e0a60d88e445373cec04b51f757ee9369de49423eb191eca0
MD5 3871860fddbd5eb3a5c123570fdb5163
BLAKE2b-256 595527c92388ad39b10a3e5fae52de34e238685e4efae7bce974efc53de27a76

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e9dc8a8f78126e46db4e21d3e2f9988063699f651c8b89c9c3bb88400af96f7
MD5 55d62c8155e1e07419ea8307f63e1f45
BLAKE2b-256 a8748923d4881a1739e411c5b0b901b073a043dee5631d9a16e2736e1f16398f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 059abee50af51abc67e3da7ba75d065c8d57e968f31b9bc1346f3f188c42bbb7
MD5 bf9bd2250f002d8a9027f6966bd86b02
BLAKE2b-256 f1075c41a5a89b48b2be4ca80bcbd6ec104269d6cb177fef5fbfc5f83f5f7e7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1e07614ce6750f75b40d6f8f8c7e337ee44a35c48af327343eaa48c68ea32d81
MD5 83e750bc365a656a348993a48646f71d
BLAKE2b-256 0e48dcb54422522886c202c9b9ab89652d3199ade16ba54018fc82c26ef2da8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 6f8c54216fbece9611e1295ecb76a9246199518c164496a1c11ad9069ed9d9cc
MD5 54d7cb1e19c43030b8fe8466c9f1d2a6
BLAKE2b-256 0aed4392d50c20275a2121b3aaf9c1b45850424d9459c2784cc7e178479ab5fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb8b0c03417272d5c4bd860ee2cfbe4c19992ca8284068b7ef70fe12cd129b99
MD5 6687edd3172b4fa2b10661f5531c3185
BLAKE2b-256 c4080ba2697d85d2b68a327b3115ff225c3583268a92570fcd06c4b2ea3ff86a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 65be2064ada8176a23e348f676cf28a7d613a7a049555633677ffbda6efaff40
MD5 30e5a3f62d8727ddc15951c607b3d6d6
BLAKE2b-256 b027983425f390d9bf30e01b2ef2ba80fe2d9bbc33bdb102fe566c9a7365b8b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 79b6c263a3b12920c672796dcf4cfd0c8fed1fa15277ea1f7164b6d39a3c3f4a
MD5 48e1c413551dec0e7acf91ba9c072d77
BLAKE2b-256 21faea09ac99c06420ee375723a2eb5eef21bba96f80ecd79034e41e20beba10

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 b61f6bc7303655dc694f0952a0080f9d08f4347c18297ce2c847b3746e587c2c
MD5 e2f72b9a238fbfab8628941e8f830e29
BLAKE2b-256 4a8dcc96d4dbef0721d8db1fda1051511a73c28ec14145ee4768d9d76104b446

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flux_limiter-0.1.16-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 28f30db918d4a772940846166e56712f08f59e9c3de6b3e971c603e87b8f44f2
MD5 a7603fe22d21bf22bfbcd60a7df90cc7
BLAKE2b-256 08c3fe1a8dd274b15d879d6cabf03e1507046536139d769a322ff70865b722b2

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