-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Rate limiter
TokenBucket::auto_replenish()
Frequently calling `auto_replenish` will reset `self.last_update` each time and `tokens` may be a fractional value (0 since it is a `u64`), in this case no tokens will replenished. To avoid this we increment `self.last_update` by the minimum time required to generate `tokens`, in the case where we have the time to generate `1.8` tokens but only generate `x` tokens due to integer arithmetic this will carry the time required to generate 0.8th of a token over to the next call, such that if the next call where to generate `2.3` tokens it would instead generate `3.1` tokens. This minimizes dropping tokens at high frequencies. Signed-off-by: Jonathan Woollett-Light <jcawl@amazon.co.uk>
- Loading branch information
1 parent
b790195
commit 02a5ad7
Showing
1 changed file
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters