Wallogit.com
2017 © Pedro Peláez
Backoff algorithms implemented in PHP
BackoffV2 is a PHP 5.5+ library implementing various backoff algorithms, such as exponential backoff., (*1)
This library only returns a backoff delay amount based on the selected algorithms; implementation of the actual delay mechanism (such as sleep()) is left to the user., (*2)
Install BackoffV2 with Composer:, (*3)
composer require patinthehat/backoffv2, (*4)
"Jitter" is implemented, if you choose to use it. Jitter is a small, variable amount of time that is added to the backoff amount., (*5)
Available Jitter algorithms (roughly based on this post) include:, (*6)
Backoff algorithms include:, (*7)
BackoffV2 implements a main class, Backoff, that acts as a container and manager for the backoff and jitter algorithms you choose.
The constructor signature for Backoff is:, (*8)
public function __construct($maxBackoff, BackoffStrategyInterface $backoff, JitterStrategyInterface $jitter)
Usage is simple:, (*9)
include 'vendor/autoload.php'; use BackoffV2\Backoff; use BackoffV2\Backoff\ExponentialBackoff; use BackoffV2\Jitter\FullJitter; $b = new Backoff(15, new ExponentialBackoff, new FullJitter); echo 'backoff = '.$b->getBackoff() . PHP_EOL; echo 'backoff = '.$b->getBackoff() . PHP_EOL; echo 'backoff = '.$b->getBackoff() . PHP_EOL; echo 'backoff = '.$b->getBackoff() . PHP_EOL; echo 'backoff = '.$b->getBackoff() . ' (attempt ' . $b->getAttempt().')' . PHP_EOL; $b->reset();
BackoffV2 is available under the MIT License., (*10)