Skip to main content

Control and limit request rates to FastAPI applications with Redis and local memory support.

Project description

FastAPI Rate Limit Controller

Table of Contents

Description

This FastAPI library enables developers to control and limit the number of requests made to API endpoints, providing an effective solution to prevent server overload and enhance security. By using Redis and local memory storage, the library keeps a record of requests per endpoint, tracking when the last request was made and the number of requests within a specific time interval. If an endpoint reaches its allowed request limit within the defined interval, the library will temporarily block further requests to that endpoint, helping to prevent API abuse. This beta version currently supports Redis and local memory as storage options, with plans to expand to more types of storage in the future.

Installation

pip install fastapi-request-limit

Usage

Explain how to use your library with simple examples. Include code blocks and descriptions for each example to guide the user. For instance:

from fastapi_requests_limit.configuration import Limiter

limiter = Limiter(host="localhost", port="6379", storage_engine='redis')

This example requires installing Redis and having a set host. If you don't want to use Redis but rather local memory, The configuration would be as follows:

from fastapi_requests_limit.configuration import Limiter

limiter = Limiter(storage_engine='memory')

Then you must limit the endpoint you want.

from fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator


@app.get("/")
@limiter_decorator(time=5, count_target=3)
async def read_users(request: Request):
    return [{"username": "Rick"}, {"username": "Morty"}]

Configurations

Local memory

limiter = Limiter(storage_engine='memory')

Redis server

limiter = Limiter(host="<host>", port="<port>", storage_engine='redis')

Decorator

@limiter_decorator(time=5, count_target=3)

Example

from fastapi import FastAPI, Request
from fastapi_requests_limit.configuration import Limiter
from fastapi_requests_limit.limiter_rest import LimiterDecorator as limiter_decorator

app = FastAPI()
limiter = Limiter(host="localhost", port="6379", storage_engine='redis')


@app.get("/")
@limiter_decorator(time=5, count_target=3)
async def read_users(request: Request):
    return [{"username": "Rick"}, {"username": "Morty"}]

Project Status

If you're interested in contributing to this project, we welcome your contributions! Please read our CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Project details


Download files

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

Source Distribution

fastapi-requests-limit-0.1.5.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file fastapi-requests-limit-0.1.5.tar.gz.

File metadata

  • Download URL: fastapi-requests-limit-0.1.5.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for fastapi-requests-limit-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9e3940b157b1c963052133dd2cb6323914c34438785c4a9cabc94a9ce1a81620
MD5 538ac40812f9967220110ccccd983cda
BLAKE2b-256 c7916676fc22150992318d5379845ffa1b8de638fa7169059a5fddbb40e3ea8a

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