Semaphore-based concurrency limiter for FastAPI
Project description
fastapi-concurrency-limiter
Semaphore-based concurrency limiter for FastAPI. Define named resources with a maximum concurrency, apply them to routes with a decorator, and get automatic 503 responses when the server is too busy.
Installation
pip install fastapi-concurrency-limiter
Usage
from fastapi import FastAPI
from fastapi_concurrency_limiter import Limiter, Resource
app = FastAPI()
database_resource = Resource(5) # max 5 concurrent DB operations
file_resource = Resource(10) # max 10 concurrent file operations
# Register resources with the limiter in the order they should be acquired.
# Always use this same order in @limiter.resources() to prevent deadlocks.
limiter = Limiter(timeout=5, resources=[database_resource, file_resource])
@app.get("/messages")
@limiter.resources([database_resource])
async def read_messages():
...
@app.get("/file")
@limiter.resources([file_resource])
async def read_file():
...
@app.get("/messages-and-file")
@limiter.resources([database_resource, file_resource]) # must match registration order
async def read_messages_and_file():
...
When a request cannot acquire a semaphore within timeout seconds it receives:
{"detail": "Server busy, please retry later"}
with HTTP status 503.
Resource ordering and deadlocks
When an endpoint acquires multiple resources, always pass them to @limiter.resources() in the same order they were registered in Limiter(resources=[...]). Passing them in a different order raises a ValueError at startup — this is intentional: consistent acquisition order is the simplest way to prevent deadlocks.
limiter = Limiter(timeout=5, resources=[db, fs])
@app.get("/ok")
@limiter.resources([db, fs]) # correct
async def ok(): ...
@app.get("/bad")
@limiter.resources([fs, db]) # raises ValueError at startup
async def bad(): ...
API
Resource(capacity: int)
A semaphore with the given concurrency limit.
Limiter(timeout: float, resources: list[Resource])
timeout— seconds to wait for semaphore acquisition before returning 503.resources— all resources managed by this limiter, in canonical acquisition order.
@limiter.resources(resources: list[Resource])
Route decorator. Acquires the listed resources before the handler runs and releases them after, even on exception.
License
MIT
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
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 fastapi_concurrency_limiter-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_concurrency_limiter-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.14.4 Linux/7.0.0-22-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b82b98ef57204c2fcbd08b49ecfc7ebcfb499bcb6c683d0a5deae0ff68c0c42
|
|
| MD5 |
7a8b814533c02f20b4c22579ba303c61
|
|
| BLAKE2b-256 |
ecb2a28b18ff6287969b86cfcee6959939fd719c1c0f49f895b26638b4ee0712
|
File details
Details for the file fastapi_concurrency_limiter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_concurrency_limiter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.14.4 Linux/7.0.0-22-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e27ca78693e33bd65331dea0ec64c39bbc386a418da0bf3109d895729310dc16
|
|
| MD5 |
ab9a087ba8568c5b971cd22a63dcdbec
|
|
| BLAKE2b-256 |
fe626387b9f93094ec2b622911b7c224af782d9a0c24ac585b9f8ca3c2902e71
|