Skip to content

Commit

Permalink
Correctly calculate the amount of seconds required to pass before a n…
Browse files Browse the repository at this point in the history
…ew token is available.
  • Loading branch information
Mohammed Karim committed Jun 24, 2019
1 parent 94ad967 commit 8e0ab84
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion system/Throttle/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8e0ab84

Please sign in to comment.