8000 [HttpClient] Force int conversion for floated multiplier for GenericR… · symfony/symfony@962050a · GitHub
[go: up one dir, main page]

Skip to content

Commit 962050a

Browse files
committed
[HttpClient] Force int conversion for floated multiplier for GenericRetryStrategy
1 parent 124311a commit 962050a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Symfony/Component/HttpClient/Retry/GenericRetryStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function getDelay(AsyncContext $context, ?string $responseContent, ?Trans
102102
$delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count');
103103

104104
if ($this->jitter > 0) {
105-
$randomness = $delay * $this->jitter;
105+
$randomness = (int) ($delay * $this->jitter);
106106
$delay = $delay + random_int(-$randomness, +$randomness);
107107
}
108108

src/Symfony/Component/HttpClient/Tests/Retry/GenericRetryStrategyTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testGetDelay(int $delay, int $multiplier, int $maxDelay, int $pr
6767

6868
public static function provideDelay(): iterable
6969
{
70-
// delay, multiplier, maxDelay, retries, expectedDelay
70+
// delay, multiplier, maxDelay, previousRetries, expectedDelay
7171
yield [1000, 1, 5000, 0, 1000];
7272
yield [1000, 1, 5000, 1, 1000];
7373
yield [1000, 1, 5000, 2, 1000];
@@ -90,13 +90,16 @@ public static function provideDelay(): iterable
9090
yield [0, 2, 10000, 1, 0];
9191
}
9292

93-
public function testJitter()
93+
/**
94+
* @dataProvider provideJitter
95+
*/
96+
public function testJitter(float $multiplier, int $previousRetries)
9497
{
95-
$strategy = new GenericRetryStrategy([], 1000, 1, 0, 1);
98+
$strategy = new GenericRetryStrategy([], 1000, $multiplier, 0, 1);
9699
$min = 2000;
97100
$max = 0;
98101
for ($i = 0; $i < 50; ++$i) {
99-
$delay = $strategy->getDelay($this->getContext(0, 'GET', 'http://example.com/', 200), null, null);
102+
$delay = $strategy->getDelay($this->getContext($previousRetries, 'GET', 'http://example.com/', 200), null, null);
100103
$min = min($min, $delay);
101104
$max = max($max, $delay);
102105
}
@@ -105,6 +108,13 @@ public function testJitter()
105108
$this->assertLessThanOrEqual(1000, $min);
106109
}
107110

111+
public static function provideJitter(): iterable
112+
{
113+
// multiplier, previousRetries
114+
yield [1, 0];
115+
yield [1.1, 2];
116+
}
117+
108118
private function getContext($retryCount, $method, $url, $statusCode): AsyncContext
109119
{
110120
$passthru = null;

0 commit comments

Comments
 (0)
0