Skip to content

Navigation Menu

Sign in
Appearance settings

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] Make RateLimiter resilient to timeShifting #44354

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
Dec 15, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
$now = microtime(true);
$availableTokens = $window->getAvailableTokens($now);
if ($availableTokens >= $tokens) {
$window->add($tokens);
$window->add($tokens, $now);

$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
} else {
Expand All @@ -77,7 +77,7 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
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);
$window->add($tokens, $now);

$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
}
Expand Down
2 changes: 1 addition & 1 deletion 2 src/Symfony/Component/RateLimiter/Policy/TokenBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function setTokens(int $tokens): void

public function getAvailableTokens(float $now): int
{
$elapsed = $now - $this->timer;
$elapsed = max(0, $now - $this->timer);

return min($this->burstSize, $this->tokens + $this->rate->calculateNewTokensDuringInterval($elapsed));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ 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(0);
$bucket->setTimer($now + $waitDuration);
$bucket->setTokens($availableTokens - $tokens);
$bucket->setTimer($now);

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

$this->storage->save($bucket);
Expand Down
5 changes: 0 additions & 5 deletions 5 src/Symfony/Component/RateLimiter/Policy/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ public function getHitCount(): int

public function getAvailableTokens(float $now)
{
// if timer is in future, there are no tokens available anymore
if ($this->timer > $now) {
return 0;
}

// if now is more than the window interval in the past, all tokens are available
if (($now - $this->timer) > $this->intervalInSeconds) {
return $this->maxSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Policy\Window;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;
Expand Down Expand Up @@ -90,6 +91,19 @@ public function testWrongWindowFromCache()
$this->assertEquals(9, $rateLimit->getRemainingTokens());
}

public function testWindowResilientToTimeShifting()
{
$serverOneClock = microtime(true) - 1;
$serverTwoClock = microtime(true) + 1;
$window = new Window('id', 300, 100, $serverTwoClock);
$this->assertSame(100, $window->getAvailableTokens($serverTwoClock));
$this->assertSame(100, $window->getAvailableTokens($serverOneClock));

$window = new Window('id', 300, 100, $serverOneClock);
$this->assertSame(100, $window->getAvailableTokens($serverTwoClock));
$this->assertSame(100, $window->getAvailableTokens($serverOneClock));
}

private function createLimiter(): FixedWindowLimiter
{
return new FixedWindowLimiter('test', 10, new \DateInterval('PT1M'), $this->storage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ public function testWrongWindowFromCache()
$this->assertEquals(9, $rateLimit->getRemainingTokens());
}

public function testBucketResilientToTimeShifting()
{
$serverOneClock = microtime(true) - 1;
$serverTwoClock = microtime(true) + 1;

$bucket = new TokenBucket('id', 100, new Rate(\DateInterval::createFromDateString('5 minutes'), 10), $serverTwoClock);
$this->assertSame(100, $bucket->getAvailableTokens($serverTwoClock));
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));

$bucket = new TokenBucket('id', 100, new Rate(\DateInterval::createFromDateString('5 minutes'), 10), $serverOneClock);
$this->assertSame(100, $bucket->getAvailableTokens($serverTwoClock));
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));
}

private function createLimiter($initialTokens = 10, Rate $rate = null)
{
return new TokenBucketLimiter('test', $initialTokens, $rate ?? Rate::perSecond(10), $this->storage);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.