forked from igorw/retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ExponentialBackoffErrorHandler. Added PowersOfTwoSequence and accompanying test.
- Loading branch information
Showing
5 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace ScriptFUSION\Retry\ErrorHandler; | ||
|
||
use ScriptFUSION\Retry\Sequence\PowersOfTwoSequence; | ||
|
||
/** | ||
* Sleeps for an exponentially increasing series of delays. | ||
*/ | ||
class ExponentialBackoffErrorHandler extends MicroSleepErrorHandler | ||
{ | ||
private $microTimeCoefficient; | ||
|
||
public function __construct($microTimeCoefficient = 102000) | ||
{ | ||
parent::__construct($this->generateSequence( | ||
$this->microTimeCoefficient = $microTimeCoefficient|0 | ||
)); | ||
} | ||
|
||
private function generateSequence($coefficient) | ||
{ | ||
foreach (new PowersOfTwoSequence as $base) { | ||
yield $base * $coefficient; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
namespace ScriptFUSION\Retry\ErrorHandler; | ||
|
||
/** | ||
* Sleeps for a series of microsecond delays for each invocation. | ||
*/ | ||
class MicroSleepErrorHandler | ||
{ | ||
private $delays; | ||
|
||
public function __construct(\Iterator $delays) | ||
{ | ||
$this->delays = $delays; | ||
} | ||
|
||
public function __invoke() | ||
{ | ||
$this->delays->next(); | ||
|
||
usleep($this->delays->current()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace ScriptFUSION\Retry\Sequence; | ||
|
||
final class PowersOfTwoSequence implements \IteratorAggregate | ||
{ | ||
private $power = 0; | ||
|
||
public function getIterator() | ||
{ | ||
yield 1; | ||
|
||
while (0 < $value = 2 << $this->power++) { | ||
yield $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
namespace ScriptFUSIONTest\Retry\Sequence; | ||
|
||
use ScriptFUSION\Retry\Sequence\PowersOfTwoSequence; | ||
|
||
final class PowersOfTwoSequenceTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testSequence() | ||
{ | ||
$powers = new \LimitIterator((new PowersOfTwoSequence)->getIterator(), 0, 6); | ||
|
||
self::assertSame([1, 2, 4, 8, 16, 32], iterator_to_array($powers)); | ||
} | ||
|
||
public function testIsFiniteSequence() | ||
{ | ||
$counter = 0; | ||
foreach (new \LimitIterator((new PowersOfTwoSequence)->getIterator(), 0, $limit = 100) as $_) { | ||
++$counter; | ||
} | ||
|
||
self::assertLessThan($limit, $counter); | ||
} | ||
} |