Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Rate limiter implementation(gcra) #204

Merged
merged 36 commits into from
Jan 17, 2020
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6b86901
Merge pull request #1 from yiisoft/master
romkatsu Sep 22, 2019
bde0a00
Merge pull request #2 from yiisoft/master
romkatsu Oct 20, 2019
532d758
Merge pull request #3 from yiisoft/master
romkatsu Oct 29, 2019
312ca3a
Merge pull request #4 from yiisoft/master
romkatsu Dec 22, 2019
7245614
Merge pull request #5 from yiisoft/master
romkatsu Jan 5, 2020
b535aae
add rate limiter clases
romkatsu Jan 9, 2020
f74ae48
fix use block
romkatsu Jan 9, 2020
c137b78
fix quota limit
romkatsu Jan 9, 2020
b33ac0f
remove counter interface and quota
romkatsu Jan 10, 2020
ac2d299
add tests
romkatsu Jan 10, 2020
0177222
fix doc block
romkatsu Jan 10, 2020
f21365c
add counter class tests
romkatsu Jan 10, 2020
535c684
fix name methods
romkatsu Jan 10, 2020
f84c84e
fix name methods
romkatsu Jan 10, 2020
2792915
fixed doc block
romkatsu Jan 11, 2020
f19ec40
corrected the calculations in milliseconds
romkatsu Jan 11, 2020
1379280
fix float value
romkatsu Jan 11, 2020
124c27d
meve check to constructor
romkatsu Jan 11, 2020
26a4d61
add cache prefix and fix setters
romkatsu Jan 11, 2020
1e92598
add RateLimitResult and edit middleware
romkatsu Jan 12, 2020
7f8b720
Add phpdoc to RateLimitResult, adjust naming
samdark Jan 13, 2020
487770d
Adjust RateLimiterMiddleware description
samdark Jan 13, 2020
e0b9d7e
add interface counter and fix method
romkatsu Jan 13, 2020
6aacd61
fix tests
romkatsu Jan 13, 2020
2b707e4
Better naming
samdark Jan 13, 2020
5d3f5a4
Refactoring of Counter
samdark Jan 13, 2020
3a775e8
Remove useless condition
samdark Jan 13, 2020
16350a6
Added phpdoc to Counter for better understanding or algorithm, adjust…
samdark Jan 13, 2020
b3fb5c8
Adjust naming in tests, remove duplicate test
samdark Jan 13, 2020
e7fa4bb
Do rounding with native PHP function
samdark Jan 14, 2020
77fce0b
Add Counter::getCacheKey()
samdark Jan 14, 2020
c8b480c
add tests and edit reset time, fix lastIncrementTime in Counter
romkatsu Jan 15, 2020
108be0e
edit reset method add timestamp returned
romkatsu Jan 15, 2020
ac0c8de
Rename CounterStatistics -> CounterState
samdark Jan 16, 2020
95e9272
Add cache TTL
samdark Jan 16, 2020
00b035a
simplified testing middleware
romkatsu Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix quota limit
  • Loading branch information
romkatsu committed Jan 9, 2020
commit c137b782d4ebebfee9178d2a61f8438f2466188a
9 changes: 7 additions & 2 deletions src/RateLimiter/GenericCellRateAlgorithm.php
Original file line number Diff line number Diff line change
@@ -35,12 +35,12 @@ public function getActualTheoreticalArrivalTime(): ?float

private function getEmissionInterval(): float
{
return (float)($this->quota->getPeriod() / $this->quota->getLimit());
return (float)($this->quota->getPeriod() / $this->getLimit());
}

private function getDelayVariationTolerance(float $emissionInterval)
{
return $emissionInterval * $this->quota->getLimit();
return $emissionInterval * $this->getLimit();
}

private function calculateTheoreticalArrivalTime(float $theoreticalArrivalTime, float $emissionInterval): float
@@ -54,4 +54,9 @@ private function calculateRemaining(float $theoreticalArrivalTime, float $emissi

return floor((($this->arrivalTime - $allowAt) / $emissionInterval) + 0.5);
}

private function getLimit(): int
{
return $this->quota->getLimit() + 1;
}
}
6 changes: 3 additions & 3 deletions tests/RateLimiter/RateLimiterTest.php
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public function moreThanDefaultNumberOfRequestsIsNotAllowed(): void
{
$middleware = $this->createRateLimiter($this->getCounter(1000));

for ($i = 0; $i < 999; $i++) {
for ($i = 0; $i < 1000; $i++) {
$middleware->process($this->createRequest(), $this->createRequestHandler());
}

@@ -48,9 +48,9 @@ public function moreThanDefaultNumberOfRequestsIsNotAllowed(): void
*/
public function customLimitWorksAsExpected(): void
{
$middleware = $this->createRateLimiter($this->getCounter(10));
$middleware = $this->createRateLimiter($this->getCounter(11));

for ($i = 0; $i < 8; $i++) {
for ($i = 0; $i < 10; $i++) {
$middleware->process($this->createRequest(), $this->createRequestHandler());
}