Skip to content

Commit f7932b3

Browse files
Merge branch '7.0' into 7.1
* 7.0: [RateLimit] Allow to get RateLimit without consuming again Reintroduce peek consume test for sliding window policy [Validator] updated missing Polish translation fix typo
2 parents 366baef + 0e775ae commit f7932b3

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,18 @@ 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) {
69+
$resetDuration = $window->calculateTimeForTokens($this->limit, $window->getHitCount());
70+
$resetTime = \DateTimeImmutable::createFromFormat('U', $availableTokens ? floor($now) : floor($now + $resetDuration));
71+
72+
return new Reservation($now, new RateLimit($availableTokens, $resetTime, true, $this->limit));
73+
}
6874
if ($availableTokens >= $tokens) {
6975
$window->add($tokens);
7076

7177
$reservation = new Reservation($now, new RateLimit($this->getAvailableTokens($window->getHitCount()), \DateTimeImmutable::createFromFormat('U', floor($now)), true, $this->limit));
7278
} else {
73-
$waitDuration = $window->calculateTimeForTokens($this->limit, max(1, $tokens));
79+
$waitDuration = $window->calculateTimeForTokens($this->limit, $tokens);
7480

7581
if (null !== $maxTime && $waitDuration > $maxTime) {
7682
// process needs to wait longer than set interval

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function testConsume()
5252
$rateLimit = $limiter->consume(10);
5353
$this->assertTrue($rateLimit->isAccepted());
5454
$this->assertSame(10, $rateLimit->getLimit());
55+
$this->assertSame(0, $rateLimit->getRemainingTokens());
5556
}
5657

5758
public function testWaitIntervalOnConsumeOverLimit()
@@ -76,6 +77,37 @@ public function testReserve()
7677

7778
// 2 over the limit, causing the WaitDuration to become 2/10th of the 12s interval
7879
$this->assertEqualsWithDelta(12 / 5, $limiter->reserve(4)->getWaitDuration(), 1);
80+
81+
$limiter->reset();
82+
$this->assertEquals(0, $limiter->reserve(10)->getWaitDuration());
83+
}
84+
85+
public function testPeekConsume()
86+
{
87+
$limiter = $this->createLimiter();
88+
89+
$limiter->consume(9);
90+
91+
// peek by consuming 0 tokens twice (making sure peeking doesn't claim a token)
92+
for ($i = 0; $i < 2; ++$i) {
93+
$rateLimit = $limiter->consume(0);
94+
$this->assertTrue($rateLimit->isAccepted());
95+
$this->assertSame(10, $rateLimit->getLimit());
96+
$this->assertEquals(
97+
\DateTimeImmutable::createFromFormat('U', (string) floor(microtime(true))),
98+
$rateLimit->getRetryAfter()
99+
);
100+
}
101+
102+
$limiter->consume();
103+
104+
$rateLimit = $limiter->consume(0);
105+
$this->assertEquals(0, $rateLimit->getRemainingTokens());
106+
$this->assertTrue($rateLimit->isAccepted());
107+
$this->assertEquals(
108+
\DateTimeImmutable::createFromFormat('U', (string) floor(microtime(true) + 12)),
109+
$rateLimit->getRetryAfter()
110+
);
79111
}
80112

81113
private function createLimiter(): SlidingWindowLimiter

src/Symfony/Component/Validator/Resources/translations/validators.de.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@
432432
</trans-unit>
433433
<trans-unit id="111">
434434
<source>The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.</source>
435-
<target>Der erkannte Zeichensatz ist nicht gültig ({{ detected }}). Gültige Zeichensätze sind "{{ encodings }}".</target>
435+
<target>Der erkannte Zeichensatz ist nicht gültig ({{ detected }}). Gültige Zeichensätze sind {{ encodings }}.</target>
436436
</trans-unit>
437437
</body>
438438
</file>

src/Symfony/Component/Validator/Resources/translations/validators.pl.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@
430430
<source>The extension of the file is invalid ({{ extension }}). Allowed extensions are {{ extensions }}.</source>
431431
<target>Rozszerzenie pliku jest nieprawidłowe ({{ extension }}). Dozwolone rozszerzenia to {{ extensions }}.</target>
432432
</trans-unit>
433+
<trans-unit id="111">
434+
<source>The detected character encoding is invalid ({{ detected }}). Allowed encodings are {{ encodings }}.</source>
435+
<target>Wykryte kodowanie znaków ({{ detected }}) jest nieprawidłowe. Dozwolone kodowania to {{ encodings }}.</target>
436+
</trans-unit>
433437
</body>
434438
</file>
435439
</xliff>

0 commit comments

Comments
 (0)