8000 [RateLimiter] Resolve crash on near-round timestamps · symfony/symfony@4965952 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4965952

Browse files
committed
[RateLimiter] Resolve crash on near-round timestamps
1 parent 33d4c43 commit 4965952

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ public function getHitCount(): int
122122

123123
public function getRetryAfter(): \DateTimeImmutable
124124
{
125-
return \DateTimeImmutable::createFromFormat('U.u', $this->windowEndAt);
125+
return \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', $this->windowEndAt));
126126
}
127127
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,13 @@ public function testGetRetryAfterUsesMicrotime()
102102
// should be 500ms left (10 - 9.5)
103103
$this->assertEqualsWithDelta(0.5, $window->getRetryAfter()->format('U.u') - microtime(true), 0.2);
104104
}
105+
106+
public function testCreateAtExactTime()
107+
{
108+
ClockMock::register(SlidingWindow::class);
109+
ClockMock::withClockMock(1234567890.000000);
110+
$window = new SlidingWindow('foo', 10);
111+
$window->getRetryAfter();
112+
$this->assertEquals('1234567900.000000', $window->getRetryAfter()->format('U.u'));
113+
}
105114
}

0 commit comments

Comments
 (0)
0