public function consume(int
$tokens = 1
): RateLimit
{ $this->lock?->
acquire(true
);
try { $backoff =
$this->storage->
fetch($this->id
);
if (!
$backoff instanceof TimeBackoff
) { $backoff =
new TimeBackoff($this->id,
$this->limits,
$this->reset
);
} $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);