Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 666b407

Browse filesBrowse files
committed
[RateLimiter] Fix results on last token consume.
1 parent 0719891 commit 666b407
Copy full SHA for 666b407

File tree

4 files changed

+89
-9
lines changed
Filter options

4 files changed

+89
-9
lines changed

‎src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/FixedWindowLimiter.php
+18-4Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,35 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
6363

6464
$now = microtime(true);
6565
$availableTokens = $window->getAvailableTokens($now);
66-
if ($availableTokens >= $tokens) {
66+
if (0 !== $tokens && $availableTokens > $tokens) {
6767
$window->add($tokens, $now);
6868

6969
$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
7070
} else {
71+
if ($availableTokens === $tokens) {
72+
$window->add($tokens, $now);
73+
}
74+
7175
$waitDuration = $window->calculateTimeForTokens($tokens, $now);
7276

73-
if (null !== $maxTime && $waitDuration > $maxTime) {
77+
if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
7478
// process needs to wait longer than set interval
7579
throw new MaxWaitDurationExceededException(sprintf('The rate limiter wait time ("%d" seconds) is longer than the provided maximum time ("%d" seconds).', $waitDuration, $maxTime), new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
7680
}
7781

78-
$window->add($tokens, $now);
82+
if ($availableTokens !== $tokens) {
83+
$window->add($tokens, $now);
84+
}
85+
86+
if ($availableTokens === $tokens || 0 === $tokens) {
87+
$accepted = true;
88+
$timeToAct = $now;
89+
} else {
90+
$accepted = false;
91+
$timeToAct = $now + $waitDuration;
92+
}
7993

80-
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
94+
$reservation = new Reservation($timeToAct, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->limit));
8195
}
8296
$this->storage->save($window);
8397
} finally {

‎src/Symfony/Component/RateLimiter/Policy/TokenBucketLimiter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Policy/TokenBucketLimiter.php
+19-5Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,22 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
6767

6868
$now = microtime(true);
6969
$availableTokens = $bucket->getAvailableTokens($now);
70-
if ($availableTokens >= $tokens) {
70+
if (0 !== $tokens && $availableTokens > $tokens) {
7171
// tokens are now available, update bucket
7272
$bucket->setTokens($availableTokens - $tokens);
7373

7474
$reservation = new Reservation($now, new RateLimit($bucket->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->maxBurst));
7575
} else {
76-
$remainingTokens = $tokens - $availableTokens;
77-
$waitDuration = $this->rate->calculateTimeForTokens($remainingTokens);
76+
if ($availableTokens === $tokens) {
77+
$waitDuration = $this->rate->calculateTimeForTokens($tokens);
78+
} elseif (0 === $tokens) {
79+
$waitDuration = 0;
80+
} else {
81+
$remainingTokens = $tokens - $availableTokens;
82+
$waitDuration = $this->rate->calculateTimeForTokens($remainingTokens);
83+
}
7884

79-
if (null !== $maxTime && $waitDuration > $maxTime) {
85+
if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
8086
// process needs to wait longer than set interval
8187
$rateLimit = new RateLimit($availableTokens, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->maxBurst);
8288

@@ -87,7 +93,15 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
8793
// so no tokens are left for other processes.
8894
$bucket->setTokens($availableTokens - $tokens);
8995

90-
$reservation = new Reservation($now + $waitDuration, new RateLimit(0, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->maxBurst));
96+
if ($availableTokens === $tokens || 0 === $tokens) {
97+
$accepted = true;
98+
$timeToAct = $now;
99+
} else {
100+
$accepted = false;
101+
$timeToAct = $now + $waitDuration;
102+
}
103+
104+
$reservation = new Reservation($timeToAct, new RateLimit(0, \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->maxBurst));
91105
}
92106

93107
$this->storage->save($bucket);

‎src/Symfony/Component/RateLimiter/Tests/Policy/FixedWindowLimiterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Tests/Policy/FixedWindowLimiterTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public function testConsume()
5757
$this->assertEquals($retryAfter, $rateLimit->getRetryAfter());
5858
}
5959

60+
public function testConsumeLastToken()
61+
{
62+
$now = time();
63+
$limiter = $this->createLimiter();
64+
$limiter->consume(9);
65+
66+
$rateLimit = $limiter->consume(1);
67+
$this->assertSame(0, $rateLimit->getRemainingTokens());
68+
$this->assertTrue($rateLimit->isAccepted());
69+
$this->assertEquals(
70+
\DateTimeImmutable::createFromFormat('U', $now + 60),
71+
$rateLimit->getRetryAfter()
72+
);
73+
}
74+
6075
/**
6176
* @dataProvider provideConsumeOutsideInterval
6277
*/
@@ -76,6 +91,15 @@ public function testConsumeOutsideInterval(string $dateIntervalString)
7691
$this->assertTrue($rateLimit->isAccepted());
7792
}
7893

94+
public function testReserve()
95+
{
96+
$limiter = $this->createLimiter('PT1S');
97+
98+
$this->assertEquals(0, $limiter->reserve(5)->getWaitDuration());
99+
$this->assertEquals(0, $limiter->reserve(5)->getWaitDuration());
100+
$this->assertEquals(1, $limiter->reserve(5)->getWaitDuration());
101+
}
102+
79103
public function testWaitIntervalOnConsumeOverLimit()
80104
{
81105
$limiter = $this->createLimiter();

‎src/Symfony/Component/RateLimiter/Tests/Policy/TokenBucketLimiterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/Tests/Policy/TokenBucketLimiterTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ public function testConsume()
9191
$this->assertSame(10, $rateLimit->getLimit());
9292
}
9393

94+
public function testConsumeLastToken()
95+
{
96+
$rate = Rate::perSecond(1);
97+
$limiter = $this->createLimiter(10, $rate);
98+
99+
$rateLimit = $limiter->consume(10);
100+
$this->assertSame(0, $rateLimit->getRemainingTokens());
101+
$this->assertTrue($rateLimit->isAccepted());
102+
$this->assertEqualsWithDelta(time(), $rateLimit->getRetryAfter()->getTimestamp(), 10);
103+
}
104+
105+
public function testConsumeZeroTokens()
106+
{
107+
$rate = Rate::perSecond(1);
108+
$limiter = $this->createLimiter(10, $rate);
109+
110+
$rateLimit = $limiter->consume(0);
111+
$this->assertTrue($rateLimit->isAccepted());
112+
$this->assertEquals(time(), $rateLimit->getRetryAfter()->getTimestamp());
113+
114+
$limiter->reset();
115+
$limiter->consume(10);
116+
117+
$rateLimit = $limiter->consume(0);
118+
$this->assertTrue($rateLimit->isAccepted());
119+
$this->assertEquals(time(), $rateLimit->getRetryAfter()->getTimestamp());
120+
}
121+
94122
public function testWaitIntervalOnConsumeOverLimit()
95123
{
96124
$limiter = $this->createLimiter();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.