8000 Add error when limiter is set-up with limit=0 · symfony/symfony@87a3ec8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 87a3ec8

Browse files
committed
Add error when limiter is set-up with limit=0
1 parent 319afde commit 87a3ec8

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/Symfony/Component/RateLimiter/FixedWindowLimiter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ final class FixedWindowLimiter implements LimiterInterface
3434

3535
public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, ?LockInterface $lock = null)
3636
{
37+
if ($limit < 1) {
38+
throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', __CLASS__));
39+
}
40+
3741
$this->storage = $storage;
3842
$this->lock = $lock ?? new NoLock();
3943
$this->id = $id;

src/Symfony/Component/RateLimiter/TokenBucket.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ final class TokenBucket implements LimiterStateInterface
3333
*/
3434
public function __construct(string $id, int $initialTokens, Rate $rate, ?float $timer = null)
3535
{
36+
if ($initialTokens < 1) {
37+
throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', TokenBucketLimiter::class));
38+
}
39+
3640
$this->id = $id;
3741
$this->tokens = $this->burstSize = $initialTokens;
3842
$this->rate = $rate;

0 commit comments

Comments
 (0)
0