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 results on last token consume #53064

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 1 commit into
base: 5.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
22 changes: 18 additions & 4 deletions 22 src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,35 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

$now = microtime(true);
$availableTokens = $window->getAvailableTokens($now);
if ($availableTokens >= $tokens) {
if (0 !== $tokens && $availableTokens > $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($tokens, $now);

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($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
}

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

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

$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
$reservation = new Reservation($timeToAct, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->limit));
}
$this->storage->save($window);
} finally {
Expand Down
24 changes: 19 additions & 5 deletions 24 src/Symfony/Component/RateLimiter/Policy/TokenBucketLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

$now = microtime(true);
$availableTokens = $bucket->getAvailableTokens($now);
if ($availableTokens >= $tokens) {
if (0 !== $tokens && $availableTokens > $tokens) {
// tokens are now available, update bucket
$bucket->setTokens($availableTokens - $tokens);

$reservation = new Reservation($now, new RateLimit($bucket->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->maxBurst));
} else {
$remainingTokens = $tokens - $availableTokens;
$waitDuration = $this->rate->calculateTimeForTokens($remainingTokens);
if ($availableTokens === $tokens) {
$waitDuration = $this->rate->calculateTimeForTokens($tokens);
} elseif (0 === $tokens) {
$waitDuration = 0;
} else {
$remainingTokens = $tokens - $availableTokens;
$waitDuration = $this->rate->calculateTimeForTokens($remainingTokens);
}

if (null !== $maxTime && $waitDuration > $maxTime) {
if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
// process needs to wait longer than set interval
$rateLimit = new RateLimit($availableTokens, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->maxBurst);

Expand All @@ -87,7 +93,15 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
// so no tokens are left for other processes.
$bucket->setTokens($availableTokens - $tokens);

$reservation = new Reservation($now + $waitDuration, new RateLimit(0, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->maxBurst));
if ($availableTokens === $tokens || 0 === $tokens) {
$accepted = true;
$timeToAct = $now;
} else {
$accepted = false;
$timeToAct = $now + $waitDuration;
}

$reservation = new Reservation($timeToAct, new RateLimit(0, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->maxBurst));
}

$this->storage->save($bucket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ 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 All @@ -76,6 +91,15 @@ public function testConsumeOutsideInterval(string $dateIntervalString)
$this->assertTrue($rateLimit->isAccepted());
}

public function testReserve()
{
$limiter = $this->createLimiter('PT1S');

$this->assertEquals(0, $limiter->reserve(5)->getWaitDuration());
$this->assertEquals(0, $limiter->reserve(5)->getWaitDuration());
$this->assertEquals(1, $limiter->reserve(5)->getWaitDuration());
}

public function testWaitIntervalOnConsumeOverLimit()
{
$limiter = $this->createLimiter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ public function testConsume()
$this->assertSame(10, $rateLimit->getLimit());
}

public function testConsumeLastToken()
{
$rate = Rate::perSecond(1);
$limiter = $this->createLimiter(10, $rate);

$rateLimit = $limiter->consume(10);
$this->assertSame(0, $rateLimit->getRemainingTokens());
$this->assertTrue($rateLimit->isAccepted());
$this->assertEqualsWithDelta(time(), $rateLimit->getRetryAfter()->getTimestamp(), 10);
}

public function testConsumeZeroTokens()
{
$rate = Rate::perSecond(1);
$limiter = $this->createLimiter(10, $rate);

$rateLimit = $limiter->consume(0);
$this->assertTrue($rateLimit->isAccepted());
$this->assertEquals(time(), $rateLimit->getRetryAfter()->getTimestamp());

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

$rateLimit = $limiter->consume(0);
$this->assertTrue($rateLimit->isAccepted());
$this->assertEquals(time(), $rateLimit->getRetryAfter()->getTimestamp());
}

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