Skip to content

Commit

Permalink
Merge branch '6.2' into 6.3
Browse files Browse the repository at this point in the history
* 6.2:
  [Security] Allow custom scheme to be used as redirection URIs
  [Validator] Do not mock metadata factory on debug command tests
  [HttpKernel][WebProfilerBundle] Fix search feature
  update Intl component to take into account B-variant when converting Alpha3 to Alpha2. fixing issue with Darwin.
  [VarDumper] Fix dumping `ArrayObject` with `DumpDataCollector`
  [VarDumper] Add tests to demonstrate a bug when dumping ArrayObject with full stack fmk
  [DebugBundle][FrameworkBundle] Fix using the framework without the Console component
  [FrameworkBundle] Add missing monolog channel tag to the `messenger:failed:retry` command
  fetch all known ChoiceType values at once
  [RateLimiter] fix incorrect retryAfter of FixedWindow
  Fix Finder phpdoc
  • Loading branch information
nicolas-grekas committed Jul 13, 2023
2 parents a8aff62 + a659a52 commit 5223387
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Policy/FixedWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation

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

if (null !== $maxTime && $waitDuration > $maxTime) {
// process needs to wait longer than set interval
Expand Down
6 changes: 2 additions & 4 deletions Policy/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@ public function getAvailableTokens(float $now): int
return $this->maxSize - $this->hitCount;
}

public function calculateTimeForTokens(int $tokens): int
public function calculateTimeForTokens(int $tokens, float $now): int
{
if (($this->maxSize - $this->hitCount) >= $tokens) {
return 0;
}

$cyclesRequired = ceil($tokens / $this->maxSize);

return $cyclesRequired * $this->intervalInSeconds;
return (int) ceil($this->timer + $this->intervalInSeconds - $now);
}

public function __serialize(): array
Expand Down
4 changes: 4 additions & 0 deletions Tests/Policy/FixedWindowLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function setUp(): void

public function testConsume()
{
$now = time();
$limiter = $this->createLimiter();

// fill 9 tokens in 45 seconds
Expand All @@ -51,6 +52,9 @@ public function testConsume()
$rateLimit = $limiter->consume();
$this->assertFalse($rateLimit->isAccepted());
$this->assertSame(10, $rateLimit->getLimit());
// Window ends after 1 minute
$retryAfter = \DateTimeImmutable::createFromFormat('U', $now + 60);
$this->assertEquals($retryAfter, $rateLimit->getRetryAfter());
}

/**
Expand Down

0 comments on commit 5223387

Please sign in to comment.