Skip to content

Commit

Permalink
Update RateLimiter.php
Browse files Browse the repository at this point in the history
  • Loading branch information
atakde committed Dec 7, 2022
1 parent 0df30b5 commit d5260f8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
class RateLimiter
{
private string $prefix;
private int $maxAmmount;
private int $refillTime;
private int $maxCapacity;
private int $refillPeriod;
private StorageInterface $storage;
private array $headers = [
'X-RateLimit-Limit' => '{{maxAmmount}}',
'X-RateLimit-Limit' => '{{maxCapacity}}',
'X-RateLimit-Remaining' => '{{currentAmmount}}',
'X-RateLimit-Reset' => '{{reset}}',
];

public function __construct(array $options, StorageInterface $storage)
{
$this->prefix = $options['prefix'];
$this->maxAmmount = $options['maxAmmount'];
$this->refillTime = $options['refillTime'];
$this->maxCapacity = $options['maxCapacity'];
$this->refillPeriod = $options['refillPeriod'];
$this->headers = $options['headers'] ?? $this->headers;
$this->storage = $storage;
}
Expand All @@ -41,27 +41,27 @@ public function check(string $identifier): bool

$currentTime = time();
$lastCheck = $this->storage->get($key . 'last_check');
$tokensToAdd = ($currentTime - $lastCheck) * ($this->maxAmmount / $this->refillTime);
$tokensToAdd = ($currentTime - $lastCheck) * ($this->maxCapacity / $this->refillPeriod);
$currentAmmount = $this->storage->get($key);
// optimization of adding a token every rate ÷ per seconds
$bucket = $currentAmmount + $tokensToAdd;
// if is greater than max ammount, set it to max ammount
$bucket = $bucket > $this->maxAmmount ? $this->maxAmmount : $bucket;
$bucket = $bucket > $this->maxCapacity ? $this->maxCapacity : $bucket;
// set last check time
$this->storage->set($key . 'last_check', $currentTime, $this->refillTime);
$this->storage->set($key . 'last_check', $currentTime, $this->refillPeriod);

if ($bucket < 1) {
return false;
}

$this->storage->set($key, $bucket - 1, $this->refillTime);
$this->storage->set($key, $bucket - 1, $this->refillPeriod);
return true;
}

private function createBucket(string $key)
{
$this->storage->set($key . 'last_check', time(), $this->refillTime);
$this->storage->set($key, $this->maxAmmount - 1, $this->refillTime);
$this->storage->set($key . 'last_check', time(), $this->refillPeriod);
$this->storage->set($key, $this->maxCapacity - 1, $this->refillPeriod);
}

private function hasBucket(string $key): bool
Expand All @@ -87,9 +87,9 @@ public function headers(string $identifier): array
$lastCheck = $this->storage->get($key . 'last_check');
$headers = [];
foreach ($this->headers as $key => $value) {
$headers[$key] = str_replace('{{maxAmmount}}', $this->maxAmmount, $value);
$headers[$key] = str_replace('{{maxCapacity}}', $this->maxCapacity, $value);
$headers[$key] = str_replace('{{currentAmmount}}', $this->get($identifier), $headers[$key]);
$headers[$key] = str_replace('{{reset}}', $lastCheck + $this->refillTime, $headers[$key]);
$headers[$key] = str_replace('{{reset}}', $lastCheck + $this->refillPeriod, $headers[$key]);
}

return $headers;
Expand Down

0 comments on commit d5260f8

Please sign in to comment.