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.1.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.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3905e0371b05051861f6835fff5cb3ef524c17d304eeaa89cf7f47a2807515d4
MD5 d32e26461d7a9800d9edcf070c303487
BLAKE2b-256 4b9e41ea1f9274ae8c04bbbaf5673e9ee47ebc40030acb3d7fec2b84ace8c6aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_rate_limiter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f6ae83e95914718cbe5095e1d0c6539a1d30f5389734db4d931b2c866ec48f7
MD5 4553a0b74408ee8ea9d6cff3d857df3e
BLAKE2b-256 5a7a08b08f30555cfd001afac6e59e27632b72cf4edebc7418af5e84c237a19f

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