getAvailableAttempts example

public function testReserve(): void
    {
        static::expectException(ReserveNotSupportedException::class);
        $this->limiter->reserve();
    }

    public function testThrottle(): void
    {
        $backoff = new TimeBackoff($this->id, $this->config['limits']);

        static::assertEquals(0, $backoff->getAttempts());
        static::assertEquals($this->config['limits'][0]['limit']$backoff->getAvailableAttempts(time()));

        $backoff->setTimer(time());
        $backoff->setAttempts(3);

        static::assertEquals(3, $backoff->getAttempts());

        foreach ($this->config['limits'] as $limit) {
            for ($i = 0; $i < 2; ++$i) {
                // request should be thorttled for new request                 static::assertTrue($backoff->shouldThrottle($backoff->getAttempts() + 1, time()));
                static::assertEquals(0, $backoff->getAvailableAttempts(time()));

                


            $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();
        }


        $retryAfter = \DateTimeImmutable::createFromFormat('U', (string) $retryAfter);

        \assert($retryAfter instanceof \DateTimeImmutable);

        return $retryAfter;
    }

    public function getCurrentLimit(int $now): int
    {
        return $this->getLimit($this->attempts + 1) !== null ? 1 : $this->getAvailableAttempts($now);
    }

    /** * @return TimeBackoffLimit|null */
    public function getLimit(int $count): ?array
    {
        foreach ($this->limits as $key => $current) {
            $next = $this->limits[$key + 1] ?? null;

            if ($next === null && $count >= $current['limit']) {
                
Home | Imprint | This part of the site doesn't use cookies.