|
| 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\RateLimiter; |
| 13 | + |
| 14 | +use Symfony\Component\Lock\LockInterface; |
| 15 | +use Symfony\Component\RateLimiter\Exception\MaxWaitDurationExceededException; |
| 16 | +use Symfony\Component\RateLimiter\Storage\StorageInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Wouter de Jong <wouter@wouterj.nl> |
| 20 | + */ |
| 21 | +class Limiter implements LimiterInterface |
| 22 | +{ |
| 23 | + private $maxBurst; |
| 24 | + private $bucket; |
| 25 | + private $storage; |
| 26 | + private $lock; |
| 27 | + |
| 28 | + public function __construct(float $maxBurst, TokenBucket $bucket, StorageInterface $storage, LockInterface $lock) |
| 29 | + { |
| 30 | + $this->maxBurst = $maxBurst; |
| 31 | + $this->bucket = $bucket; |
| 32 | + $this->storage = $storage; |
| 33 | + $this->lock = $lock; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritDoc} |
| 38 | + * |
| 39 | + * @throws \InvalidArgumentException if $tokens is larger than the maximum burst size |
| 40 | + */ |
| 41 | + public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation |
| 42 | + { |
| 43 | + if ($tokens > $this->maxBurst) { |
| 44 | + throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the burst size of the rate limiter (%d).', $tokens, $this->maxBurst)); |
| 45 | + } |
| 46 | + |
| 47 | + $this->lock->acquire(true); |
| 48 | + |
| 49 | + try { |
| 50 | + $now = microtime(true); |
| 51 | + $bucket = $this->storage->fetch($this->bucket->getId()); |
| 52 | + if (null !== $bucket) { |
| 53 | + $this->bucket = $bucket; |
| 54 | + } |
| 55 | + $elapsed = $now - $this->bucket->getTimer(); |
| 56 | + |
| 57 | + $availableTokens = $this->bucket->getAvailableTokens($elapsed); |
| 58 | + if ($availableTokens >= $tokens) { |
| 59 | + // tokens are now available, update bucket |
| 60 | + $this->bucket->setTokens($availableTokens - $tokens); |
| 61 | + $this->bucket->setTimer($now); |
| 62 | + |
| 63 | + $reservation = new Reservation($tokens, $now); |
| 64 | + } else { |
| 65 | + $remainingTokens = $tokens - $availableTokens; |
| 66 | + $waitDuration = $this->bucket->calculateTimeForTokens($remainingTokens); |
| 67 | + |
| 68 | + if (null !== $maxTime && $waitDuration > $maxTime) { |
| 69 | + // process needs to wait longer than set interval |
| 70 | + throw new MaxWaitDurationExceededException(sprintf('The rate limiter wait time ("%d" seconds) is longer than the provided maximum time ("%d" seconds).', $waitDuration, $maxTime)); |
| 71 | + } |
| 72 | + |
| 73 | + // at $now + $waitDuration all tokens will be reserved for this process, |
| 74 | + // so no tokens are left for other processes. |
| 75 | + $this->bucket->setTokens(0); |
| 76 | + $this->bucket->setTimer($now + $waitDuration); |
| 77 | + |
| 78 | + $reservation = new Reservation($tokens, $this->bucket->getTimer()); |
| 79 | + } |
| 80 | + |
| 81 | + $this->storage->save($this->bucket); |
| 82 | + } finally { |
| 83 | + $this->lock->release(); |
| 84 | + } |
| 85 | + |
| 86 | + return $reservation; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Use this method if you intend to drop if the required number |
| 91 | + * of tokens is unavailable. |
| 92 | + * |
| 93 | + * @param int $tokens the number of tokens required |
| 94 | + */ |
| 95 | + public function consume(int $tokens = 1): bool |
| 96 | + { |
| 97 | + try { |
| 98 | + $this->reserve($tokens, 0); |
| 99 | + |
| 100 | + return true; |
| 101 | + } catch (MaxWaitDurationExceededException $e) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments