Skip to main content

penguin-limiter

Rate limiting middleware for Penguin Tech Python applications.

Features

  • Flask extension with decorator-based route limiting
  • Token bucket rate limiting algorithm
  • In-process memory storage with LRU eviction
  • Redis-backed storage for distributed systems
  • Configurable rate limits (per second, minute, hour, day)
  • Thread-safe implementation
  • Standard HTTP rate limit headers

Installation

pip install penguin-limiter

# With Redis support:
pip install 'penguin-limiter[redis]'

Quick Start

from flask import Flask
from penguin_limiter import FlaskRateLimiter, MemoryStorage, RateLimitConfig

app = Flask(__name__)

# Create rate limiter with 100 requests per hour
limiter = FlaskRateLimiter(
    config=RateLimitConfig.from_string("100/hour"),
    storage=MemoryStorage(),
)
limiter.init_app(app)

@app.route("/api/endpoint")
@limiter.limit()
def my_endpoint():
    return {"message": "success"}

Configuration

Using RateLimitConfig

# Parse from string
config = RateLimitConfig.from_string("100/hour")

# Or create directly
from penguin_limiter import RateLimitConfig
config = RateLimitConfig(rate=100, unit="hour")

Supported units: second, minute, hour, day

Storage Options

Memory Storage (Default)

from penguin_limiter import MemoryStorage

storage = MemoryStorage(max_entries=10000)

Redis Storage

from penguin_limiter import RedisStorage

storage = RedisStorage(url="redis://localhost:6379/0")

API Reference

FlaskRateLimiter

limiter = FlaskRateLimiter(
    config=RateLimitConfig.from_string("100/hour"),
    storage=MemoryStorage(),
    key_func=lambda: request.remote_addr  # Optional custom key function
)

Decorating Routes

# Use default limit
@app.route("/api/users")
@limiter.limit()
def get_users():
    return []

# Override limit for specific route
@app.route("/api/expensive")
@limiter.limit(RateLimitConfig.from_string("10/hour"))
def expensive_operation():
    return {}

Rate Limit Headers

Standard X-RateLimit headers are added to all responses:

  • X-RateLimit-Limit: Maximum requests in window
  • X-RateLimit-Remaining: Requests remaining in current window
  • X-RateLimit-Reset: Seconds until window resets
  • Retry-After: Seconds to wait on 429 responses

See Also

  • Backend standards: backend.md Rate Limiting
  • Flask extensions pattern: Similar to Flask-Security-Too, Flask-Limiter

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

penguin_limiter-0.1.0.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

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

penguin_limiter-0.1.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: penguin_limiter-0.1.0.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for penguin_limiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b594a76cd41b71fc8d853d627e568cb46a9545f385741ab2ceeb213da31ba17
MD5 178de079dc3ce4db815c830b28a7df45
BLAKE2b-256 97faea42e9f0dcecbdfda41fe99ea7851d4e421ac66ca92f3f8ce86a3038f3a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for penguin_limiter-0.1.0.tar.gz:

Publisher: publish.yml on penguintechinc/penguin-libs

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

File details

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

File metadata

File hashes

Hashes for penguin_limiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0bcb0efb90d1b33f8b16fa76cb961a0bb432b1a2ce36a7d01e18b0234cb6f85c
MD5 2a7c65d37e00d64d5aae3a1c7a646c94
BLAKE2b-256 116c4a4aa9a6481dc5b7ca8702b9653d20c29b2ed383deeca94e7badb1527525

See more details on using hashes here.

Provenance

The following attestation bundles were made for penguin_limiter-0.1.0-py3-none-any.whl:

Publisher: publish.yml on penguintechinc/penguin-libs

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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page