Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[RateLimiter] fix incorrect retryAfter of FixedWindow#49070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
RobertMe commentedJan 23, 2023
I'm uncertain about the Psalm failure. |
Robert-Embloom commentedFeb 6, 2023
@wouterj it seems like you're the original developer of the RateLimiter component. So may I ask you to have a look at this? (as it seems to have gone unnoticed in general) |
Uh oh!
There was an error while loading.Please reload this page.
c612584 to5c380e9CompareA FixedWindow always ends `intervalInSeconds` after the start time(`timer`). So when calculating the time to consume some tokens thetokens will always be available at `timer + intervalInSeconds`. Butcurrently this is reported incorrectly as `calculateTimeForTokens()`calculates the time based on the desired amount of tokens (and cycles)while it doesn't take into account `maxSize` amount of tokens becomeavailable at the windows end.Furthermore calculating the amount of needed cycles is incorrect. Thisas all tokens become available at once (at the windows end) and youcan't consume more tokens than `maxSize` (which is validated at thestart of `FixedWindowLimiter::reserve`, in case of `tokens > limit` itthrows).
5c380e9 to2316932Comparefabpot commentedJul 13, 2023
Thank you@RobertMe. |
A FixedWindow always ends
intervalInSecondsafter the start time (timer). So when calculating the time to consume some tokens the tokens will always be available attimer + intervalInSeconds. But currently this is reported incorrectly ascalculateTimeForTokens()calculates the time based on the desired amount of tokens (and cycles) while it doesn't take into accountmaxSizeamount of tokens become available at the windows end.Furthermore calculating the amount of needed cycles is incorrect. This as all tokens become available at once (at the windows end) and you can't consume more tokens than
maxSize(which is validated at the start ofFixedWindowLimiter::reserve, in case oftokens > limitit throws).Note: I don't think that changing the signature of
calculateTimeForTokensis a deprecation. This as theWindowclass is marked as@internal. So it should only be used by theRateLimitercomponent.