Skip to content

Commit

Permalink
[HttpClient] Force int conversion for floated multiplier for GenericR…
Browse files Browse the repository at this point in the history
…etryStrategy
  • Loading branch information
francisbesset committed Jun 18, 2023
1 parent 9e89ac4 commit 3d60434
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Retry/GenericRetryStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getDelay(AsyncContext $context, ?string $responseContent, ?Trans
$delay = $this->delayMs * $this->multiplier ** $context->getInfo('retry_count');

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

Expand Down
18 changes: 14 additions & 4 deletions Tests/Retry/GenericRetryStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testGetDelay(int $delay, int $multiplier, int $maxDelay, int $pr

public static function provideDelay(): iterable
{
// delay, multiplier, maxDelay, retries, expectedDelay
// delay, multiplier, maxDelay, previousRetries, expectedDelay
yield [1000, 1, 5000, 0, 1000];
yield [1000, 1, 5000, 1, 1000];
yield [1000, 1, 5000, 2, 1000];
Expand All @@ -90,13 +90,16 @@ public static function provideDelay(): iterable
yield [0, 2, 10000, 1, 0];
}

public function testJitter()
/**
* @dataProvider provideJitter
*/
public function testJitter(float $multiplier, int $previousRetries)
{
$strategy = new GenericRetryStrategy([], 1000, 1, 0, 1);
$strategy = new GenericRetryStrategy([], 1000, $multiplier, 0, 1);
$min = 2000;
$max = 0;
for ($i = 0; $i < 50; ++$i) {
$delay = $strategy->getDelay($this->getContext(0, 'GET', 'http://example.com/', 200), null, null);
$delay = $strategy->getDelay($this->getContext($previousRetries, 'GET', 'http://example.com/', 200), null, null);
$min = min($min, $delay);
$max = max($max, $delay);
}
Expand All @@ -105,6 +108,13 @@ public function testJitter()
$this->assertLessThanOrEqual(1000, $min);
}

public static function provideJitter(): iterable
{
// multiplier, previousRetries
yield [1, 0];
yield [1.1, 2];
}

private function getContext($retryCount, $method, $url, $statusCode): AsyncContext
{
$passthru = null;
Expand Down

0 comments on commit 3d60434

Please sign in to comment.