Skip to content

[RateLimiter] TokenBucket policy fix for adding tokens with a predefined frequency #44766

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

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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
12 changes: 12 additions & 0 deletions src/Symfony/Component/RateLimiter/Policy/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public function calculateNewTokensDuringInterval(float $duration): int
return $cycles * $this->refillAmount;
}

/**
* Calculates total amount in seconds of refill intervals during $duration (for maintain strict refill frequency).
*
* @param float $duration interval in seconds
*/
public function calculateRefillInterval(float $duration): int
{
$cycleTime = TimeUtil::dateIntervalToSeconds($this->refillTime);

return floor($duration / $cycleTime) * $cycleTime;
}

public function __toString(): string
{
return $this->refillTime->format('P%dDT%HH%iM%sS').'-'.$this->refillAmount;
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/RateLimiter/Policy/TokenBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public function setTokens(int $tokens): void
public function getAvailableTokens(float $now): int
{
$elapsed = max(0, $now - $this->timer);
$newTokens = $this->rate->calculateNewTokensDuringInterval($elapsed);

return min($this->burstSize, $this->tokens + $this->rate->calculateNewTokensDuringInterval($elapsed));
if ($newTokens > 0) {
$this->timer += $this->rate->calculateRefillInterval($elapsed);
}

return min($this->burstSize, $this->tokens + $newTokens);
}

public function getExpirationTime(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
if ($availableTokens >= $tokens) {
// tokens are now available, update bucket
$bucket->setTokens($availableTokens - $tokens);
$bucket->setTimer($now);

$reservation = new Reservation($now, new RateLimit($bucket->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->maxBurst));
} else {
Expand All @@ -89,7 +88,6 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
// at $now + $waitDuration all tokens will be reserved for this process,
// so no tokens are left for other processes.
$bucket->setTokens($availableTokens - $tokens);
$bucket->setTimer($now);

$reservation = new Reservation($now + $waitDuration, new RateLimit(0, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->maxBurst));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@ public function testBucketResilientToTimeShifting()
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));
}

public function testBucketRefilledWithStrictFrequency()
{
$limiter = $this->createLimiter(1000, new Rate(\DateInterval::createFromDateString('15 seconds'), 100));
$rateLimit = $limiter->consume(300);

$this->assertTrue($rateLimit->isAccepted());
$this->assertEquals(700, $rateLimit->getRemainingTokens());

$expected = 699;

for ($i = 1; $i <= 20; ++$i) {
$rateLimit = $limiter->consume();
$this->assertTrue($rateLimit->isAccepted());
$this->assertEquals($expected, $rateLimit->getRemainingTokens());

sleep(4);
--$expected;

if (\in_array($i, [4, 8, 12], true)) {
$expected += 100;
} elseif (\in_array($i, [15, 19], true)) {
$expected = 999;
}
}
}

private function createLimiter($initialTokens = 10, Rate $rate = null)
{
return new TokenBucketLimiter('test', $initialTokens, $rate ?? Rate::perSecond(10), $this->storage);
Expand Down