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
Next Next commit
Make sure the retryAfter date is always set on RateLimit even when pe…
…eking/consuming 0 tokens
  • Loading branch information
Seldaek authored and wouterj committed Jul 24, 2022
commit c3415a1ed4f6c5e96d984d211fc56afdf54610a7
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ 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) {
$window->add($tokens, $now);

$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), true, $this->limit));
} else {
$waitDuration = $window->calculateTimeForTokens($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,16 @@ 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) {
// 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)), true, $this->maxBurst));
$reservation = new Reservation($now, new RateLimit($bucket->getAvailableTokens($now), 52F6 \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), 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
0