Skip to main content

A lightweight, modular rate limiting library for python

Project description

RateGuard Minimal Usage Guide

RateGuard is a framework-agnostic rate limiting library. While it includes a built-in decorator for FastAPI, its core RateLimiter class can be used in any Python framework (Flask, Django, Celery, or pure Python scripts).


1. Framework-Agnostic Usage (Any Python App)

You can use the core RateLimiter class directly anywhere in your code.

from rateguard import RateLimiter

# Create a limiter: Allow 100 requests every 60 seconds
limiter = RateLimiter(
    max_retries=100,
    ttl=60 #-----> total time limit
)

def some_action(user_id: str):
    # Check the rate limit for this specific user
    result = limiter.check(user_id)
    
    if result["allowed"]:
        print(f"Action allowed! Remaining requests: {result['remaining']}")
        # Perform the action...
    else:
        print(f"Rate limited. Try again in {result['retry_after']} seconds")
        # Return an error to the user

2. FastAPI Usage (Built-in Decorator)

If you are using FastAPI, RateGuard provides a convenient @limit decorator that automatically handles key resolution (like IP addresses or authenticated users) and throws standard 429 Too Many Requests HTTP errors.

from fastapi import FastAPI, Request
from rateguard import limit

app = FastAPI()

# Limit this endpoint to 5 requests per 60 seconds
@limit(max_retries=5, ttl=60)
def my_endpoint(request: Request):
    return {"message": "Hello!"}

@app.get("/hello")
def hello_route(request: Request):
    return my_endpoint(request)

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

requestguard-0.1.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

requestguard-0.1.2-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file requestguard-0.1.2.tar.gz.

File metadata

  • Download URL: requestguard-0.1.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for requestguard-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e22e706870a95eaab277f4011d71ec3cee797d117cddc33e0b89a665a624dec1
MD5 974d717ceb3022280f872ee5e6eb1d08
BLAKE2b-256 655404a351bb91079bbcb7022f5a02b87a96f1cad0afac9f9133ffe1da250e5c

See more details on using hashes here.

File details

Details for the file requestguard-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: requestguard-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for requestguard-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 56bb07584facf31d2271c55705c6d0e83734aee6792d298432b55402d9886e72
MD5 81449e3422877843e77a5e6c272ac9c2
BLAKE2b-256 53332388d3420173013d49eda1fe3f4432bf599afb12cbc3bdf02eb2ff6c0d7d

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