Skip to content

[RateLimiter] Fix wait duration for fixed window policy #42167

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,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 {
$remainingTokens = $tokens - $availableTokens;
$waitDuration = $window->calculateTimeForTokens($remainingTokens);
$waitDuration = $this->interval;

if (null !== $maxTime && $waitDuration > $maxTime) {
// process needs to wait longer than set interval
Expand Down
11 changes: 0 additions & 11 deletions src/Symfony/Component/RateLimiter/Policy/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,4 @@ public function getAvailableTokens(float $now)

return $this->maxSize - $this->hitCount;
}

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

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

return $cyclesRequired * $this->intervalInSeconds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;

Expand All @@ -29,6 +30,7 @@ protected function setUp(): void
$this->storage = new InMemoryStorage();

ClockMock::register(InMemoryStorage::class);
ClockMock::register(RateLimit::class);
}

public function testConsume()
Expand Down Expand Up @@ -65,6 +67,20 @@ public function testConsumeOutsideInterval()
$this->assertTrue($rateLimit->isAccepted());
}

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

// initial consume
$limiter->consume(8);
// consumer over the limit
$rateLimit = $limiter->consume(4);

$start = microtime(true);
$rateLimit->wait(); // wait 1 minute
$this->assertEqualsWithDelta($start + 60, microtime(true), 0.5);
}

public function testWrongWindowFromCache()
{
$this->storage->save(new DummyWindow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\RateLimiter\Exception\ReserveNotSupportedException;
use Symfony\Component\RateLimiter\Policy\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

/**
Expand All @@ -29,6 +30,7 @@ protected function setUp(): void
$this->storage = new InMemoryStorage();

ClockMock::register(InMemoryStorage::class);
ClockMock::register(RateLimit::class);
}

public function testConsume()
Expand All @@ -53,6 +55,20 @@ public function testConsume()
$this->assertSame(10, $rateLimit->getLimit());
}

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

// initial consume
$limiter->consume(8);
// consumer over the limit
$rateLimit = $limiter->consume(4);

$start = microtime(true);
$rateLimit->wait(); // wait 12 seconds
$this->assertEqualsWithDelta($start + 12, microtime(true), 0.5);
}

public function testReserve()
{
$this->expectException(ReserveNotSupportedException::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\RateLimiter\Policy\Rate;
use Symfony\Component\RateLimiter\Policy\TokenBucket;
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;

Expand All @@ -34,6 +35,7 @@ protected function setUp(): void
ClockMock::register(TokenBucketLimiter::class);
ClockMock::register(InMemoryStorage::class);
ClockMock::register(TokenBucket::class);
ClockMock::register(RateLimit::class);
}

public function testReserve()
Expand Down Expand Up @@ -89,6 +91,20 @@ public function testConsume()
$this->assertSame(10, $rateLimit->getLimit());
}

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

// initial consume
$limiter->consume(8);
// consumer over the limit
$rateLimit = $limiter->consume(4);

$start = microtime(true);
$rateLimit->wait(); // wait 1 second
$this->assertEqualsWithDelta($start + 1, microtime(true), 0.5);
}

public function testWrongWindowFromCache()
{
$this->storage->save(new DummyWindow());
Expand Down