| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpFoundation\RateLimiter;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\RateLimiter\LimiterInterface;
|
|
|
16 |
use Symfony\Component\RateLimiter\Policy\NoLimiter;
|
|
|
17 |
use Symfony\Component\RateLimiter\RateLimit;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* An implementation of PeekableRequestRateLimiterInterface that
|
|
|
21 |
* fits most use-cases.
|
|
|
22 |
*
|
|
|
23 |
* @author Wouter de Jong <wouter@wouterj.nl>
|
|
|
24 |
*/
|
|
|
25 |
abstract class AbstractRequestRateLimiter implements PeekableRequestRateLimiterInterface
|
|
|
26 |
{
|
|
|
27 |
public function consume(Request $request): RateLimit
|
|
|
28 |
{
|
|
|
29 |
return $this->doConsume($request, 1);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function peek(Request $request): RateLimit
|
|
|
33 |
{
|
|
|
34 |
return $this->doConsume($request, 0);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
private function doConsume(Request $request, int $tokens): RateLimit
|
|
|
38 |
{
|
|
|
39 |
$limiters = $this->getLimiters($request);
|
|
|
40 |
if (0 === \count($limiters)) {
|
|
|
41 |
$limiters = [new NoLimiter()];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$minimalRateLimit = null;
|
|
|
45 |
foreach ($limiters as $limiter) {
|
|
|
46 |
$rateLimit = $limiter->consume($tokens);
|
|
|
47 |
|
|
|
48 |
$minimalRateLimit = $minimalRateLimit ? self::getMinimalRateLimit($minimalRateLimit, $rateLimit) : $rateLimit;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
return $minimalRateLimit;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
public function reset(Request $request): void
|
|
|
55 |
{
|
|
|
56 |
foreach ($this->getLimiters($request) as $limiter) {
|
|
|
57 |
$limiter->reset();
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @return LimiterInterface[] a set of limiters using keys extracted from the request
|
|
|
63 |
*/
|
|
|
64 |
abstract protected function getLimiters(Request $request): array;
|
|
|
65 |
|
|
|
66 |
private static function getMinimalRateLimit(RateLimit $first, RateLimit $second): RateLimit
|
|
|
67 |
{
|
|
|
68 |
if ($first->isAccepted() !== $second->isAccepted()) {
|
|
|
69 |
return $first->isAccepted() ? $second : $first;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$firstRemainingTokens = $first->getRemainingTokens();
|
|
|
73 |
$secondRemainingTokens = $second->getRemainingTokens();
|
|
|
74 |
|
|
|
75 |
if ($firstRemainingTokens === $secondRemainingTokens) {
|
|
|
76 |
return $first->getRetryAfter() < $second->getRetryAfter() ? $second : $first;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return $firstRemainingTokens > $secondRemainingTokens ? $second : $first;
|
|
|
80 |
}
|
|
|
81 |
}
|