Skip to main content

A decorator-based rate limiter using the sliding-window algorithm

Project description

Rate Limiter

A Python decorator-based rate limiter using the sliding window algorithm.

Made by Dan Foster

Features

  • Simple decorator-based API rate limiting
  • Sliding window algorithm for strict rate limiting (exactly N calls allowed per time period)
  • Per-key rate limiting support (e.g., per user ID)
  • Auto-retry with configurable max retries
  • Thread-safe implementation
  • Zero external dependencies (Python standard library only)

Installation

pip install rate-limiter-df

For development:

pip install -e ".[dev]"

Usage

Basic Rate Limiting

from rate_limiter_df import RateLimiter, RateLimitExceeded

@RateLimiter(calls=10, period=60)  # 10 calls per 60 seconds
def my_api_call():
    return "Success"

try:
    result = my_api_call()
except RateLimitExceeded as e:
    print(f"Rate limit exceeded: {e}")

Per-Key Rate Limiting

Rate limit different keys (e.g., user IDs) independently:

def get_user_id(user_id, **kwargs):
    return user_id

@RateLimiter(calls=5, period=60, per_key=get_user_id)
def process_user_request(user_id, data):
    # Each user gets their own rate limit
    return f"Processed request for user {user_id}"

# User 1 can make 5 calls
process_user_request(user_id=1, data="...")
process_user_request(user_id=1, data="...")

# User 2 has their own separate rate limit
process_user_request(user_id=2, data="...")

Auto-Retry

Automatically wait and retry when rate limited:

@RateLimiter(calls=2, period=1.0, auto_retry=True, max_retries=3)
def resilient_call():
    return "Success"

# If rate limited, the decorator will sleep and retry up to 3 times
# before raising RateLimitExceeded
result = resilient_call()

How It Works

The rate limiter uses the sliding window algorithm:

  • Each function (or key) tracks timestamps of recent calls
  • When a call is made, timestamps older than the window period are discarded
  • If the number of calls within the window is under the limit, the call proceeds
  • If the limit has been reached, a RateLimitExceeded exception is raised with a retry time
  • With auto_retry enabled, the decorator sleeps for the wait time and retries automatically

API Reference

RateLimiter(calls, period, per_key=None, auto_retry=False, max_retries=3)

Creates a rate limiter decorator.

Parameters:

  • calls (int): Maximum number of calls allowed per period. Default: 1
  • period (float): Time period in seconds. Default: 60.0
  • per_key (callable, optional): Function to extract a key from function arguments for per-key rate limiting
  • auto_retry (bool): If True, automatically wait and retry when rate limited. Default: False
  • max_retries (int): Maximum number of retry attempts when auto_retry is enabled. Default: 3

Returns:

  • A decorator that can be applied to functions

RateLimitExceeded

Exception raised when the rate limit is exceeded. The exception message includes the wait time before a retry will succeed.

License

MIT License - see LICENSE.txt 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

rate_limiter_df-0.2.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

rate_limiter_df-0.2.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file rate_limiter_df-0.2.1.tar.gz.

File metadata

  • Download URL: rate_limiter_df-0.2.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for rate_limiter_df-0.2.1.tar.gz
Algorithm Hash digest
SHA256 32294757d84615cd86426b282ec465ff5f2cbcb9857f89b5c6c4c41838952817
MD5 539e924975e13bfdcb43671dcb312165
BLAKE2b-256 5b082eba9c55d74b239dca10cb9a2fd8949ea517ade6a6a718ca3028798e65e7

See more details on using hashes here.

File details

Details for the file rate_limiter_df-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for rate_limiter_df-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f3c94b4840bac26b56be98c68a9ad7f618af1e774a9deacc28ea30c10df6a4a4
MD5 6a8d5707d7e8c49b447cc0732c2b3b45
BLAKE2b-256 e115fe432d29761da0406c3b47f5f62562f06b487ff58802030774cbe5248660

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