Skip to main content

Asyncio rate limiter

Project description

arate-limit

A flexible and robust rate limiting library for Python applications, offering multiple implementation strategies including leaky bucket, token bucket, and Redis-based sliding window rate limiters.

Features

  • Multiple rate limiting strategies:
    • Leaky bucket rate limiter
    • Token bucket rate limiter
    • Redis-based sliding window rate limiter
    • Redis-based sliding window API rate limiter
  • Async/await support using asyncio
  • Configurable time windows and burst allowances
  • Safe for concurrent access within asyncio applications
  • Redis integration for distributed rate limiting

Installation

pip install arate-limit

Usage

Leaky Bucket Rate Limiter

A rate limiter that implements the leaky bucket algorithm, which smooths out bursts of requests and processes them at a steady rate:

import asyncio

from arate_limit import LeakyBucketRateLimiter

async def example():
    # Allow 100 requests per minute with some slack
    limiter = LeakyBucketRateLimiter(event_count=100, time_window=60, slack=10)

    async def limited_task():
        await limiter.wait()
        # Your rate-limited code here
        print("Task executed")

    # Execute multiple tasks
    tasks = [limited_task() for _ in range(10)]
    await asyncio.gather(*tasks)

Token Bucket Rate Limiter

More sophisticated rate limiting with burst support:

from datetime import timedelta

from arate_limit import TokenBucketRateLimiter

async def example():
    # Allow 1000 requests per hour with burst of 100
    limiter = TokenBucketRateLimiter(
        event_count=1000,
        time_window=timedelta(hours=1),
        burst=100
    )

    await limiter.wait()  # Wait for rate limit

Redis Sliding Window Rate Limiter

Distributed rate limiting using Redis:

from arate_limit import RedisSlidingWindowRateLimiter
import redis.asyncio as redis

async def example():
    redis_client = redis.Redis(host='localhost', port=6379)

    # Allow 1000 requests per minute with slack of 10
    limiter = RedisSlidingWindowRateLimiter(
        redis=redis_client,
        event_count=1000,
        time_window=60,
        slack=10
    )

    await limiter.wait()  # Wait for rate limit

Redis Sliding Window API Rate Limiter

Distributed API rate limiting using Redis:

from arate_limit import RedisSlidingWindowApiRateLimiter
import redis.asyncio as redis

async def example():
    redis_client = redis.Redis(host='localhost', port=6379)

    # Allow 1000 requests per minute per user
    limiter = RedisSlidingWindowApiRateLimiter(
        redis=redis_client,
        event_count=1000,
        time_window=60,
    )

    result, time_remaining = await limiter.check("user-1")
    if not result:
        raise HTTPException(
            status_code=429,
            detail=f"Rate limit exceeded. Try again in {time_remaining} seconds"
        )

Configuration Options

All rate limiters accept these common parameters:

  • event_count: Maximum number of events allowed in the time window
  • time_window: Time period for the rate limit (accepts int/float seconds or timedelta)

Additional options per implementation:

LeakyBucketRateLimiter

  • slack: Additional allowance for brief bursts (default: 10)

TokenBucketRateLimiter

  • burst: Maximum burst size (default: 100)

RedisSlidingWindowRateLimiter

  • redis: Redis compatible client/interface
  • slack: Additional allowance for brief bursts (default: 10)
  • key_prefix: Prefix for Redis keys (default: "rate_limiter:")

RedisSlidingWindowApiRateLimiter

  • redis: Redis compatible client/interface
  • key_prefix: Prefix for Redis keys (default: "rate_limiter:")

Error Handling

The rate limiters raise appropriate exceptions for invalid configurations:

  • TypeError: When parameters are of incorrect type
  • ValueError: When parameters have invalid values

Performance Considerations

  • LeakyBucketRateLimiter: Best for scenarios requiring steady, predictable request rates
  • TokenBucketRateLimiter: Efficient for bursty workloads
  • RedisSlidingWindowRateLimiter: Suitable for distributed systems, but requires Redis or Redis compatible cache service
  • RedisSlidingWindowApiRateLimiter: Suitable for distributed systems, but requires Redis or Redis compatible cache service

License

This project is licensed under the MIT License - see the LICENSE 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

arate_limit-1.2.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

arate_limit-1.2.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file arate_limit-1.2.1.tar.gz.

File metadata

  • Download URL: arate_limit-1.2.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for arate_limit-1.2.1.tar.gz
Algorithm Hash digest
SHA256 6018d8eacb0dbb4b250b54e4d87df58d962774869dfd5bb93954d06340c975ad
MD5 db043df20c9f0af1b31e9291d55d4b09
BLAKE2b-256 3e0c4c3eefd001011cc1686da4883dfcf321e0cbf8c2816dc49fc23fc006a207

See more details on using hashes here.

Provenance

The following attestation bundles were made for arate_limit-1.2.1.tar.gz:

Publisher: release.yml on jammymalina/arate-limit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file arate_limit-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: arate_limit-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for arate_limit-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bf23a0ea5f490c4e9778a18badbb027884873051191f5a0c722a9d6742ffda25
MD5 3acf1e24ef7be3635d4a1a6e2aa850bb
BLAKE2b-256 387cf15845926da35e442635557fb1100784bf6aab241aecc473c3287bf27954

See more details on using hashes here.

Provenance

The following attestation bundles were made for arate_limit-1.2.1-py3-none-any.whl:

Publisher: release.yml on jammymalina/arate-limit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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