Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 6.4
Choose a base branch
from

Conversation

ERuban
Copy link
Contributor

@ERuban ERuban commented Mar 5, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
License MIT

Fix retryAfter value when the last tokens are consumed ($availableTokens === $tokens).

/cc @wouterj πŸ™

@carsonbot carsonbot added this to the 6.4 milestone Mar 5, 2024
@ERuban ERuban changed the title [RateLimiter] Fix results on last token consume. [RateLimiter] Fix retryAfter value on last token consume (SlidingWindow). Mar 5, 2024
Comment on lines +77 to +84
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));
Copy link
Contributor

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:

Suggested change
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.)

Copy link
Contributor Author

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

@jprivet-dev
Copy link
Contributor

jprivet-dev commented Mar 6, 2024

Hello again!

I noticed a small detail: the try on the line 57 of the file SlidingWindowLimiter.php never catches an exception. It's only used for finally, which executes in all cases, even with a return in the try:

        try {
        ...
                return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit));
        ...
        } finally {
            $this->lock?->release();
        }

In this case, all the errors in the try will be silent. Isn't that risky? πŸ˜₯

@ERuban
Copy link
Contributor Author

ERuban commented Apr 8, 2024

In this case, all the errors in the try will be silent. Isn't that risky? πŸ˜₯

Seems it is not related to that PR, so you can suggest some fixes in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants