diff --git a/system/Throttle/Throttler.php b/system/Throttle/Throttler.php index 44d12005a80d..c023b5e4d052 100644 --- a/system/Throttle/Throttler.php +++ b/system/Throttle/Throttler.php @@ -102,8 +102,11 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1): // Number of seconds to get one token $refresh = 1 / $rate; + /** @var float|int|null $tokens */ + $tokens = $this->cache->get($tokenName); + // Check to see if the bucket has even been created yet. - if (($tokens = $this->cache->get($tokenName)) === null) { + if ($tokens === null) { // If it hasn't been created, then we'll set it to the maximum // capacity - 1, and save it to the cache. $tokens = $capacity - $cost; @@ -124,7 +127,7 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1): // should be refilled, then checked against capacity // to be sure the bucket didn't overflow. $tokens += $rate * $elapsed; - $tokens = $tokens > $capacity ? $capacity : $tokens; + $tokens = min($tokens, $capacity); // If $tokens >= 1, then we are safe to perform the action, but // we need to decrement the number of available tokens.