13
13
class RateLimiter
14
14
{
15
15
private string $ prefix ;
16
- private int $ maxAmmount ;
17
- private int $ refillTime ;
16
+ private int $ maxCapacity ;
17
+ private int $ refillPeriod ;
18
18
private StorageInterface $ storage ;
19
19
private array $ headers = [
20
- 'X-RateLimit-Limit ' => '{{maxAmmount }} ' ,
20
+ 'X-RateLimit-Limit ' => '{{maxCapacity }} ' ,
21
21
'X-RateLimit-Remaining ' => '{{currentAmmount}} ' ,
22
22
'X-RateLimit-Reset ' => '{{reset}} ' ,
23
23
];
24
24
25
25
public function __construct (array $ options , StorageInterface $ storage )
26
26
{
27
27
$ this ->prefix = $ options ['prefix ' ];
28
- $ this ->maxAmmount = $ options ['maxAmmount ' ];
29
- $ this ->refillTime = $ options ['refillTime ' ];
28
+ $ this ->maxCapacity = $ options ['maxCapacity ' ];
29
+ $ this ->refillPeriod = $ options ['refillPeriod ' ];
30
30
$ this ->headers = $ options ['headers ' ] ?? $ this ->headers ;
31
31
$ this ->storage = $ storage ;
32
32
}
@@ -41,27 +41,27 @@ public function check(string $identifier): bool
41
41
42
42
$ currentTime = time ();
43
43
$ lastCheck = $ this ->storage ->get ($ key . 'last_check ' );
44
- $ tokensToAdd = ($ currentTime - $ lastCheck ) * ($ this ->maxAmmount / $ this ->refillTime );
44
+ $ tokensToAdd = ($ currentTime - $ lastCheck ) * ($ this ->maxCapacity / $ this ->refillPeriod );
45
45
$ currentAmmount = $ this ->storage ->get ($ key );
46
46
// optimization of adding a token every rate ÷ per seconds
47
47
$ bucket = $ currentAmmount + $ tokensToAdd ;
48
48
// 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 ;
50
50
// set last check time
51
- $ this ->storage ->set ($ key . 'last_check ' , $ currentTime , $ this ->refillTime );
51
+ $ this ->storage ->set ($ key . 'last_check ' , $ currentTime , $ this ->refillPeriod );
52
52
53
53
if ($ bucket < 1 ) {
54
54
return false ;
55
55
}
56
56
57
- $ this ->storage ->set ($ key , $ bucket - 1 , $ this ->refillTime );
57
+ $ this ->storage ->set ($ key , $ bucket - 1 , $ this ->refillPeriod );
58
58
return true ;
59
59
}
60
60
61
61
private function createBucket (string $ key )
62
62
{
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 );
65
65
}
66
66
67
67
private function hasBucket (string $ key ): bool
@@ -87,9 +87,9 @@ public function headers(string $identifier): array
87
87
$ lastCheck = $ this ->storage ->get ($ key . 'last_check ' );
88
88
$ headers = [];
89
89
foreach ($ this ->headers as $ key => $ value ) {
90
- $ headers [$ key ] = str_replace ('{{maxAmmount }} ' , $ this ->maxAmmount , $ value );
90
+ $ headers [$ key ] = str_replace ('{{maxCapacity }} ' , $ this ->maxCapacity , $ value );
91
91
$ 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 ]);
93
93
}
94
94
95
95
return $ headers ;
0 commit comments