-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RateLimiter] Fix retryAfter value on last token consume (SlidingWindow). #54163
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 our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 6.4
Are you sure you want to change the base?
Conversation
if ($availableTokens === $tokens) { | ||
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount()); | ||
$retryAfter = $now + $resetDuration; | ||
} else { | ||
$retryAfter = $now; | ||
} | ||
|
||
$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($retryAfter)), true, $this->limit)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi!
I was wondering if we were following the spirit and formalism of the previous lines of code 69 to 72 :
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount());
$resetTime = \DateTimeImmutable::createFromFormat('U', $availableTokens ? floor($now) : floor($now + $resetDuration));
return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit));
it might be possible to propose the following code:
if ($availableTokens === $tokens) { | |
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount()); | |
$retryAfter = $now + $resetDuration; | |
} else { | |
$retryAfter = $now; | |
} | |
$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($retryAfter)), true, $this->limit)); | |
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount()); | |
$resetTime = \DateTimeImmutable::createFromFormat('U', $availableTokens === $tokens ? floor($now + $resetDuration) : floor($now)); | |
$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), $resetTime, true, $this->limit)); |
Do you think it's worth keeping this consistency or do you think it's too much?
(This suggestion passes the tests.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that it makes sense, and imo using if-else is more readable than ternary ones
Hello again! I noticed a small detail: the try {
...
return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit));
...
} finally {
$this->lock?->release();
} In this case, all the errors in the |
Seems it is not related to that PR, so you can suggest some fixes in a separate PR. |
Fix retryAfter value when the last tokens are consumed ($availableTokens === $tokens).
/cc @wouterj 馃檹