3个版本
0.3.2 | 2023年3月31日 |
---|---|
0.3.1 | 2023年3月30日 |
0.3.0 | 2023年3月28日 |
0.2.0 |
|
0.1.0 |
|
#1218 在 异步
11,118 每月下载量
在 tokio-utils 中使用
18KB
277 代码行
Async Throttle
异步速率限制
本库提供了粗粒度和细粒度的方式来限制异步任务执行的速率。
用法
将其添加到您的 Cargo.toml 中
[dependencies]
async-throttle = "0.3.2"
您可以使用细粒度速率限制器,例如:
#[tokio::main]
async fn main() {
let period = std::time::Duration::from_secs(5);
let rate_limiter = MultiRateLimiter::new(period);
// This completes instantly
rate_limiter.throttle("foo", || computation()).await;
// This completes instantly
rate_limiter.throttle("bar", || computation()).await;
// This takes 5 seconds to complete because the key "foo" is rate limited
rate_limiter.throttle("foo", || computation()).await;
}
依赖关系
~3–9.5MB
~75K SLoC