Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix Throttler::check() $tokens #9067

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions system/Throttle/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Loading