Merge pull request #2873 from jlamim/throttler_bug

Bug fix in Throttler class check validation
This commit is contained in:
Michal Sniatala 2020-07-11 11:58:31 +02:00 committed by GitHub
commit 246ab82658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,8 +139,7 @@ class Throttler implements ThrottlerInterface
$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);
@ -169,10 +168,9 @@ class Throttler implements ThrottlerInterface
$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);
@ -209,5 +207,4 @@ class Throttler implements ThrottlerInterface
{
return $this->testTime ?? time();
}
}