Skip to main content

A rate limiter for FastAPI

Project description

ThrottledAPI

This repo aims to be an audacious rate limiter for FastAPI. Check our features to see the use-cases already tested.

Why another rate limiter for FastAPI?

Why another rate limiter for FastAPI, if we already have slowapi and fastapi-limiter? This limiter glues what is good from both projects and adds a bit more. Here is a list of reasons:

  • The throttled-api rate limiter takes full advantage from the composable dependency injection system in FastAPI. That means you can also create limiters per resource.
    • Want to limit requests per IP or per user? Got it!
    • Want to limit requests based on another weird parameter you are receiving? Just extend our FastAPILimiter and you are good to go!
  • You can use different storage storages backends (different implementations for BaseStorage) for each limiter.
    • Want to each API instance to 2000 requests per second? You don´t need more than a in-memory counter. Just use MemoryStorage for the task.
    • Want to limit calls to all your API instances by user or IP? A shared cache is what you need. Our RedisStorage implementation is an adapter for the famous redis package. Other implementations + asyncio support are comming...

Instalation

Just use your favorite python package manager. Here are two examples:

  • With pip: pip install throttled
  • With poetry: poetry add throttled

Usage


Middleware vs Dependency

Although FastAPI dependency injection is really powerfull, some limiters doesn´t require any special resource in other to use it. In that case you cut some latency if using the limiter as a Middleware. To do that, just extend our MiddlewareLimiter, which is a extension of FastAPILimiter to work as a middleware.

When implementing a custom limiter, how to choose between extending FastAPILimiter or MiddlewareLimiter?

stateDiagram-v2
    state FirstCondition <<choice>>
    state SecondCondition <<choice>>
    
    FirstQuestion: What type of limiter should I choose?
    FirstQuestion --> FirstCondition
    
    FirstCondition: Limitting depends on resources other\nthan Request object from Starlette?
    FirstCondition --> FastAPILimiter: yes
    FirstCondition --> MiddlewareLimiter : no
    FastAPILimiter --> SecondQuestion
    MiddlewareLimiter --> SecondQuestion
    
    SecondQuestion: What storage should I pick?
    SecondQuestion --> SecondCondition
    SecondCondition: The parameters you are limitting spams a parameter space.\n Is that space too large?
    SecondCondition --> RedisStorage : yes
    SecondCondition --> ThirdCondition : no
    
    ThirdCondition: You want to share the limiter\nbetween different API instances (pods)?
    ThirdCondition --> RedisStorage : yes
    ThirdCondition --> MemoryStorage : no
    
    RedisStorage --> End
    MemoryStorage --> End
    End: Attach the limiter to your API     

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

throttled-0.1.16a37.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

throttled-0.1.16a37-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file throttled-0.1.16a37.tar.gz.

File metadata

  • Download URL: throttled-0.1.16a37.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for throttled-0.1.16a37.tar.gz
Algorithm Hash digest
SHA256 0ff5346b9ed655fb9de825dedcbb3058178b74643643477130e62dbf833856d5
MD5 1dfebeeb3f3eda8fe2240f2a37397e27
BLAKE2b-256 bf110351dc75600a9c1d459233879084f5d42f57ca554c67158a444b233c3ae7

See more details on using hashes here.

File details

Details for the file throttled-0.1.16a37-py3-none-any.whl.

File metadata

  • Download URL: throttled-0.1.16a37-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure

File hashes

Hashes for throttled-0.1.16a37-py3-none-any.whl
Algorithm Hash digest
SHA256 e928f2890d41052427c01d07ef9650c794426c1e2d930f5474d3d634f201b1e4
MD5 82bf48ef3b514359c7c07d87aef90a2c
BLAKE2b-256 cdd4dbaeb03ca107e128fd126e81ee9491b5377312264a51d80925cde40f35e9

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