Introduction to throttled-py
mainOverview
throttled-py is a high-performance Python rate limiting library designed for both synchronous and asynchronous (async/await) applications. It provides developers with multiple algorithms and storage backends to control request rates and prevent service overload.
Key Features
Supported Algorithms
- Fixed Window: Simple window-based limiting.
- Sliding Window: Smoother window transitions.
- Token Bucket: Allows for bursts of traffic up to a defined bucket size.
- Leaky Bucket: Smooths out traffic by processing requests at a constant rate.
- Generic Cell Rate Algorithm (GCRA): A sophisticated algorithm often used in network traffic shaping.
Storage Backends
- In-Memory: High-performance local storage with support for key expiration and eviction.
- Redis: Distributed storage suitable for multi-node/distributed systems.
Usage Modes
- Function Call: Explicitly calling the rate limiter within your logic.
- Decorator: Using
@syntax to wrap functions with rate limiting logic. - Context Manager: Using
withblocks to limit code execution within a specific scope.
Response Strategies
- Immediate Response: Returns a failure/status immediately if the limit is exceeded.
- Wait-Retry: Pauses execution until the rate limit allows for the next request.