Skip to main content

In-memory rate limiter with sliding window and token bucket

Project description

philiprehberger-rate-limiter

Tests PyPI version Last updated

In-memory rate limiter with sliding window and token bucket algorithms.

Installation

pip install philiprehberger-rate-limiter

Usage

Basic Rate Limiting

from philiprehberger_rate_limiter import RateLimiter, Algorithm

limiter = RateLimiter(
    requests=100,
    window_seconds=60,
    algorithm=Algorithm.SLIDING_WINDOW,
)

if limiter.allow("user-123"):
    handle_request()

Check Status

status = limiter.status("user-123")
print(f"Allowed: {status.allowed}")
print(f"Remaining: {status.remaining}/{status.limit}")
print(f"Resets at: {status.reset_at}")

Blocking Wait

# Synchronous — blocks until quota is available or timeout expires
status = limiter.wait("user-123", timeout=5.0)

# Async — awaits until quota is available
status = await limiter.async_acquire("user-123")

Decorator

limiter = RateLimiter(10, 60)

@limiter.limit("10/minute")
def api_endpoint():
    return {"data": "ok"}

Algorithms

# Fixed window — resets at interval boundaries
RateLimiter(100, 60, Algorithm.FIXED_WINDOW)

# Sliding window (default) — rolling time window
RateLimiter(100, 60, Algorithm.SLIDING_WINDOW)

# Token bucket — smooth rate with burst capacity
RateLimiter(100, 60, Algorithm.TOKEN_BUCKET)

API

Name Description
RateLimiter(requests, window_seconds, algorithm=SLIDING_WINDOW) Create a rate limiter
limiter.allow(key) Check if request is allowed
limiter.status(key) Get detailed LimitStatus
limiter.wait(key, timeout=10.0) Block until quota available or raise RateLimitExceeded
limiter.async_acquire(key) Async wait until quota is available
limiter.reset(key) Reset state for a key
limiter.reset_all() Reset state for all keys
limiter.active_keys() List all keys with active state
limiter.limit(rate) Decorator with rate string (e.g., "10/minute")

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_rate_limiter-0.3.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_rate_limiter-0.3.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_rate_limiter-0.3.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.3.1.tar.gz
Algorithm Hash digest
SHA256 72275168019d95dfc28a71d2149a11a4533ee56e19c7e961bd236bfec9a1a593
MD5 55bb193a3d83e49645c28bd26674eef7
BLAKE2b-256 9d56bb1d1f2ea2057db0316cccaa962a5804aa1ea4e9f2d43775e136c901d42b

See more details on using hashes here.

File details

Details for the file philiprehberger_rate_limiter-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 59fbbd2c62592bdcfc1c5dbc24cc8f94fd3c547fddac4300aac60ceb67523182
MD5 d72cc7830bb620edaef64bb680d0e89f
BLAKE2b-256 9a1e3f4d2885728f01a0dfd124debee6ae2b8ad46685fd55580f69ba7762d984

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