Skip to main content

A Python async rate limiter using Redis and Lua with sliding window algorithm. Supports retries, exponential backoff, and optional exception handling.

Project description

Async Rate Limiter (Sliding Window + Lua + Redis)

A Python async rate limiter using Redis and Lua with sliding window algorithm.
Supports retries, exponential backoff, and optional exception handling.


Installation

pip install redis asyncio

Usage

Basic usage with inline function

import asyncio
import redis.asyncio as aioredis
from rate_limit_module import RateLimit  # replace with your module
from rate_limiter.exceptions import RetryLimitReached

redis_client = aioredis.Redis(host='localhost', port=6379, db=0)

rate_limit = RateLimit(
    redis=redis_client,
    limit=5,
    window=10,
    retries=3,
    backoff_ms=200,
    backoff_factor=2.0,
    retry_on_exceptions=(ValueError,),
)

async def my_task():
    print('Task executed.')
    return 42

wrapped = rate_limit(fn=my_task, key='task_key')

try:
    result = await wrapped()
    print(result)
except RetryLimitReached:
    print('All retry attempts exhausted.')

Using as a decorator

rate_limit = RateLimit(
    redis=redis_client,
    limit=3,
    window=5,
    retries=4,
    backoff_ms=100,
    backoff_factor=1.5,
)

@rate_limit(key='decorated_task')
async def my_decorated_task():
    print('Decorated task executed.')
    return 'done'

try:
    await my_decorated_task()
except RetryLimitReached:
    print('Rate limit retry attempts exhausted.')

Exception behavior

After all retries are used without success, the limiter raises RetryLimitReached exception
instead—making it easier to handle failure explicitly in your code.


Features

  • Sliding window rate limiting using Redis + Lua
  • Async-friendly
  • Retries with exponential backoff configurable
  • Optional exception-based retry logic
  • Raises RetryLimitReached when retry attempts are exhausted
  • Supports both inline wrapper and decorator syntax

License

MIT License

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_decorator-0.0.7.tar.gz (3.0 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_decorator-0.0.7-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file rate_limiter_decorator-0.0.7.tar.gz.

File metadata

  • Download URL: rate_limiter_decorator-0.0.7.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.12.9 Linux/6.8.0-55-generic

File hashes

Hashes for rate_limiter_decorator-0.0.7.tar.gz
Algorithm Hash digest
SHA256 dde6dc3148b2a7c1655064e923756d4990b398a5083df856da1fd7b57da23960
MD5 fa85a04ec102e551fc827b41db47a377
BLAKE2b-256 5568bee9646364121b51afbaa44ac8378a20ebfe69fce5eb69834a17c6e02f5a

See more details on using hashes here.

File details

Details for the file rate_limiter_decorator-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for rate_limiter_decorator-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 e63f2d2d12b73926c8f389fbd1483a0b9e9a5c8cc1ec2012ec9e9a545837ddc0
MD5 fe10be09a7ddbfb582d03d12bfb5e49f
BLAKE2b-256 4c8e763f614cf3d30ab0653c8a1783b0e7d02f15e8f8942bd9aa163c790f6873

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