RateLimit example



            $now = time();
            $limit = $backoff->getCurrentLimit($now);

            if ($tokens > $limit) {
                throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the size of the rate limiter (%d).', $tokens$limit));
            }

            $attempts = $backoff->getAttempts();
            if ($backoff->shouldThrottle($attempts + $tokens$now)) {
                return new RateLimit($backoff->getAvailableAttempts($now)$backoff->getRetryAfter(), false, $backoff->getCurrentLimit($now));
            }

            $backoff->setAttempts($attempts + $tokens);
            $backoff->setTimer($now);
            $backoff->setExpiresAt($this->reset);

            $this->storage->save($backoff);

            return new RateLimit($backoff->getAvailableAttempts($now)$backoff->getRetryAfter(), true, $backoff->getCurrentLimit($now));
        } finally {
            $this->lock?->release();
        }
use IntegrationTestBehaviour;

    /** * @param array<string, int> $factories */
    private function mockResetLimiter(array $factories): RateLimiter
    {
        $rateLimiter = new RateLimiter();

        foreach ($factories as $factory => $expects) {
            $limiter = $this->createMock(LimiterInterface::class);
            $limiter->method('consume')->willReturn(new RateLimit(1, new \DateTimeImmutable(), true, 1));
            $limiter->expects($this->exactly($expects))->method('reset');

            $limiterFactory = $this->createMock(RateLimiterFactory::class);
            $limiterFactory->method('create')->willReturn($limiter);

            $rateLimiter->registerLimiterFactory($factory$limiterFactory);
        }

        return $rateLimiter;
    }

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