Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created September 24, 2014 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmikola/cc3034eb2c78c863db40 to your computer and use it in GitHub Desktop.
Save jmikola/cc3034eb2c78c863db40 to your computer and use it in GitHub Desktop.
Exponential backoff callback for https://github.com/igorw/retry
<?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);
}
}
<?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