项目作者: yriveiro

项目描述 :
Simple back off / retry functionality
高级语言: PHP
项目地址: git://github.com/yriveiro/php-backoff.git
创建时间: 2015-12-13T23:46:54Z
项目社区:https://github.com/yriveiro/php-backoff

开源协议:MIT License

下载


Backoff, Simple backoff / retry functionality

License Build Status Coverage Status Total Downloads SensioLabsInsight

NOTE: to use php-backoff with PHP 5.x please use the lastet release of branch 1.x

API

getDefaultOptions():

This method is static and returns an array with the default options:

  • cap: Max duration allowed (in microseconds). If backoff duration is greater than cap, cap is returned, default is 1000000 microseconds.
  • maxAttempts: Number of attempts before thrown an Yriveiro\Backoff\BackoffException. Default is 0, no limit.

exponential($attempt):

This method use and exponential function E(attempt) = (2**attempt - 1) to calculate backoff time.

Parameters

  • attempt: incremental value that represents the current retry number.

equalJitter($attempt);

Exponential backoff has one disadvantage. In high concurrence, we can have multiples calls with the same backoff time due the time is highly bound to the current attempt, different calls could be in the same attempt.

To solve this we can add a jitter value to allow some randomization.

equalJitter uses the function: E(attempt) = min(((2**attempt - 1) / 2), random(0, ((2**attempt - 1) / 2))).

Parameters

  • attempt: incremental value that represents the current retry number.

fullJitter($attempt);

Full jitter behaves like equalJitter method, the main difference between them is the way in how the jitter value is calculated.

fullJitter uses the function: E(attempt) = min(random(0, (2**attempt - 1) / 2)).

Parameters

  • attempt: incremental value that represents the current retry number.

Usage

Zero configuration examples:

With zero configuration, we will never stop to try fetch data. The exit condition is your responsibility.

  1. $attempt = 1;
  2. $backoff = new Backoff();
  3. $response = $http->get('http://myservice.com/user/1');
  4. while (!$response) {
  5. $time = $backoff->exponential($attempt);
  6. $attempt++;
  7. usleep($time);
  8. $response = $http->get('http://myservice.com/user/1');
  9. }

With configuration examples:

  1. $attempt = 1;
  2. $options = Backoff::getDefaultOptions();
  3. $options['maxAttempts'] = 3;
  4. $backoff = new Backoff($options);
  5. $response = $http->get('http://myservice.com/user/1');
  6. try
  7. while (!$response) {
  8. $time = $backoff->fullJitter($attempt);
  9. $attempt++;
  10. usleep($time);
  11. $response = $http->get('http://myservice.com/user/1');
  12. }
  13. } catch (Yriveiro\Backoff\BackoffException $e) {
  14. // Handle the exception
  15. }

Installation

The recommended way to install this package is through Composer.

  1. php composer.phar require "yriveiro/php-backoff"

Tests

Tests are performed using the phpunit library, to run them:

  1. php vendor/bin/phpunit tests

Know issues

None.

How to contribute

Have an idea? Found a bug?, contributions are welcome :)

License

Backoff is licensed under MIT license.