From 8e0ab844be5889a8703b23fcd9dc9caea6cddb70 Mon Sep 17 00:00:00 2001 From: Mohammed Karim Date: Mon, 24 Jun 2019 20:36:05 +0100 Subject: [PATCH] Correctly calculate the amount of seconds required to pass before a new token is available. --- system/Throttle/Throttler.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/Throttle/Throttler.php b/system/Throttle/Throttler.php index 8987aeb85d87..f29e42355ef2 100644 --- a/system/Throttle/Throttler.php +++ b/system/Throttle/Throttler.php @@ -152,12 +152,15 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1): // based on how long it's been since the last update. $throttleTime = $this->cache->get($tokenName . 'Time'); $elapsed = $this->time() - $throttleTime; + // Number of tokens to add back per second $rate = $capacity / $seconds; + // How many seconds till a new token is available. // We must have a minimum wait of 1 second for a new token. // Primarily stored to allow devs to report back to users. - $this->tokenTime = max(1, $rate); + $newTokenAvailable = (1/$rate) - $elapsed; + $this->tokenTime = max(1, $newTokenAvailable); // Add tokens based up on number per second that // should be refilled, then checked against capacity