Skip to main content

FastLimits is a rate-limiter extensions/middleware for FastAPI applications

Project description

[WIP] FastLimits is a rate limiting extension/middleware for FastAPI applications powered by limits.


Documentation: https://astronuttt.github.io/fastlimits

Source Code: https://github.com/astronuttt/fastlimits

Features

  • FastAPI depencies support: FastAPI dependencies can be used in limiters to apply limits based on the injected dependency.

  • Automatic documentation for additional responses: It will automatically add additional 429_TOO_MANY_REQUESTS response to the schema, so it can be used in the generated documentation.

  • Middlewares support: Can act as a middleware to apply limits to all the routes and etc.

  • Partial and full limits: limits can be applied to an APIRouter the whole FastAPI app or a single route operation.

  • Exclude certain responses: certain responses can be excluded from the limitation. for example, you can exclude 400 status codes and if the route returns a 400 it won't be counted for limitations.

  • Limit groups: You can apply a group of limits, for example a certain limit for admin users, and a certain limit for normal users.

  • Multiple limit strategies: It uses the limits library on the back-end so it supports multiple strategies. see Supported Strategies.

  • Multiple Storage backends: As another perk of the limits library, multiple storage backends are supported. see Storage backends

How it works

It uses the limits library in the backend for all the limitation handlings.

As for the FastAPI side it Injects a dependency into the APIRoute object and that dependency acts as the main component for limitation on that route. one of it's perks are that you can apply some limitations based on a Depends and everything will be handled automatically by FastAPI.

Other limit libraries

Some of the ideas for this library comes from two great libraries that were created before this:

SlowApi: https://github.com/laurents/slowapi

fastapi-limiter: https://github.com/long2ice/fastapi-limiter

These are two great libraries, but each of them lack some functionality, so I decided to extend them and create FastLimits.

Quick Start

This library works by a combination of a dependency and a middleware, but using it is very simple.

First step is to setup the Limiter and then add the limiting middleware to your FastAPI application.

from fastapi import FastAPI

# The simplest setup is to use 'MemoryStorage' and 'FixedWindowRateLimiter', you don't even have to specify these because it's the default
from limits.aio.storage import MemoryStorage
from limits.aio.strategies import FixedWindowRateLimiter

from fastlimits import RateLimitingMiddleware, limit


app = FastAPI()


limiter = FixedWindowRateLimiter(storage=MemoryStorage())
app.add_middleware(RateLimitingMiddleware, strategy=limiter)

# and thats for the setup! you can now use the 'limit' decorator to apply limits on any route

@limit(app, "5/minute")
@app.get("/")
async def items(q: int | None = None):
    return {"q": q}

And thats it! now users can call the GET 'items' route only 5 times per minute.

For more usage please refer to the documentation at https://astronuttt.github.io/fastlimits


This library inherits most of it's functionality from limits. special thanks to the everyone who helped make that happen.

License

This project is licensed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastlimits-0.0.1.tar.gz (298.3 kB view details)

Uploaded Source

Built Distribution

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

fastlimits-0.0.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file fastlimits-0.0.1.tar.gz.

File metadata

  • Download URL: fastlimits-0.0.1.tar.gz
  • Upload date:
  • Size: 298.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.1 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for fastlimits-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6cd6a8ffc9e179ee30de08c1786e14be42b422d3704cf6f040c5251148d59c52
MD5 1328ace7f1e352b78db7dc70f383944d
BLAKE2b-256 ac5d9e835c19db7760efb765548e3fef3678472a34ab7e5c549bb119f00879f3

See more details on using hashes here.

File details

Details for the file fastlimits-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: fastlimits-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.1 CPython/3.12.5 Linux/6.9.3-76060903-generic

File hashes

Hashes for fastlimits-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ed37bfa1db50685b770b341d61c2bfad8fb7fc91d9f560940dd7dc05ccd1f51
MD5 6c21bc0ed4b1d0a8f7afcee91458af44
BLAKE2b-256 9b96e9b77de5672c06bf792b54d7de98c4b1e02de06cc94225dc84e0fbfcc9f4

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