Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Open
wants to merge 2 commits into
base: 6.4
Choose a base branch
Loading
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));

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

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.