Skip to content

[RateLimiter] Fix reservations outside the second fixed window #52413

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

Open
wants to merge 2 commits into
base: 6.3
Choose a base branch
from
Open
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
Next Next commit
Fix reservations outside the second fixed window
  • Loading branch information
SanderSander committed Nov 1, 2023
commit 62e3e5cda956655c1984a8663d422a59b3def5b4
3 changes: 2 additions & 1 deletion src/Symfony/Component/RateLimiter/Policy/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public function calculateTimeForTokens(int $tokens, float $now): int
return 0;
}

return (int) ceil($this->timer + $this->intervalInSeconds - $now);
$inWindow = ceil($this->hitCount / $this->maxSize);
return (int) ceil($this->timer + ($this->intervalInSeconds * $inWindow) - $now);
}

public function __serialize(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ public function testConsumeOutsideInterval(string $dateIntervalString)
$this->assertTrue($rateLimit->isAccepted());
}

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

// initial reserve
$limiter->reserve(10);

// Reserve the first window and the second window
$firstReservation = $limiter->reserve(10);
$secondReservation = $limiter->reserve(10);

$this->assertFalse($firstReservation->getRateLimit()->isAccepted());
$this->assertFalse($secondReservation->getRateLimit()->isAccepted());
$this->assertEquals(60, ceil($firstReservation->getWaitDuration()));
$this->assertEquals(120, ceil($secondReservation->getWaitDuration()));
}

public function testWaitIntervalOnConsumeOverLimit()
{
$limiter = $this->createLimiter();
Expand Down
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