We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GitHub: https://github.com/ZhuoZhuoCrayon/throttled-py 简介:🔧 支持多种算法(固定窗口,滑动窗口,令牌桶,漏桶 & GCRA)及存储(Redis、内存)的高性能 Python 限流库。
dict[key] += 1
INCRBY key increment
$ pip install throttled-py==1.1.0
limit
peek
from throttled import RateLimiterType, Throttled, rate_limiter, store, utils throttle = Throttled( # 📈 使用令牌桶作为限流算法。 using=RateLimiterType.TOKEN_BUCKET.value, # 🪣 设置配额:每秒填充 1000 个 Token(limit),桶大小为 1000(burst)。 quota=rate_limiter.per_sec(1_000, burst=1_000), # 📁 使用内存作为存储 store=store.MemoryStore(), ) def call_api() -> bool: # 💧 消耗 Key=/ping 的一个 Token。 result = throttle.limit("/ping", cost=1) return result.limited if __name__ == "__main__": # ✅ Total: 100000, 🕒 Latency: 0.5463 ms/op, 🚀 Throughput: 55630 req/s (--) # ❌ Denied: 96314 requests benchmark: utils.Benchmark = utils.Benchmark() denied_num: int = sum(benchmark.concurrent(call_api, 100_000, workers=32)) print(f"❌ Denied: {denied_num} requests")
from throttled import Throttled, rate_limiter, exceptions # 创建一个每分钟允许通过 1 次的限流器。 @Throttled(key="/ping", quota=rate_limiter.per_min(1)) def ping() -> str: return "ping" ping() try: ping() except exceptions.LimitedError as exc: # raise Rate limit exceeded: remaining=0, reset_after=60 print(exc) # 在异常中获取限流结果:RateLimitResult(limited=True, state=RateLimitState(limit=1, remaining=0, reset_after=60)) print(exc.rate_limit_result)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
throttled-py
🚀 功能
dict[key] += 1
操作。INCRBY key increment
操作。🔰 安装
🔥 快速开始
1)通用 API
limit
:消耗请求,返回 RateLimitResult。peek
:获取指定 Key 的限流器状态,返回 RateLimitState。2)样例
3)作为装饰器
The text was updated successfully, but these errors were encountered: