1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace Symfony\Component\HttpFoundation\RateLimiter;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\RateLimiter\LimiterInterface;
- use Symfony\Component\RateLimiter\Policy\NoLimiter;
- use Symfony\Component\RateLimiter\RateLimit;
- abstract class AbstractRequestRateLimiter implements RequestRateLimiterInterface
- {
- public function consume(Request $request): RateLimit
- {
- $limiters = $this->getLimiters($request);
- if (0 === \count($limiters)) {
- $limiters = [new NoLimiter()];
- }
- $minimalRateLimit = null;
- foreach ($limiters as $limiter) {
- $rateLimit = $limiter->consume(1);
- $minimalRateLimit = $minimalRateLimit ? self::getMinimalRateLimit($minimalRateLimit, $rateLimit) : $rateLimit;
- }
- return $minimalRateLimit;
- }
- public function reset(Request $request): void
- {
- foreach ($this->getLimiters($request) as $limiter) {
- $limiter->reset();
- }
- }
-
- abstract protected function getLimiters(Request $request): array;
- private static function getMinimalRateLimit(RateLimit $first, RateLimit $second): RateLimit
- {
- if ($first->isAccepted() !== $second->isAccepted()) {
- return $first->isAccepted() ? $second : $first;
- }
- $firstRemainingTokens = $first->getRemainingTokens();
- $secondRemainingTokens = $second->getRemainingTokens();
- if ($firstRemainingTokens === $secondRemainingTokens) {
- return $first->getRetryAfter() < $second->getRetryAfter() ? $second : $first;
- }
- return $firstRemainingTokens > $secondRemainingTokens ? $second : $first;
- }
- }
|