dateIntervalToSeconds example

/** * @param list<TimeBackoffLimit> $limits */
    public function __construct(
        string $id,
        private readonly array $limits,
        \DateInterval $reset,
        StorageInterface $storage,
        LockInterface|null $lock = new NoLock()
    ) {
        $this->id = $id;
        $this->reset = TimeUtil::dateIntervalToSeconds($reset);
        $this->storage = $storage;
        $this->lock = $lock;
    }

    public function reserve(int $tokens = 1, ?float $maxTime = null): Reservation
    {
        throw new ReserveNotSupportedException(self::class);
    }

    public function consume(int $tokens = 1): RateLimit
    {
        
public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, LockInterface $lock = null)
    {
        if ($limit < 1) {
            throw new \InvalidArgumentException(sprintf('Cannot set the limit of "%s" to 0, as that would never accept any hit.', __CLASS__));
        }

        $this->storage = $storage;
        $this->lock = $lock;
        $this->id = $id;
        $this->limit = $limit;
        $this->interval = TimeUtil::dateIntervalToSeconds($interval);
    }

    public function reserve(int $tokens = 1, float $maxTime = null): Reservation
    {
        if ($tokens > $this->limit) {
            throw new \InvalidArgumentException(sprintf('Cannot reserve more tokens (%d) than the size of the rate limiter (%d).', $tokens$this->limit));
        }

        $this->lock?->acquire(true);

        try {
            


    public static function exceptionData(): \Generator
    {
        yield 'consume 4 and expect 3' => [4, 3];
        yield 'consume 2, then consoume 2 and expect 1' => [2, 1, 2];
        yield 'consume 3, then consume 2 and expect 1' => [2, 1, 3];
    }

    private function intervalToSeconds(string $interval): int
    {
        return TimeUtil::dateIntervalToSeconds((new \DateTimeImmutable())->diff(new \DateTimeImmutable('+' . $interval)));
    }
}
use ResetLimiterTrait;

    private int $limit;
    private int $interval;

    public function __construct(string $id, int $limit, \DateInterval $interval, StorageInterface $storage, LockInterface $lock = null)
    {
        $this->storage = $storage;
        $this->lock = $lock;
        $this->id = $id;
        $this->limit = $limit;
        $this->interval = TimeUtil::dateIntervalToSeconds($interval);
    }

    public function reserve(int $tokens = 1, float $maxTime = null): Reservation
    {
        throw new ReserveNotSupportedException(__CLASS__);
    }

    public function consume(int $tokens = 1): RateLimit
    {
        $this->lock?->acquire(true);

        
/** * @dataProvider provideConsumeOutsideInterval */
    public function testConsumeOutsideInterval(string $dateIntervalString)
    {
        $limiter = $this->createLimiter($dateIntervalString);

        // start window...         $limiter->consume();
        // ...add a max burst, 5 seconds before the end of the window...         sleep(TimeUtil::dateIntervalToSeconds(new \DateInterval($dateIntervalString)) - 5);
        $limiter->consume(9);
        // ...try bursting again at the start of the next window, 10 seconds later         sleep(10);
        $rateLimit = $limiter->consume(10);
        $this->assertEquals(0, $rateLimit->getRemainingTokens());
        $this->assertTrue($rateLimit->isAccepted());
    }

    public function testWaitIntervalOnConsumeOverLimit()
    {
        $limiter = $this->createLimiter();

        
return new static(new \DateInterval($interval)$rate);
    }

    /** * Calculates the time needed to free up the provided number of tokens in seconds. */
    public function calculateTimeForTokens(int $tokens): int
    {
        $cyclesRequired = ceil($tokens / $this->refillAmount);

        return TimeUtil::dateIntervalToSeconds($this->refillTime) * $cyclesRequired;
    }

    /** * Calculates the next moment of token availability. */
    public function calculateNextTokenAvailability(): \DateTimeImmutable
    {
        return (new \DateTimeImmutable())->add($this->refillTime);
    }

    /** * Calculates the number of new free tokens during $duration. * * @param float $duration interval in seconds */
if ($count > $current['limit'] && $next && $count <= $next['limit']) {
                return $current;
            }
        }

        return null;
    }

    private function intervalToSeconds(string $interval): int
    {
        return TimeUtil::dateIntervalToSeconds(\DateInterval::createFromDateString($interval));
    }
}
Home | Imprint | This part of the site doesn't use cookies.