ds_protocol_http_py_lib.utils.http.token_bucket¶
File: token_bucket.py
Region: ds_protocol_http_py_lib/utils/http/token_bucket
Token Bucket Rate Limiter
This module implements a cooperative token bucket algorithm for rate limiting HTTP requests. A token bucket maintains a reservoir of tokens that are consumed when requests are made. Tokens are replenished at a constant rate (tokens per second), providing smooth rate limiting that allows burst traffic while maintaining an overall rate cap.
Key features: - Thread-safe using threading.Lock - Configurable requests per second (RPS) rate - Adjustable capacity for burst handling - Uses threading.Lock for thread-safe operations - Global RPS cap across all concurrent operations
Example
>>> limiter = TokenBucket(rps=10.0, capacity=20)
>>> limiter.acquire()
>>> # Make your HTTP request here
Attributes¶
Classes¶
Token Bucket Rate Limiter |
Module Contents¶
- ds_protocol_http_py_lib.utils.http.token_bucket.logger¶
- class ds_protocol_http_py_lib.utils.http.token_bucket.TokenBucket(rps: float = 10.0, capacity: int = 20)[source]¶
Token Bucket Rate Limiter
Implements the classic token bucket algorithm for controlling request rates in threading environments. Each request consumes one token from the bucket. If no tokens are available, the request waits until tokens are replenished based on the configured rate.
The bucket starts full and refills continuously at the specified RPS rate. This allows for burst traffic (up to the bucket capacity) while maintaining the overall rate limit.
- Parameters:
rps – Target requests per second rate. Determines token refill rate.
capacity – Maximum number of tokens the bucket can hold. Defaults to 2x RPS.
- Returns:
None
Example
# Limit to 10 requests per second with burst capacity of 20 limiter = TokenBucket(rps=10.0, capacity=20)
# Acquire permission for a request limiter.acquire() # Make your HTTP request here
- rps¶
- capacity = 20¶
- tokens¶
- last¶
- _lock¶