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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
if ($availableTokens >= $tokens) {
$window->add($tokens);

$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
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));
Comment on lines +77 to +84

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

} else {
$waitDuration = $window->calculateTimeForTokens($this->limit, $tokens);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public function testWaitIntervalOnConsumeOverLimit()
$this->assertTrue($limiter->consume()->isAccepted());
}

public function testConsumeLastToken()
{
$limiter = $this->createLimiter();
$limiter->reset();
$limiter->consume(9);

$rateLimit = $limiter->consume(1);
$this->assertSame(0, $rateLimit->getRemainingTokens());
$this->assertTrue($rateLimit->isAccepted());
$this->assertEquals(
\DateTimeImmutable::createFromFormat('U', (string) floor(microtime(true) + 12)),
$rateLimit->getRetryAfter()
);
}

public function testReserve()
{
$limiter = $this->createLimiter();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.