英文:
How can I create a RateLimiter for 10 requests per 10 seconds using Rust Governor?
问题
在Rust中使用Governor,是否有一种方法可以创建一个10/10秒的RateLimiter?
你可以创建Quota:
let q10 = Quota::with_period(Duration::from_secs(10))
.unwrap()
.allow_burst(NonZeroU32::new(10).unwrap());
然而,这只会每10秒恢复一次。
而且似乎没有一种完全在每隔一些秒钟就完全恢复配额的方法,对吗?
唯一的方法似乎是每10秒完全替换RateLimiter。
英文:
Is there a way in Rust with Governor, to create a RateLimiter for 10/10s?
You can create the Quota:
let q10 = Quota::with_period(Duration::from_secs(10))
.unwrap()
.allow_burst(NonZeroU32::new(10).unwrap());
However this only replenishes one every 10seconds.
And there doesn't seem to be a way to completely replenish a quota every some seconds correct?
The only way seems to be replacing the RateLimiter completely each 10 seconds.
答案1
得分: 1
我不相信 `governor` 能做到你想要的。[The ratelimit crate][1] 似乎可以做到。
```rust
let ratelimiter = Ratelimiter::builder(10, Duration::from_secs(10))
.max_tokens(10)
.build()
.unwrap();
这段代码将创建一个限制器,允许在每10秒内发起10次请求,最大突发请求数为10。这意味着你可以快速发起10次请求,但必须等待10秒才能再发起更多请求。
<details>
<summary>英文:</summary>
I do not believe `governor` can do what you want. [The ratelimit crate][1] seems to, though.
let ratelimiter = Ratelimiter::builder(10, Duration::from_secs(10))
.max_tokens(10)
.build()
.unwrap();
This snippet will create a limiter that allows 10 requests every 10 seconds, with a max burst of 10. This means that you can make 10 requests in quick succession but must wait 10 seconds before sending any more.
[1]: https://docs.rs/ratelimit/latest/ratelimit/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论