Skip to content

Commit

Permalink
Use setters instead of having lots of constructor arguments in Expone…
Browse files Browse the repository at this point in the history
…ntialBackoffStrategy (#2)
  • Loading branch information
jakewright authored Mar 14, 2017
1 parent a363f79 commit fbe9361
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
55 changes: 40 additions & 15 deletions src/RetryStrategy/ExponentialBackoffStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,66 @@
class ExponentialBackoffStrategy extends AbstractRetryStrategy implements RetryStrategyInterface
{
/** @var bool */
protected $firstFastRetry;
protected $firstFastRetry = false;

/** @var int */
protected $minBackoff;
protected $minBackoff = 0;

/** @var int */
protected $maxBackoff;

/** @var int */
protected $multiplier;
protected $multiplier = 1000;

/**
* ExponentialBackoffStrategy constructor.
* Whether to retry immediately in the first instance (or after minBackoff if set).
*
* @param int $maxRetries Maximum number of allowed retries before giving up
* @param bool $firstFastRetry Whether to retry immediately in the first instance (or after minBackoff if non-zero)
* @param int $multiplier The upper bound for the backoff period will be multiplied by this. A value of 1000 will
* give backoff periods that increase in the order of seconds, e.g. 1s, 2s, 4s, ....
* @param int $minBackoff The minimum allowed time in ms between retries
* @param int|null $maxBackoff The maximum allowed time in ms between retries
* @param bool $firstFastRetry
*/
public function __construct($maxRetries = 2, $firstFastRetry = false, $multiplier = 1000, $minBackoff = 0, $maxBackoff = null)
public function setFirstFastRetry($firstFastRetry)
{
parent::__construct($maxRetries);
$this->firstFastRetry = $firstFastRetry;
}

if ($maxBackoff && $minBackoff > $maxBackoff) {
/**
* The minimum allowed time in ms between retries.
*
* @param int $minBackoff
*/
public function setMinBackoff($minBackoff)
{
if ($this->maxBackoff && $minBackoff > $this->maxBackoff) {
throw new InvalidArgumentException("Minimum backoff period must be less than or equal to maximum backoff period");
}

$this->firstFastRetry = $firstFastRetry;
$this->multiplier = $multiplier;
$this->minBackoff = $minBackoff;
}

/**
* The maximum allowed time in ms between retries.
*
* @param int $maxBackoff
*/
public function setMaxBackoff($maxBackoff)
{
if ($maxBackoff < $this->minBackoff) {
throw new InvalidArgumentException("Maximum backoff period must be more than or equal to minimum backoff period");
}

$this->maxBackoff = $maxBackoff;
}

/**
* The upper bound for the backoff period will be multiplied by this. A value of 1000 will give backoff periods
* that increase in the order of seconds, i.e. 1s, 2s, 4s, ....
*
* @param int $multiplier
*/
public function setMultiplier($multiplier)
{
$this->multiplier = $multiplier;
}

/**
* @param int $retryCount
* @return int
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/RetryStrategy/ExponentialBackoffStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ class ExponentialBackoffStrategyTest extends TestCase
*/
public function testBackoffPeriodIsAboveMinimum($maxRetries, $firstFastRetry, $multiplier, $minBackoff, $maxBackoff, $retryCount)
{
$strategy = new ExponentialBackoffStrategy($maxRetries, $firstFastRetry, $multiplier, $minBackoff, $maxBackoff);
$strategy = new ExponentialBackoffStrategy($maxRetries);
$strategy->setFirstFastRetry($firstFastRetry);
$strategy->setMultiplier($multiplier);
$strategy->setMinBackoff($minBackoff);
$strategy->setMaxBackoff($maxBackoff);

$this->assertGreaterThanOrEqual($minBackoff, $strategy->calculateBackoffPeriod($retryCount));
}

Expand All @@ -40,7 +45,9 @@ public function testBackoffPeriodIsAboveMinimum($maxRetries, $firstFastRetry, $m
*/
public function testMinBackoffAboveMaxBackoff()
{
new ExponentialBackoffStrategy(1, false, 1000, 5, 4);
$strategy = new ExponentialBackoffStrategy(1);
$strategy->setMinBackoff(5);
$strategy->setMaxBackoff(4);
}

/**
Expand All @@ -52,7 +59,7 @@ public function backoffPeriodDataProvider()
$firstFastRetry = [true, false];
$multiplier = [1000];
$minBackoff = [0, 10];
$maxBackoff = [10, 1000, null];
$maxBackoff = [10, 1000];
$retryCount = [0, 10];

$cartesianProduct = new CartesianProduct();
Expand Down

0 comments on commit fbe9361

Please sign in to comment.