如何使用Rust Governor创建一个每10秒允许10个请求的速率限制器?

huangapple go评论57阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2023年6月5日 00:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定