2 个不稳定版本
0.2.0 | 2023年10月8日 |
---|---|
0.1.0 | 2022年11月7日 |
928 在 并发 类别中
48 每月下载量
用于 runway-rbx
10KB
94 行
ARL - 异步速率限制器
Arl 的主要目的是简化处理各种 API 的限制。例如,GitHub 每小时允许 5000 个请求。在业务逻辑中保留计时器可能会模糊主要流程,并增加大量样板代码。
使用 Arl 可以用一行代码限制任务的速度。作为一个额外的优点,限制器可以被克隆并发送到其他线程/任务,所有这些都将看到相同的实例并遵守相同的限制。
示例
// Create a rate limiter. Limit speed to 10 times in 2 seconds.
let limiter = RateLimiter::new(10, Duration::from_secs(2));
// Spawn 4 threads.
for i in 0..4 {
// RateLimiter can be cloned - all threads will use the same timer/stats underneath.
let limiter = limiter.clone();
tokio::spawn(async move {
// Create some imaginary client (for a rest service or sth).
let client = Client::new();
// Do things in a loop. Notice there is no `sleep` in here.
loop {
// Wait if needed. First 10 will be executed immediately.
limiter.wait().await;
// Call some api here.
let response = client.call();
}
});
}
依赖项
~2.3–4MB
~65K SLoC