A rate limiter library for managing API key usage.
Project description
Rate Limiter
Rate Limiter 是一个用于管理 API 密钥使用的限流库,支持滑动窗口速率限制。
特性
- 支持每个 API 密钥的自定义速率限制
- 使用 Redis 作为后端存储
- 提供简单的接口来获取可用的 API 密钥
安装
首先,确保你已经安装了 Python 和 Redis。然后,你可以通过以下命令安装依赖:
pip install -r requirements.txt
使用说明
以下是一个简单的使用示例:
from rate_limiter import RateWindowLimiter
api_key_limits = {
"api_key_1": {"max_rpm": 2, "max_tpm": 5000, "max_tpd": 100000},
"api_key_2": {"max_rpm": 3, "max_tpm": 6000, "max_tpd": 120000},
"api_key_3": {"max_rpm": 4, "max_tpm": 7000, "max_tpd": 140000},
}
# 初始化Redis客户端
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)
# 创建限流器
rate_limiter = RateWindowLimiter(api_key_limits=api_key_limits, redis_client=redis_client)
# 模拟请求
for _ in range(15):
allowed, wait_time, api_key = rate_limiter.acquire()
if allowed:
print(f"请求成功,使用API密钥: {api_key}")
# 执行API调用...
else:
print(f"需等待 {wait_time:.2f} 秒后重试,API密钥: {api_key}")
time.sleep(wait_time)
贡献
欢迎贡献代码!请 fork 本仓库并提交 pull request。
许可证
本项目采用 MIT 许可证。详情请参阅 LICENSE 文件。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file key_rate_limiter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: key_rate_limiter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a13facd5ce7253616180430434e635326ce1afaaa626d81aa52b24738d99259
|
|
| MD5 |
179a16a6d3ae1ce5acd914a7434078b8
|
|
| BLAKE2b-256 |
e7bbf129e95d5a1a995fb0037cdd60b0b5e1f764f61027736d4a854adb98a7de
|