Skip to content

Commit d5260f8

Browse files
committed
Update RateLimiter.php
1 parent 0df30b5 commit d5260f8

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/RateLimiter.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
class RateLimiter
1414
{
1515
private string $prefix;
16-
private int $maxAmmount;
17-
private int $refillTime;
16+
private int $maxCapacity;
17+
private int $refillPeriod;
1818
private StorageInterface $storage;
1919
private array $headers = [
20-
'X-RateLimit-Limit' => '{{maxAmmount}}',
20+
'X-RateLimit-Limit' => '{{maxCapacity}}',
2121
'X-RateLimit-Remaining' => '{{currentAmmount}}',
2222
'X-RateLimit-Reset' => '{{reset}}',
2323
];
2424

2525
public function __construct(array $options, StorageInterface $storage)
2626
{
2727
$this->prefix = $options['prefix'];
28-
$this->maxAmmount = $options['maxAmmount'];
29-
$this->refillTime = $options['refillTime'];
28+
$this->maxCapacity = $options['maxCapacity'];
29+
$this->refillPeriod = $options['refillPeriod'];
3030
$this->headers = $options['headers'] ?? $this->headers;
3131
$this->storage = $storage;
3232
}
@@ -41,27 +41,27 @@ public function check(string $identifier): bool
4141

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

5353
if ($bucket < 1) {
5454
return false;
5555
}
5656

57-
$this->storage->set($key, $bucket - 1, $this->refillTime);
57+
$this->storage->set($key, $bucket - 1, $this->refillPeriod);
5858
return true;
5959
}
6060

6161
private function createBucket(string $key)
6262
{
63-
$this->storage->set($key . 'last_check', time(), $this->refillTime);
64-
$this->storage->set($key, $this->maxAmmount - 1, $this->refillTime);
63+
$this->storage->set($key . 'last_check', time(), $this->refillPeriod);
64+
$this->storage->set($key, $this->maxCapacity - 1, $this->refillPeriod);
6565
}
6666

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

9595
return $headers;

0 commit comments

Comments
 (0)