8000 [RateLimiter] Fix `RateLimit->getRetryAfter()` return value when consuming `0` or last tokens. by ERuban · Pull Request #52835 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Prev Previous commit
[RateLimiter] Allow to get RateLimit without consuming again.
  • Loading branch information
ERuban committed Dec 17, 2023
commit c15d76d41d08e2fb0c952464ff9dca5ec358be4c
20 changes: 4 additions & 16 deletions src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,21 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
$now = microtime(true);
$availableTokens = $window->getAvailableTokens($now);

if (0 !== $tokens && $availableTokens > $tokens) {
if ($availableTokens >= max(1, $tokens)) {
$window->add($tokens, $now);

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

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

if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
if (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($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
}

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

if ($availableTokens === $tokens || 0 === $tokens) {
$accepted = true;
} else {
$accepted = false;
}
$window->add($tokens, $now);

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

if (0 < $tokens) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

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)), $accepted, $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 @@ -57,21 +57,6 @@ public function testConsume()
$this->assertEquals($retryAfter, $rateLimit->getRetryAfter());
}

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

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

/**
* @dataProvider provideConsumeOutsideInterval
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,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
0