10000 bug #50698 [HttpClient] Fix int conversion for `GenericRetryStrategy`… · symfony/symfony@d979cfe · GitHub
[go: up one dir, main page]

Skip to content

Commit d979cfe

Browse files
committed
bug #50698 [HttpClient] Fix int conversion for GenericRetryStrategy with floated multiplier (francisbesset)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpClient] Fix int conversion for `GenericRetryStrategy` with floated multiplier | Q | A | ------------- | --- | Branch? | 5.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> When we use floated multiplier, we have a deprecated message from php 8.1: ``` Deprecated: Implicit conversion from float -288.99999999999994 to int loses precision Deprecated: Implicit conversion from float 288.99999999999994 to int loses precision ``` This PR add a int conversion before use [random_int](php.net/random_int)() function. Commits ------- 962050a [HttpClient] Force int conversion for floated multiplier for GenericRetryStrategy
2 parents 98ecd86 + 962050a commit d979cfe

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