Skip to content

Commit

Permalink
(update) Bug fix in Throttler class check validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jlamim committed Apr 24, 2020
1 parent 2f1e413 commit e19e0fa
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions system/Throttle/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
$tokenName = $this->prefix . $key;

// Check to see if the bucket has even been created yet.
if (($tokens = $this->cache->get($tokenName)) === null)
{
if (($tokens = $this->cache->get($tokenName)) === null) {
// If it hasn't been created, then we'll set it to the maximum
// capacity - 1, and save it to the cache.
$this->cache->save($tokenName, $capacity - $cost, $seconds);
$this->cache->save($tokenName . 'Time', time(), $seconds);

return true;
}

// If $tokens > 0, then we need to replenish the bucket
Expand All @@ -167,10 +168,9 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
$tokens += $rate * $elapsed;
$tokens = $tokens > $capacity ? $capacity : $tokens;

// If $tokens >= 0, then we are safe to perform the action, but
// If $tokens >= 1, then we are safe to perform the action, but
// we need to decrement the number of available tokens.
if ($tokens >= 0)
{
if ($tokens >= 1) {
$this->cache->save($tokenName, $tokens - $cost, $seconds);
$this->cache->save($tokenName . 'Time', time(), $seconds);

Expand Down Expand Up @@ -207,5 +207,4 @@ public function time(): int
{
return $this->testTime ?? time();
}

}

0 comments on commit e19e0fa

Please sign in to comment.