Skip to content

Commit

Permalink
Merge pull request #816 from jeskew/fix/backoff-in-ms-with-jitter
Browse files Browse the repository at this point in the history
Backoff in MS (not seconds) and throw in some jitter
  • Loading branch information
jeskew committed Nov 7, 2015
2 parents c275ac7 + f59372c commit fa6adc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/DynamoDb/DynamoDbClient.php
Expand Up @@ -78,7 +78,7 @@ public static function _applyRetryConfig($value, array &$args, HandlerList $list
RetryMiddleware::createDefaultDecider($value),
function ($retries) {
return $retries
? (50 * (int) pow(2, $retries - 1)) / 1000
? RetryMiddleware::exponentialDelay($retries) / 2
: 0;
}
),
Expand Down
2 changes: 1 addition & 1 deletion src/RetryMiddleware.php
Expand Up @@ -86,7 +86,7 @@ public static function createDefaultDecider($maxRetries = 3)
*/
public static function exponentialDelay($retries)
{
return (int) pow(2, $retries - 1);
return mt_rand(0, (int) pow(2, $retries - 1) * 100);
}

/**
Expand Down
22 changes: 18 additions & 4 deletions tests/RetryMiddlewareTest.php
Expand Up @@ -82,10 +82,24 @@ public function testDeciderDoesNotRetryAfterMaxAttempts()
public function testDelaysExponentially()
{
$this->assertEquals(0, RetryMiddleware::exponentialDelay(0));
$this->assertEquals(1, RetryMiddleware::exponentialDelay(1));
$this->assertEquals(2, RetryMiddleware::exponentialDelay(2));
$this->assertEquals(4, RetryMiddleware::exponentialDelay(3));
$this->assertEquals(8, RetryMiddleware::exponentialDelay(4));
$this->assertLessThanOrEqual(100, RetryMiddleware::exponentialDelay(1));
$this->assertLessThanOrEqual(200, RetryMiddleware::exponentialDelay(2));
$this->assertLessThanOrEqual(400, RetryMiddleware::exponentialDelay(3));
$this->assertLessThanOrEqual(800, RetryMiddleware::exponentialDelay(4));
}

public function testDelaysWithSomeRandomness()
{
$maxDelay = 100 * pow(2, 4);
$values = array_map(function () {
return RetryMiddleware::exponentialDelay(5);
}, range(1, 200));

$this->assertGreaterThan(1, count(array_unique($values)));
foreach ($values as $value) {
$this->assertGreaterThanOrEqual(0, $value);
$this->assertLessThanOrEqual($maxDelay, $value);
}
}

public function testRetriesWhenResultMatches()
Expand Down

0 comments on commit fa6adc0

Please sign in to comment.