Created
September 24, 2014 20:58
-
-
Save jmikola/cc3034eb2c78c863db40 to your computer and use it in GitHub Desktop.
Exponential backoff callback for https://github.com/igorw/retry
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
<?php | |
class ExponentialBackoff | |
{ | |
private $invoked = 0; | |
private $slotTimeUsec; | |
public function __construct($slotTimeUsec) | |
{ | |
$this->slotTimeUsec = (int) $slotTimeUsec; | |
} | |
public function __invoke() | |
{ | |
usleep(rand(0, (2 << (++$this->invoked - 1)) - 1) * $this->slotTimeUsec); | |
} | |
} |
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
<?php | |
function eb($slotTimeUsec) { | |
$invoked = 0; | |
return function() use (&$invoked, $slotTimeUsec) { | |
usleep(rand(0, (2 << (++$invoked - 1)) - 1) * $slotTimeUsec); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment