Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a RateLimiter for 10 requests per 10 seconds using Rust Governor?

Tags:

rust

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.

like image 350
LGXerxes Avatar asked Oct 19 '25 06:10

LGXerxes


1 Answers

I do not believe governor can do what you want. The ratelimit crate 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.

like image 103
amycodes Avatar answered Oct 22 '25 04:10

amycodes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!