Skip to content

Commit 8e0ab84

Browse files
author
Mohammed Karim
committed
Correctly calculate the amount of seconds required to pass before a new token is available.
1 parent 94ad967 commit 8e0ab84

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

system/Throttle/Throttler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,15 @@ public function check(string $key, int $capacity, int $seconds, int $cost = 1):
152152
// based on how long it's been since the last update.
153153
$throttleTime = $this->cache->get($tokenName . 'Time');
154154
$elapsed = $this->time() - $throttleTime;
155+
155156
// Number of tokens to add back per second
156157
$rate = $capacity / $seconds;
157158

159+
// How many seconds till a new token is available.
158160
// We must have a minimum wait of 1 second for a new token.
159161
// Primarily stored to allow devs to report back to users.
160-
$this->tokenTime = max(1, $rate);
162+
$newTokenAvailable = (1/$rate) - $elapsed;
163+
$this->tokenTime = max(1, $newTokenAvailable);
161164

162165
// Add tokens based up on number per second that
163166
// should be refilled, then checked against capacity

0 commit comments

Comments
 (0)