-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCompoundLimiter.php
More file actions
61 lines (52 loc) · 1.58 KB
/
CompoundLimiter.php
File metadata and controls
61 lines (52 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?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 Symfony\Component\RateLimiter\Exception\ReserveNotSupportedException;
/**
* @author Wouter de Jong <wouter@wouterj.nl>
*/
final class CompoundLimiter implements LimiterInterface
{
/**
* @param LimiterInterface[] $limiters
*/
public function __construct(
private array $limiters,
) {
if (!$limiters) {
throw new \LogicException(\sprintf('"%s::%s()" require at least one limiter.', self::class, __METHOD__));
}
}
public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
{
throw new ReserveNotSupportedException(__CLASS__);
}
public function consume(int $tokens = 1): RateLimit
{
$minimalRateLimit = null;
foreach ($this->limiters as $limiter) {
$rateLimit = $limiter->consume($tokens);
if (
null === $minimalRateLimit
|| $rateLimit->getRemainingTokens() < $minimalRateLimit->getRemainingTokens()
|| ($minimalRateLimit->isAccepted() && !$rateLimit->isAccepted())
) {
$minimalRateLimit = $rateLimit;
}
}
return $minimalRateLimit;
}
public function reset(): void
{
foreach ($this->limiters as $limiter) {
$limiter->reset();
}
}
}