项目作者: patinthehat

项目描述 :
PHP library implementing various backoff alforithms such as exponential backoff.
高级语言: PHP
项目地址: git://github.com/patinthehat/BackoffV2.git
创建时间: 2017-02-15T09:29:00Z
项目社区:https://github.com/patinthehat/BackoffV2

开源协议:MIT License

下载


BackoffV2 Build Status


BackoffV2 is a PHP 5.5+ library implementing various backoff algorithms, such as exponential backoff.

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.


Installation

Install BackoffV2 with Composer:

composer require patinthehat/backoffv2


Implementation

“Jitter” is implemented, if you choose to use it. Jitter is a small, variable amount of time that is added to the backoff amount.


Available Jitter algorithms (roughly based on this post) include:

  • NoJitter - No jitter
  • FullJitter - Standard jitter amount
  • EqualJitter - More consistent jitter amounts
  • DecorrelatedJitter - Higher jitter amounts

Backoff algorithms include:

  • ExponentialBackoff - exponentially increase the backoff amount
  • ConstantBackoff - use the same backoff amount, regardless of the attempt count.
  • LinearBackoff - linear increase of the backoff amount, i.e. 1, 2, 3, 4, …

Usage


Using the Backoff class

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:

  1. public function __construct($maxBackoff, BackoffStrategyInterface $backoff, JitterStrategyInterface $jitter)

Usage is simple:

  1. include 'vendor/autoload.php';
  2. use BackoffV2\Backoff;
  3. use BackoffV2\Backoff\ExponentialBackoff;
  4. use BackoffV2\Jitter\FullJitter;
  5. $b = new Backoff(15, new ExponentialBackoff, new FullJitter);
  6. echo 'backoff = '.$b->getBackoff() . PHP_EOL;
  7. echo 'backoff = '.$b->getBackoff() . PHP_EOL;
  8. echo 'backoff = '.$b->getBackoff() . PHP_EOL;
  9. echo 'backoff = '.$b->getBackoff() . PHP_EOL;
  10. echo 'backoff = '.$b->getBackoff() . ' (attempt ' . $b->getAttempt().')' . PHP_EOL;
  11. $b->reset();

License

BackoffV2 is available under the MIT License.