From e19e0fa498ff92ece690f11c1d9d0b595c857996 Mon Sep 17 00:00:00 2001 From: Jonathan Lamim Date: Fri, 24 Apr 2020 10:29:08 -0300 Subject: [PATCH] (update) Bug fix in Throttler class check validation --- system/Throttle/Throttler.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/system/Throttle/Throttler.php b/system/Throttle/Throttler.php index 9c8314aab1f7..51d496f78f65 100644 --- a/system/Throttle/Throttler.php +++ b/system/Throttle/Throttler.php @@ -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 @@ -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); @@ -207,5 +207,4 @@ public function time(): int { return $this->testTime ?? time(); } - }