Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[RateLimiter] add the ability to use a Clock inside the RateLimiter #49222

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
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 @@ -25,6 +25,7 @@
abstract_arg('config'),
abstract_arg('storage'),
null,
service('clock')->nullOnInvalid(),
])
;
};
35 changes: 35 additions & 0 deletions 35 src/Symfony/Component/RateLimiter/ClockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\RateLimiter;

use Psr\Clock\ClockInterface;

/**
* @internal
*/
trait ClockTrait
{
private ?ClockInterface $clock;

/**
* @internal
*/
public function setClock(?ClockInterface $clock): void
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
{
$this->clock = $clock;
}

private function now(): float
{
return (float) ($this->clock?->now()->format('U.u') ?? microtime(true));
}
}
19 changes: 13 additions & 6 deletions 19 src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\RateLimiter\Policy;

use Psr\Clock\ClockInterface;
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\RateLimiter\ClockTrait;
use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
Expand All @@ -24,6 +26,7 @@
*/
final class FixedWindowLimiter implements LimiterInterface
{
use ClockTrait;
use ResetLimiterTrait;

private int $interval;
Expand All @@ -34,6 +37,7 @@ public function __construct(
\DateInterval $interval,
StorageInterface $storage,
?LockInterface $lock = null,
?ClockInterface $clock = null,
) {
if ($limit < 1) {
throw new \InvalidArgumentException(\sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', __CLASS__));
Expand All @@ -43,6 +47,7 @@ public function __construct(
$this->lock = $lock;
$this->id = $id;
$this->interval = TimeUtil::dateIntervalToSeconds($interval);
$this->setClock($clock);
}

public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
Expand All @@ -56,30 +61,32 @@ public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
try {
$window = $this->storage->fetch($this->id);
if (!$window instanceof Window) {
$window = new Window($this->id, $this->interval, $this->limit);
$window = new Window($this->id, $this->interval, $this->limit, $this->clock);
xabbuh marked this conversation as resolved.
Show resolved Hide resolved
} else {
$window->setClock($this->clock);
}

$now = microtime(true);
$now = $this->now();
$availableTokens = $window->getAvailableTokens($now);

if (0 === $tokens) {
$waitDuration = $window->calculateTimeForTokens(1, $now);
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), true, $this->limit));
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), true, $this->limit), $this->clock);
} elseif ($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)), true, $this->limit, $this->clock), $this->clock);
} else {
$waitDuration = $window->calculateTimeForTokens($tokens, $now);

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));
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, $this->clock));
}

$window->add($tokens, $now);

$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit, $this->clock), $this->clock);
}

if (0 < $tokens) {
Expand Down
13 changes: 11 additions & 2 deletions 13 src/Symfony/Component/RateLimiter/Policy/NoLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\RateLimiter\Policy;

use Psr\Clock\ClockInterface;
use Symfony\Component\RateLimiter\ClockTrait;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;
Expand All @@ -25,14 +27,21 @@
*/
final class NoLimiter implements LimiterInterface
{
use ClockTrait;

public function __construct(?ClockInterface $clock = null)
{
$this->setClock($clock);
}

public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
{
return new Reservation(microtime(true), new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX));
return new Reservation($this->now(), new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX, $this->clock), $this->clock);
}

public function consume(int $tokens = 1): RateLimit
{
return new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX);
return new RateLimit(\PHP_INT_MAX, new \DateTimeImmutable(), true, \PHP_INT_MAX, $this->clock);
}

public function reset(): void
Expand Down
20 changes: 13 additions & 7 deletions 20 src/Symfony/Component/RateLimiter/Policy/SlidingWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\RateLimiter\Policy;

use Psr\Clock\ClockInterface;
use Symfony\Component\RateLimiter\ClockTrait;
use Symfony\Component\RateLimiter\Exception\InvalidIntervalException;
use Symfony\Component\RateLimiter\LimiterStateInterface;

Expand All @@ -21,26 +23,30 @@
*/
final class SlidingWindow implements LimiterStateInterface
{
use ClockTrait;

private int $hitCount = 0;
private int $hitCountForLastWindow = 0;
private float $windowEndAt;

public function __construct(
private string $id,
private int $intervalInSeconds,
?ClockInterface $clock = null,
) {
if ($intervalInSeconds < 1) {
throw new InvalidIntervalException(\sprintf('The interval must be positive integer, "%d" given.', $intervalInSeconds));
}
$this->windowEndAt = microtime(true) + $intervalInSeconds;
$this->setClock($clock);
$this->windowEndAt = $this->now() + $intervalInSeconds;
}

public static function createFromPreviousWindow(self $window, int $intervalInSeconds): self
public static function createFromPreviousWindow(self $window, int $intervalInSeconds, ?ClockInterface $clock = null): self
{
$new = new self($window->id, $intervalInSeconds);
$windowEndAt = $window->windowEndAt + $intervalInSeconds;

if (microtime(true) < $windowEndAt) {
if (($clock?->now()->format('U.u') ?? microtime(true)) < $windowEndAt) {
$new->hitCountForLastWindow = $window->hitCount;
$new->windowEndAt = $windowEndAt;
}
Expand All @@ -58,12 +64,12 @@ public function getId(): string
*/
public function getExpirationTime(): int
{
return (int) ($this->windowEndAt + $this->intervalInSeconds - microtime(true));
return (int) ($this->windowEndAt + $this->intervalInSeconds - $this->now());
}

public function isExpired(): bool
{
return microtime(true) > $this->windowEndAt;
return $this->now() > $this->windowEndAt;
}

public function add(int $hits = 1): void
Expand All @@ -77,7 +83,7 @@ public function add(int $hits = 1): void
public function getHitCount(): int
{
$startOfWindow = $this->windowEndAt - $this->intervalInSeconds;
$percentOfCurrentTimeFrame = min((microtime(true) - $startOfWindow) / $this->intervalInSeconds, 1);
$percentOfCurrentTimeFrame = min(($this->now() - $startOfWindow) / $this->intervalInSeconds, 1);

return (int) floor($this->hitCountForLastWindow * (1 - $percentOfCurrentTimeFrame) + $this->hitCount);
}
Expand All @@ -89,7 +95,7 @@ public function calculateTimeForTokens(int $maxSize, int $tokens): float
return 0;
}

$time = microtime(true);
$time = $this->now();
$startOfWindow = $this->windowEndAt - $this->intervalInSeconds;
$timePassed = $time - $startOfWindow;
$windowPassed = min($timePassed / $this->intervalInSeconds, 1);
Expand Down
26 changes: 18 additions & 8 deletions 26 src/Symfony/Component/RateLimiter/Policy/SlidingWindowLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\RateLimiter\Policy;

use Psr\Clock\ClockInterface;
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\RateLimiter\ClockTrait;
use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimit;
Expand All @@ -32,6 +34,7 @@
*/
final class SlidingWindowLimiter implements LimiterInterface
{
use ClockTrait;
use ResetLimiterTrait;

private int $interval;
Expand All @@ -42,11 +45,13 @@ public function __construct(
\DateInterval $interval,
StorageInterface $storage,
?LockInterface $lock = null,
?ClockInterface $clock = null,
) {
$this->storage = $storage;
$this->lock = $lock;
$this->id = $id;
$this->interval = TimeUtil::dateIntervalToSeconds($interval);
$this->setClock($clock);
}

public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
Expand All @@ -59,25 +64,30 @@ public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation

try {
$window = $this->storage->fetch($this->id);
if (!$window instanceof SlidingWindow) {
$window = new SlidingWindow($this->id, $this->interval);
} elseif ($window->isExpired()) {
$window = SlidingWindow::createFromPreviousWindow($window, $this->interval);

if ($window instanceof SlidingWindow) {
$window->setClock($this->clock);

if ($window->isExpired()) {
$window = SlidingWindow::createFromPreviousWindow($window, $this->interval, $this->clock);
}
} else {
$window = new SlidingWindow($this->id, $this->interval, $this->clock);
}

$now = microtime(true);
$now = $this->now();
$hitCount = $window->getHitCount();
$availableTokens = $this->getAvailableTokens($hitCount);
if (0 === $tokens) {
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount());
$resetTime = \DateTimeImmutable::createFromFormat('U', $availableTokens ? floor($now) : floor($now + $resetDuration));

return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit));
return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit, $this->clock), $this->clock);
}
if ($availableTokens >= $tokens) {
$window->add($tokens);

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

Expand All @@ -88,7 +98,7 @@ public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation

$window->add($tokens);

$reservation = new Reservation($now + $waitDuration, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
$reservation = new Reservation($now + $waitDuration, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit), $this->clock);
}

if (0 < $tokens) {
Expand Down
12 changes: 8 additions & 4 deletions 12 src/Symfony/Component/RateLimiter/Policy/TokenBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\RateLimiter\Policy;

use Psr\Clock\ClockInterface;
use Symfony\Component\RateLimiter\ClockTrait;
use Symfony\Component\RateLimiter\LimiterStateInterface;

/**
Expand All @@ -20,6 +22,8 @@
*/
final class TokenBucket implements LimiterStateInterface
{
use ClockTrait;

private int $tokens;
private int $burstSize;
private float $timer;
Expand All @@ -28,22 +32,22 @@ final class TokenBucket implements LimiterStateInterface
* @param string $id unique identifier for this bucket
* @param int $initialTokens the initial number of tokens in the bucket (i.e. the max burst size)
* @param Rate $rate the fill rate and time of this bucket
* @param float|null $timer the current timer of the bucket, defaulting to microtime(true)
* @param float|null $timer the current timer of the bucket, defaulting to the current time in microseconds
*/
public function __construct(
private string $id,
int $initialTokens,
private Rate $rate,
?ClockInterface $clock = null,
?float $timer = null,
) {
if ($initialTokens < 1) {
throw new \InvalidArgumentException(\sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', TokenBucketLimiter::class));
}

$this->id = $id;
$this->tokens = $this->burstSize = $initialTokens;
$this->rate = $rate;
$this->timer = $timer ?? microtime(true);
$this->setClock($clock);
$this->timer = $timer ?? $this->now();
}

public function getId(): string
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.