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

Commit eea8d30

Browse filesBrowse files
committed
Make RateLimiter resilient to timeShifting
1 parent e9a7026 commit eea8d30
Copy full SHA for eea8d30

File tree

6 files changed

+35
-11
lines changed
Filter options

6 files changed

+35
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/TokenBucket.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/TokenBucketLimiter.php
+3-3Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/Window.php
-5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Tests/Policy/FixedWindowLimiterTest.php
+14Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Tests/Policy/TokenBucketLimiterTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\RateLimiter\Policy\Rate;
1818
use Symfony\Component\RateLimiter\Policy\TokenBucket;
1919
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
20+
use Symfony\Component\RateLimiter\Policy\Window;
2021
use Symfony\Component\RateLimiter\RateLimit;
2122
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
2223
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;
@@ -114,6 +115,20 @@ public function testWrongWindowFromCache()
114115
$this->assertEquals(9, $rateLimit->getRemainingTokens());
115116
}
116117

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.