Skip to content

[RateLimiter] Fix RateLimit->getRetryAfter() return value when consuming 0 or last tokens. #52835

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

Closed
wants to merge 7 commits into from
Closed
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
22 changes: 18 additions & 4 deletions src/Symfony/Component/RateLimiter/Policy/SlidingWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,35 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
$now = microtime(true);
$hitCount = $window->getHitCount();
$availableTokens = $this->getAvailableTokens($hitCount);
if ($availableTokens >= $tokens) {
if (0 !== $tokens && $availableTokens > $tokens) {
$window->add($tokens);

$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
} else {
if ($availableTokens === $tokens) {
$window->add($tokens);
}

$waitDuration = $window->calculateTimeForTokens($this->limit, max(1, $tokens));

if (null !== $maxTime && $waitDuration > $maxTime) {
if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
// process needs to wait longer than set interval
throw new MaxWaitDurationExceededException(sprintf('The rate limiter wait time ("%d" seconds) is longer than the provided maximum time ("%d" seconds).', $waitDuration, $maxTime), new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
}

$window->add($tokens);
if ($availableTokens !== $tokens) {
$window->add($tokens);
}

if ($availableTokens === $tokens || 0 === $tokens) {
$accepted = true;
$timeToAct = $now;
} else {
$accepted = false;
$timeToAct = $now + $waitDuration;
}

$reservation = new Reservation($now + $waitDuration, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
$reservation = new Reservation($timeToAct, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->limit));
}

if (0 < $tokens) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,44 @@ public function testConsume()
$this->assertSame(10, $rateLimit->getLimit());
}

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 / 10)),
$rateLimit->getRetryAfter()
);
}

public function testConsumeZeroTokens()
{
$limiter = $this->createLimiter();
$limiter->reset();

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

$limiter->reset();
$limiter->consume(10);

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

public function testWaitIntervalOnConsumeOverLimit()
{
$limiter = $this->createLimiter();
Expand All @@ -76,6 +114,9 @@ public function testReserve()

// 2 over the limit, causing the WaitDuration to become 2/10th of the 12s interval
$this->assertEqualsWithDelta(12 / 5, $limiter->reserve(4)->getWaitDuration(), 1);

$limiter->reset();
$this->assertEquals(0, $limiter->reserve(10)->getWaitDuration());
}

private function createLimiter(): SlidingWindowLimiter
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy