Skip to content

[RateLimiter] Always store SlidingWindows with an expiration set #44996

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 1 commit into from
Mar 29, 2022
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
41 changes: 18 additions & 23 deletions src/Symfony/Component/RateLimiter/Policy/SlidingWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ final class SlidingWindow implements LimiterStateInterface
*/
private $windowEndAt;

/**
* @var bool true if this window has been cached
*/
private $cached = true;

public function __construct(string $id, int $intervalInSeconds)
{
if ($intervalInSeconds < 1) {
Expand All @@ -56,7 +51,6 @@ public function __construct(string $id, int $intervalInSeconds)
$this->id = $id;
$this->intervalInSeconds = $intervalInSeconds;
$this->windowEndAt = microtime(true) + $intervalInSeconds;
$this->cached = false;
}

public static function createFromPreviousWindow(self $window, int $intervalInSeconds): self
Expand All @@ -72,31 +66,17 @@ public static function createFromPreviousWindow(self $window, int $intervalInSec
return $new;
}

/**
* @internal
*/
public function __sleep(): array
{
// $cached is not serialized, it should only be set
// upon first creation of the window.
return ['id', 'hitCount', 'intervalInSeconds', 'hitCountForLastWindow', 'windowEndAt'];
}

public function getId(): string
{
return $this->id;
}

/**
* Store for the rest of this time frame and next.
* Returns the remaining of this timeframe and the next one.
*/
public function getExpirationTime(): ?int
public function getExpirationTime(): int
{
if ($this->cached) {
return null;
}

return 2 * $this->intervalInSeconds;
return $this->windowEndAt + $this->intervalInSeconds - microtime(true);
}

public function isExpired(): bool
Expand Down Expand Up @@ -124,4 +104,19 @@ public function getRetryAfter(): \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', $this->windowEndAt));
}

public function __serialize(): array
{
return [
pack('NNN', $this->hitCount, $this->hitCountForLastWindow, $this->intervalInSeconds).$this->id => $this->windowEndAt,
];
}

public function __unserialize(array $data): void
{
$pack = key($data);
$this->windowEndAt = $data[$pack];
['a' => $this->hitCount, 'b' => $this->hitCountForLastWindow, 'c' => $this->intervalInSeconds] = unpack('Na/Nb/Nc', $pack);
$this->id = substr($pack, 12);
}
}
34 changes: 20 additions & 14 deletions src/Symfony/Component/RateLimiter/Policy/TokenBucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
final class TokenBucket implements LimiterStateInterface
{
private $stringRate;
private $id;
private $rate;

Expand All @@ -47,8 +46,6 @@ final class TokenBucket implements LimiterStateInterface
*/
public function __construct(string $id, int $initialTokens, Rate $rate, float $timer = null)
{
unset($this->stringRate);

if ($initialTokens < 1) {
throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', TokenBucketLimiter::class));
}
Expand Down Expand Up @@ -91,26 +88,35 @@ public function getExpirationTime(): int
return $this->rate->calculateTimeForTokens($this->burstSize);
}

/**
* @internal
*/
public function __serialize(): array
{
return [
pack('N', $this->burstSize).$this->id => $this->tokens,
(string) $this->rate => $this->timer,
];
}

public function __unserialize(array $data): void
{
[$this->tokens, $this->timer] = array_values($data);
[$pack, $rate] = array_keys($data);
$this->rate = Rate::fromString($rate);
$this->burstSize = unpack('Na', $pack)['a'];
$this->id = substr($pack, 4);
}

public function __sleep(): array
{
$this->stringRate = (string) $this->rate;

return ['id', 'tokens', 'timer', 'burstSize', 'stringRate'];
}

/**
* @internal
*/
public function __wakeup(): void
{
if (!\is_string($this->stringRate)) {
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
if (\is_string($rate = $this->stringRate ?? null)) {
$this->rate = Rate::fromString($rate);
unset($this->stringRate);
}

$this->rate = Rate::fromString($this->stringRate);
unset($this->stringRate);
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/RateLimiter/Policy/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ public function calculateTimeForTokens(int $tokens): int

return $cyclesRequired * $this->intervalInSeconds;
}

public function __serialize(): array
{
return [
$this->id => $this->timer,
pack('NN', $this->hitCount, $this->intervalInSeconds) => $this->maxSize,
];
}

public function __unserialize(array $data): void
{
[$this->timer, $this->maxSize] = array_values($data);
[$this->id, $pack] = array_keys($data);
['a' => $this->hitCount, 'b' => $this->intervalInSeconds] = unpack('Na/Nb', $pack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function testGetExpirationTime()
$this->assertSame(2 * 10, $window->getExpirationTime());

$data = serialize($window);
sleep(10);
$cachedWindow = unserialize($data);
$this->assertNull($cachedWindow->getExpirationTime());
$this->assertSame(10, $cachedWindow->getExpirationTime());

$new = SlidingWindow::createFromPreviousWindow($cachedWindow, 15);
$this->assertSame(2 * 15, $new->getExpirationTime());
Expand Down