Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  [AssetMapper] Fix `JavaScriptImportPathCompiler` regex for non-latin characters
  Definition::$class may not be class-string
  require Cache component versions compatible with Redis 6.1
  [Twitter][Notifier] Fix post INIT upload
  [Messenger][RateLimiter] fix additional message handled when using a rate limiter
  [Serializer] Revert default groups
  [Serializer] fixed object normalizer for a class with `cancel` method
  Fix bucket size reduce when previously created with bigger size
  • Loading branch information
xabbuh committed Nov 9, 2024
2 parents 83b447a + f1df8f0 commit bb6b14e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Policy/TokenBucketLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
$now = microtime(true);
$availableTokens = $bucket->getAvailableTokens($now);

if ($availableTokens > $this->maxBurst) {
$availableTokens = $this->maxBurst;
}

if ($availableTokens >= $tokens) {
// tokens are now available, update bucket
$bucket->setTokens($availableTokens - $tokens);
Expand Down
12 changes: 12 additions & 0 deletions Tests/Policy/TokenBucketLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public function testReserveMoreTokensThanBucketSize()
$limiter->reserve(15);
}

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

$limiter->consume();

$limiter2 = $this->createLimiter(1);
$limiter2->consume();

$this->assertFalse($limiter2->consume()->isAccepted());
}

public function testReserveMaxWaitingTime()
{
$limiter = $this->createLimiter(10, Rate::perMinute());
Expand Down

0 comments on commit bb6b14e

Please sign in to comment.