8000 [RateLimiter][Security] Improve performance of login/request rate limiter by Seldaek · Pull Request #46110 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RateLimiter][Security] Improve performance of login/request rate limiter #46110

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 5 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[RateLimiter] Fix peeking in token bucket/fixed window and add tests
  • Loading branch information
wouterj committed Jul 24, 2022
commit 5c360db7d4dc33ad66487f9386d2dfed6d97422e
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

$now = microtime(true);
$availableTokens = $window->getAvailableTokens($now);
$waitDuration = $window->calculateTimeForTokens(max(1, $tokens));

if ($availableTokens >= $tokens) {
if ($availableTokens >= max(1, $tokens)) {
$window->add($tokens, $now);

$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), true, $this->limit));
$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
} else {
$waitDuration = $window->calculateTimeForTokens(max(1, $tokens));

if (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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,17 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

$now = microtime(true);
$availableTokens = $bucket->getAvailableTokens($now);
$remainingTokens = $tokens - $availableTokens;
$waitDuration = $this->rate->calculateTimeForTokens(max(1, $remainingTokens));

if ($availableTokens >= $tokens) {
if ($availableTokens >= max(1, $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 + $waitDuration)), true, $this->maxBurst));
$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 (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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public function testWindowResilientToTimeShifting()
$this->assertSame(100, $window->getAvailableTokens($serverOneClock));
}

public function testPeekConsume()
{
$limiter = $this->createLimiter();

$limiter->consume(9);

// peek by consuming 0 tokens twice (making sure peeking doesn't claim a token)
for ($i = 0; $i < 2; ++$i) {
$rateLimit = $limiter->consume(0);
$this->assertSame(10, $rateLimit->getLimit());
$this->assertTrue($rateLimit->isAccepted());
}
}

public function provideConsumeOutsideInterval(): \Generator
{
yield ['PT15S'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public function testReserve()
$this->createLimiter()->reserve();
}

public function testPeekConsume()
{
$limiter = $this->createLimiter();

$limiter->consume(9);

for ($i = 0; $i < 2; ++$i) {
$rateLimit = $limiter->consume(0);
$this->assertTrue($rateLimit->isAccepted());
$this->assertSame(10, $rateLimit->getLimit());
}
}

private function createLimiter(): SlidingWindowLimiter
{
return new SlidingWindowLimiter('test', 10, new \DateInterval('PT12S'), $this->storage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ public function testBucketResilientToTimeShifting()
$this->assertSame(100, $bucket->getAvailableTokens($serverOneClock));
}

public function testPeekConsume()
{
$limiter = $this->createLimiter();

$limiter->consume(9);

for ($i = 0; $i < 2; ++$i) {
$rateLimit = $limiter->consume(0);
$this->assertTrue($rateLimit->isAccepted());
$this->assertSame(10, $rateLimit->getLimit()); 4EE3
}
}

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