Skip to content

[RateLimiter] Add support for long intervals (months and years) #43060

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
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
1 change: 1 addition & 0 deletions src/Symfony/Component/RateLimiter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* The component is not experimental anymore
* Add support for long intervals (months and years)

5.2.0
-----
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/RateLimiter/Policy/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public static function perDay(int $rate = 1): self
return new static(new \DateInterval('P1D'), $rate);
}

public static function perMonth(int $rate = 1): self
{
return new static(new \DateInterval('P1M'), $rate);
}

public static function perYear(int $rate = 1): self
{
return new static(new \DateInterval('P1Y'), $rate);
}

/**
* @param string $string using the format: "%interval_spec%-%rate%", {@see DateInterval}
*/
Expand Down Expand Up @@ -91,6 +101,6 @@ public function calculateNewTokensDuringInterval(float $duration): int

public function __toString(): string
{
return $this->refillTime->format('P%dDT%HH%iM%sS').'-'.$this->refillAmount;
return $this->refillTime->format('P%y%m%dDT%HH%iM%sS').'-'.$this->refillAmount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\RateLimiter\Policy\FixedWindowLimiter;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;
use Symfony\Component\RateLimiter\Util\TimeUtil;

/**
* @group time-sensitive
Expand Down Expand Up @@ -49,16 +50,19 @@ public function testConsume()
$this->assertSame(10, $rateLimit->getLimit());
}

public function testConsumeOutsideInterval()
/**
* @dataProvider provideConsumeOutsideInterval
*/
public function testConsumeOutsideInterval(string $dateIntervalString)
{
$limiter = $this->createLimiter();
$limiter = $this->createLimiter($dateIntervalString);

// start window...
$limiter->consume();
// ...add a max burst at the end of the window...
sleep(55);
// ...add a max burst, 5 seconds before the end of the window...
sleep(TimeUtil::dateIntervalToSeconds(new \DateInterval($dateIntervalString)) - 5);
$limiter->consume(9);
// ...try bursting again at the start of the next window
// ...try bursting again at the start of the next window, 10 seconds later
sleep(10);
$rateLimit = $limiter->consume(10);
$this->assertEquals(0, $rateLimit->getRemainingTokens());
Expand All @@ -74,8 +78,21 @@ public function testWrongWindowFromCache()
$this->assertEquals(9, $rateLimit->getRemainingTokens());
}

private function createLimiter(): FixedWindowLimiter
private function createLimiter(string $dateIntervalString = 'PT1M'): FixedWindowLimiter
{
return new FixedWindowLimiter('test', 10, new \DateInterval($dateIntervalString), $this->storage);
}

public function provideConsumeOutsideInterval(): \Generator
{
return new FixedWindowLimiter('test', 10, new \DateInterval('PT1M'), $this->storage);
yield ['PT15S'];

yield ['PT1M'];

yield ['PT1H'];

yield ['P1M'];

yield ['P1Y'];
}
}
8 changes: 3 additions & 5 deletions src/Symfony/Component/RateLimiter/Util/TimeUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ final class TimeUtil
{
public static function dateIntervalToSeconds(\DateInterval $interval): int
{
return (float) $interval->format('%s') // seconds
+ $interval->format('%i') * 60 // minutes
+ $interval->format('%H') * 3600 // hours
+ $interval->format('%d') * 3600 * 24 // days
;
$now = new \DateTimeImmutable();

return ($now->add($interval))->getTimestamp() - $now->getTimestamp();
}
}