2017 © Pedro Peláez
 

library php-backoff

Simple backoff / retry functionality

image

yriveiro/php-backoff

Simple backoff / retry functionality

  • Friday, July 13, 2018
  • by yriveiro
  • Repository
  • 2 Watchers
  • 15 Stars
  • 12,018 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 38 % Grown

The README.md

Backoff, Simple backoff / retry functionality

License Build Status Coverage Status Total Downloads SensioLabsInsight, (*1)

NOTE: to use php-backoff with PHP 5.x please use the lastet release of branch 1.x, (*2)

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., (*3)

exponential($attempt):

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

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., (*5)

To solve this we can add a jitter value to allow some randomization., (*6)

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

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., (*8)

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

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., (*10)

$attempt = 1;
$backoff = new Backoff();

$response = $http->get('http://myservice.com/user/1');

while (!$response) {
    $time = $backoff->exponential($attempt);
    $attempt++;

    usleep($time);

    $response = $http->get('http://myservice.com/user/1');
}

With configuration examples:

$attempt = 1;
$options = Backoff::getDefaultOptions();
$options['maxAttempts'] = 3;

$backoff = new Backoff($options);

$response = $http->get('http://myservice.com/user/1');

try
    while (!$response) {
        $time = $backoff->fullJitter($attempt);
        $attempt++;

        usleep($time);

        $response = $http->get('http://myservice.com/user/1');
    }
} catch (Yriveiro\Backoff\BackoffException $e) {
    // Handle the exception
}

Installation

The recommended way to install this package is through Composer., (*11)

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

Tests

Tests are performed using the phpunit library, to run them:, (*12)

php vendor/bin/phpunit tests

Know issues

None., (*13)

How to contribute

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

License

Backoff is licensed under MIT license., (*15)

The Versions

13/07 2018

dev-master

9999999-dev

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

26/04 2018

2.1.0

2.1.0.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

13/01 2018

2.0.2

2.0.2.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

06/07 2017

1.x-dev

1.9999999.9999999.9999999-dev

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

  • php >=5.3.3 <7.0

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

05/07 2017

1.1.0

1.1.0.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

  • php >=5.3.3 <7.0

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

04/07 2017

2.0.1

2.0.1.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

04/07 2017

2.0.0

2.0.0.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

22/05 2017

1.0.2

1.0.2.0

Simple backoff / retry functionality

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

17/02 2017

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

17/02 2017

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff

31/01 2016

0.0.1

0.0.1.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yago Riveiro

retry backoff jitter exponential backoff