Ratelimit quantum not working as documented
sarfata opened this issue · comments
There are a few issues with the quantum argument of the rate limiter. I can split this up in multiple issues but I thought it would be easier to discuss in one ticket.
Filling the bucket with 10 tokens 10 times per second does not work
Expected
Ratelimiter(100, 10, 10)
should add 10 tokens in the bucket every 100 ms.
Actual
It adds 10 tokens in the bucket every second.
That is because in the constructor let tick = SECOND / (rate / quantum);
sets tick to be 1 second.
Comment
Since we add quantum
tokens to the bucket at each tick
interval, I don't think this division is required here. The length of a tick should not change (or the addition in tick()
needs to be removed to always add 1).
Divide by zero error
Expected
Ratelimiter(100, 10, 1)
should behave very similarly to Ratelimiter(100, 1, 10)
.
Actual
Divide by zero error because of let tick = SECOND / (rate / quantum);
in Ratelimiter::new()
.
Agreed that the behavior does not match the docs. I think this should be corrected to simply add the specified quantum
of tokens every ONE_SECOND / rate
. I normally use this with quantum = 1. Good catch.
This was fixed by #37