Skip to main content

In-memory rate limiter with sliding window and token bucket

Project description

philiprehberger-rate-limiter

Tests PyPI version License

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

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.2.2.tar.gz (5.7 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.2.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.2.2.tar.gz
Algorithm Hash digest
SHA256 1310d0b3a5fb5f56fa2a221e796a42d40a5f9e984f70be05fac682de4cd5a3b3
MD5 28a338156a8c069f67bd5263957739fb
BLAKE2b-256 ef2e46f9910170e53e013855adf5ff224326399b1f9378e92c113fe653eb0002

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 505629a1d15387c4ad931b8c3bac2cc2a4a377a969011e2993b2c4b8c2393282
MD5 511e5ee2d37f1d73ffc4ec5f9499480d
BLAKE2b-256 a0212de857bfd0f1eff9c82281b1dbde8a6e0e564901adacad9cab3a452889a9

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