Skip to content

Commit 9b1dcc5

Browse files
bug #44354 [RateLimiter] Make RateLimiter resilient to timeShifting (jderusse)
This PR was merged into the 5.3 branch. Discussion ---------- [RateLimiter] Make RateLimiter resilient to timeShifting | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - When 2 servers use the FixedWindowLimiter and do not have the exact same clock (with microsecond precision) the `Window` might return 0 available tokens. Commits ------- 6918a21 Make RateLimiter resilient to timeShifting
2 parents fdc61b4 + 6918a21 commit 9b1dcc5

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
6666
$now = microtime(true);
6767
$availableTokens = $window->getAvailableTokens($now);
6868
if ($availableTokens >= $tokens) {
69-
$window->add($tokens);
69+
$window->add($tokens, $now);
7070

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

80-
$window->add($tokens);
80+
$window->add($tokens, $now);
8181

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

src/Symfony/Component/RateLimiter/Policy/TokenBucket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function setTokens(int $tokens): void
8282

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

8787
return min($this->burstSize, $this->tokens + $this->rate->calculateNewTokensDuringInterval($elapsed));
8888
}

src/Symfony/Component/RateLimiter/Policy/TokenBucketLimiter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
8888

8989
// at $now + $waitDuration all tokens will be reserved for this process,
9090
// so no tokens are left for other processes.
91-
$bucket->setTokens(0);
92-
$bucket->setTimer($now + $waitDuration);
91+
$bucket->setTokens($availableTokens - $tokens);
92+
$bucket->setTimer($now);
9393

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

9797
$this->storage->save($bucket);

src/Symfony/Component/RateLimiter/Policy/Window.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ public function getHitCount(): int
6868

6969
public function getAvailableTokens(float $now)
7070
{
71-
// if timer is in future, there are no tokens available anymore
72-
if ($this->timer > $now) {
73-
return 0;
74-
}
75-
7671
// if now is more than the window interval in the past, all tokens are available
7772
if (($now - $this->timer) > $this->intervalInSeconds) {
7873
return $this->maxSize;

src/Symfony/Component/RateLimiter/Tests/Policy/FixedWindowLimiterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ClockMock;
1616
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
17+
use Symfony\Component\RateLimiter\Policy\Window;
1718
use Symfony\Component\RateLimiter\RateLimit;
1819
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
1920
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;
@@ -90,6 +91,19 @@ public function testWrongWindowFromCache()
9091
$this->assertEquals(9, $rateLimit->getRemainingTokens());
9192
}
9293

94+
public function testWindowResilientToTimeShifting()
95+
{
96+
$serverOneClock = microtime(true) - 1;
97+
$serverTwoClock = microtime(true) + 1;
98+
$window = new Window('id', 300, 100, $serverTwoClock);
99+
$this->assertSame(100, $window->getAvailableTokens($serverTwoClock));
100+
$this->assertSame(100, $window->getAvailableTokens($serverOneClock));
101+
102+
$window = new Window('id', 300, 100, $serverOneClock);
103+
$this->assertSame(100, $window->getAvailableTokens($serverTwoClock));
104+
$this->assertSame(100, $window->getAvailableTokens($serverOneClock));
105+
}
106+
93107
private function createLimiter(): FixedWindowLimiter
94108
{
95109
return new FixedWindowLimiter('test', 10, new \DateInterval('PT1M'), $this->storage);

src/Symfony/Component/RateLimiter/Tests/Policy/TokenBucketLimiterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ public function testWrongWindowFromCache()
114114
$this->assertEquals(9, $rateLimit->getRemainingTokens());
115115
}
116116

117+
public function testBucketResilientToTimeShifting()
118+
{
119+
$serverOneClock = microtime(true) - 1;
120+
$serverTwoClock = microtime(true) + 1;
121+
122+
$bucket = new TokenBucket('id', 100, new Rate(\DateInterval::createFromDateString('5 minutes'), 10), $serverTwoClock);
123+
$this->assertSame(100, $bucket->getAvailableTokens($serverTwoClock));
124+
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));
125+
126+
$bucket = new TokenBucket('id', 100, new Rate(\DateInterval::createFromDateString('5 minutes'), 10), $serverOneClock);
127+
$this->assertSame(100, $bucket->getAvailableTokens($serverTwoClock));
128+
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));
129+
}
130+
117131
private function createLimiter($initialTokens = 10, Rate $rate = null)
118132
{
119133
return new TokenBucketLimiter('test', $initialTokens, $rate ?? Rate::perSecond(10), $this->storage);

0 commit comments

Comments
 (0)
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