A robust, centralized rate limiting utility for Python applications that need precise control over API request rates with built-in retry mechanisms and a single rate limit for all request methods.
pip install centralized-rate-limiter
- Centralized Configuration: Set rate limiting parameters once and reuse across your application for all request methods
- Comprehensive Retry Logic: Built-in exponential backoff and configurable retry attempts
- Thread-Safe: Reliable rate limiting in multi-threaded environments
- Flexible HTTP Support: Works with all standard HTTP methods
- Session Management: Extends
requests.Session
for seamless integration - Type Hints: Full type annotation support for better IDE integration
from centralized_rate_limiter import get_rate_limiter
# Create a rate limiter with custom parameters
rate_limiter = get_rate_limiter(
requests_per_second=10,
total_retries=5,
backoff_factor=0.25
)
# Use it for your API calls
response = rate_limiter.get('https://api.example.com/data')
# Works with all HTTP methods
post_response = rate_limiter.post(
'https://api.example.com/create',
json={'key': 'value'}
)
from centralized_rate_limiter import RateLimitedSession
# Create a session with specific parameters
session = RateLimitedSession(
requests_per_second=5,
total_retries=3,
backoff_factor=0.5
)
# Use session in your application
responses = []
for endpoint in endpoints:
response = session.get(endpoint)
responses.append(response)
from requests.exceptions import RetryError
try:
response = rate_limiter.get('https://api.example.com/data')
data = response.json()
except RetryError:
print("Max retries exceeded")
except Exception as e:
print(f"An error occurred: {e}")
Parameter | Type | Default | Description |
---|---|---|---|
requests_per_second |
int | 10 | Maximum number of requests allowed per second |
total_retries |
int | 5 | Maximum number of retry attempts for failed requests |
backoff_factor |
float | 0.25 | Multiplier for exponential backoff between retries |
-
Centralized Rate Limiting and Retry Logic
- You want a single solution that handles both rate limiting and retries
- You want a single rate limit for all your requests methods
- You need fine-grained control over retry behavior
-
Thread-Safe Operation
- Your application makes API calls from multiple threads
- You need reliable rate limiting in concurrent scenarios
-
Session Management
- You want to maintain session state across requests
- You need to reuse connections for better performance
-
Custom Configuration
- You require specific combinations of rate limiting and retry parameters
- You need to adjust settings per-instance
-
Distributed Rate Limiting Required
- Consider
python-redis-rate-limit
for distributed systems - Use
Flask-Limiter
for API endpoint protection
- Consider
-
Simpler Requirements
- Use
requests-ratelimiter
for basic rate limiting - Use
backoff
package for simple retry logic
- Use
-
Async Operation
- Consider
aiohttp
withaiohttp-client-manager
- Use
asyncio
based solutions for async workflows
- Consider
-
Minimal Dependencies
- Use built-in
time.sleep()
for simple delays - Implement basic rate limiting without external packages
- Use built-in
The library uses a combination of:
- Decorator-based rate limiting using the
ratelimit
package urllib3.util.Retry
for configurable retry behavior- Custom session management extending
requests.Session
Example retry sequence:
Request 1 (fails) β Wait 0.25s β Retry 1 (fails) β Wait 0.5s β Retry 2 (succeeds)
- Python 3.7+
- Make (optional, but recommended)
- Git
- Clone the repository:
git clone https://github.com/waddafunk/CentralizedRateLimiter.git
cd CentralizedRateLimiter
- Install development dependencies:
make dev-install
Or without make:
python -m pip install -e ".[dev]"
The project uses several development tools, all configured in pyproject.toml
:
- Black: Code formatter with a line length of 88 characters
- isort: Import statement organizer, configured to work with Black
- pyupgrade: Automatically upgrades Python syntax for newer versions
# Format all code
make format
or without make
# Individual tools
python -m black .
python -m isort .
python -m pyupgrade --py37-plus **/*.py
- pylint: Static code analysis
- flake8: Style guide enforcement
# Run all linters
make lint
or without make
python -m pylint centralized_rate_limiter tests
python -m flake8 centralized_rate_limiter tests
- pytest: Test framework
- pytest-cov: Coverage reporting
- responses: HTTP request mocking
# Run tests with coverage
make test
or without make
make coverage
- Create a new branch for your feature:
git checkout -b feature/your-feature-name
- Make your changes and ensure quality:
make format # Format code
make lint # Check code quality
make test # Run tests
# Or run all checks at once:
make all
- Build and check the package:
make build
- Submit a pull request
Command | Description |
---|---|
make install |
Install package for production |
make dev-install |
Install package with development dependencies |
make format |
Format code using black and isort |
make lint |
Run code quality checks |
make test |
Run tests with pytest |
make coverage |
Generate test coverage report |
make clean |
Remove build artifacts and cache files |
make build |
Build distribution packages |
make publish |
Prepare package for PyPI publishing |
make all |
Run all quality checks and tests |
make help |
Show available commands |
centralized_rate_limiter/
βββ .git/
βββ .gitignore
βββ centralized_rate_limiter/
β βββ __init__.py
β βββ rate_limiting.py
βββ tests/
β βββ test_rate_limiting.py
βββ Makefile
βββ pyproject.toml
βββ README.md
βββ LICENSE
The pyproject.toml
file contains all project configurations:
- Build system configuration
- Package metadata
- Dependencies (both runtime and development)
- Tool configurations:
- pytest settings
- coverage settings
- black configuration
- isort configuration
The Makefile provides convenient commands for common development tasks. See the commands table above for available operations.
The project uses GitHub Actions for CI/CD, running the following checks on each pull request:
- Code formatting (black, isort)
- Linting (pylint, flake8)
- Tests with coverage reporting
- Package building
Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
class RateLimitedSession(requests.Session):
"""A rate-limited HTTP session with built-in retry logic."""
def __init__(
self,
requests_per_second: int = 10,
total_retries: int = 5,
backoff_factor: float = 0.25
) -> None:
"""Initialize a new rate-limited session."""
def get_rate_limiter(
requests_per_second: int = 10,
total_retries: int = 5,
backoff_factor: float = 0.25,
) -> RateLimitedSession:
"""Create a new rate-limited session with specified parameters."""
For questions and support, please open an issue in the GitHub repository.
Made with β€οΈ for the Python community