Skip to content

Commit 2d9fb06

Browse files
committed
fix_rate_limit_bc_after_51676_pr
-
1 parent 6fe7205 commit 2d9fb06

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,33 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
5959
$now = microtime(true);
6060
$availableTokens = $window->getAvailableTokens($now);
6161

62-
if ($availableTokens >= max(1, $tokens)) {
62+
if (0 !== $tokens && $availableTokens > $tokens) {
6363
$window->add($tokens, $now);
6464

6565
$reservation = new Reservation($now, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
6666
} else {
67+
if ($availableTokens === $tokens) {
68+
$window->add($tokens, $now);
69+
}
70+
6771
$waitDuration = $window->calculateTimeForTokens(max(1, $tokens), $now);
6872

69-
if (null !== $maxTime && $waitDuration > $maxTime) {
73+
if ($availableTokens !== $tokens && 0 !== $tokens && null !== $maxTime && $waitDuration > $maxTime) {
7074
// process needs to wait longer than set interval
7175
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));
7276
}
7377

74-
$window->add($tokens, $now);
78+
if ($availableTokens !== $tokens) {
79+
$window->add($tokens, $now);
80+
}
81+
82+
if ($availableTokens === $tokens || 0 === $tokens) {
83+
$accepted = true;
84+
} else {
85+
$accepted = false;
86+
}
7587

76-
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
88+
$reservation = new Reservation($now + $waitDuration, new RateLimit($window->getAvailableTokens($now), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->limit));
7789
}
7890

7991
if (0 < $tokens) {

src/Symfony/Component/RateLimiter/Policy/SlidingWindowLimiter.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,33 @@ public function reserve(int $tokens = 1, float $maxTime = null): Reservation
6565
$now = microtime(true);
6666
$hitCount = $window->getHitCount();
6767
$availableTokens = $this->getAvailableTokens($hitCount);
68-
if ((0 !== $tokens || 0 !== $availableTokens) && $availableTokens >= $tokens) {
68+
if (0 !== $tokens && $availableTokens > $tokens) {
6969
$window->add($tokens);
7070

7171
$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
7272
} else {
73+
if ($availableTokens === $tokens) {
74+
$window->add($tokens);
75+
}
76+
7377
$waitDuration = $window->calculateTimeForTokens($this->limit, max(1, $tokens));
7478

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

80-
$window->add($tokens);
84+
if ($availableTokens !== $tokens) {
85+
$window->add($tokens);
86+
}
87+
88+
if ($availableTokens === $tokens || 0 === $tokens) {
89+
$accepted = true;
90+
} else {
91+
$accepted = false;
92+
}
8193

82-
$reservation = new Reservation($now + $waitDuration, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), false, $this->limit));
94+
$reservation = new Reservation($now + $waitDuration, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now + $waitDuration)), $accepted, $this->limit));
8395
}
8496

8597
if (0 < $tokens) {

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

Lines changed: 15 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
*/

src/Symfony/Component/RateLimiter/Tests/Policy/SlidingWindowLimiterTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public function testConsume()
5454
$this->assertSame(10, $rateLimit->getLimit());
5555
}
5656

57+
public function testConsumeLastToken()
58+
{
59+
$limiter = $this->createLimiter();
60+
$limiter->reset();
61+
$limiter->consume(9);
62+
63+
$rateLimit = $limiter->consume(1);
64+
$this->assertSame(0, $rateLimit->getRemainingTokens());
65+
$this->assertTrue($rateLimit->isAccepted());
66+
$this->assertEquals(
67+
\DateTimeImmutable::createFromFormat('U', (string) floor(microtime(true) + 12 / 10)),
68+
$rateLimit->getRetryAfter()
69+
);
70+
}
71+
5772
public function testConsumeZeroTokens()
5873
{
5974
$limiter = $this->createLimiter();
@@ -70,7 +85,7 @@ public function testConsumeZeroTokens()
7085
$limiter->consume(10);
7186

7287
$rateLimit = $limiter->consume(0);
73-
$this->assertFalse($rateLimit->isAccepted());
88+
$this->assertTrue($rateLimit->isAccepted());
7489
$this->assertEquals(
7590
\DateTimeImmutable::createFromFormat('U', (string) floor(microtime(true) + 12 / 10)),
7691
$rateLimit->getRetryAfter()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy