Skip to content

Commit 502be80

Browse files
committed
refactor: fix Throttler::check() $tokens
1 parent 1192b91 commit 502be80

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

system/Throttle/Throttler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
102102
// Number of seconds to get one token
103103
$refresh = 1 / $rate;
104104

105+
/** @var float|int|null $tokens */
106+
$tokens = $this->cache->get($tokenName);
107+
105108
// Check to see if the bucket has even been created yet.
106-
if (($tokens = $this->cache->get($tokenName)) === null) {
109+
if ($tokens === null) {
107110
// If it hasn't been created, then we'll set it to the maximum
108111
// capacity - 1, and save it to the cache.
109112
$tokens = $capacity - $cost;
@@ -124,7 +127,7 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
124127
// should be refilled, then checked against capacity
125128
// to be sure the bucket didn't overflow.
126129
$tokens += $rate * $elapsed;
127-
$tokens = $tokens > $capacity ? $capacity : $tokens;
130+
$tokens = min($tokens, $capacity);
128131

129132
// If $tokens >= 1, then we are safe to perform the action, but
130133
// we need to decrement the number of available tokens.

0 commit comments

Comments
 (0)