NoLimiter example

namespace Symfony\Component\RateLimiter\Tests\Policy;

use PHPUnit\Framework\TestCase;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\Reservation;

class NoLimiterTest extends TestCase
{
    public function testConsume()
    {
        $limiter = new NoLimiter();
        $this->assertInstanceOf(RateLimit::class$limiter->consume());
    }

    public function testReserve()
    {
        $limiter = new NoLimiter();
        $this->assertInstanceOf(Reservation::class$limiter->reserve());
    }
}


    public function create(string $key = null): LimiterInterface
    {
        $id = $this->config['id'].'-'.$key;
        $lock = $this->lockFactory?->createLock($id);

        return match ($this->config['policy']) {
            'token_bucket' => new TokenBucketLimiter($id$this->config['limit']$this->config['rate']$this->storage, $lock),
            'fixed_window' => new FixedWindowLimiter($id$this->config['limit']$this->config['interval']$this->storage, $lock),
            'sliding_window' => new SlidingWindowLimiter($id$this->config['limit']$this->config['interval']$this->storage, $lock),
            'no_limit' => new NoLimiter(),
            default => throw new \LogicException(sprintf('Limiter policy "%s" does not exists, it must be either "token_bucket", "sliding_window", "fixed_window" or "no_limit".', $this->config['policy'])),
        };
    }

    protected static function configureOptions(OptionsResolver $options): void
    {
        $intervalNormalizer = static function DOptions $options, string $interval): \DateInterval {
            try {
                return (new \DateTimeImmutable())->diff(new \DateTimeImmutable('+'.$interval));
            } catch (\Exception $e) {
                if (!preg_match('/Failed to parse time string \(\+([^)]+)\)/', $e->getMessage()$m)) {
                    


    public function peek(Request $request): RateLimit
    {
        return $this->doConsume($request, 0);
    }

    private function doConsume(Request $request, int $tokens): RateLimit
    {
        $limiters = $this->getLimiters($request);
        if (0 === \count($limiters)) {
            $limiters = [new NoLimiter()];
        }

        $minimalRateLimit = null;
        foreach ($limiters as $limiter) {
            $rateLimit = $limiter->consume($tokens);

            $minimalRateLimit = $minimalRateLimit ? self::getMinimalRateLimit($minimalRateLimit$rateLimit) : $rateLimit;
        }

        return $minimalRateLimit;
    }

    
Home | Imprint | This part of the site doesn't use cookies.