Skip to main content

In-memory rate limiter with sliding window and token bucket

Project description

philiprehberger-rate-limiter

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}")

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

  • 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.reset(key) — Reset state for a key
  • limiter.limit(rate) — Decorator with rate string (e.g., "10/minute")

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.1.0.tar.gz (4.3 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.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73e283642e2f0d1fa1b48b5cb446f6cdc86b7079d37ac6e84cf9406bb4d32ab4
MD5 cac5d595c2dbab13a66cb0802e80f192
BLAKE2b-256 6328e89de4c12cbbd946079dfe2693d2791c478e56b365511c65d73b2a0c6b69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3856d59a9480467e2994f314723be6c7777a6581c9f369f6fe44e2a5e6a7b0dd
MD5 c3653ba82fefec49a50e4cecfbe49e54
BLAKE2b-256 fd91548570aac6f4ef74363b150921f9ffd692b790e2c1681f5ceb23021ae0fc

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