2017 © Pedro Peláez
 

library exponential-backoff

Prevent overloading an unavailable service by doubling the timeout each iteration.

image

crowdstar/exponential-backoff

Prevent overloading an unavailable service by doubling the timeout each iteration.

  • Friday, July 27, 2018
  • by deminy
  • Repository
  • 5 Watchers
  • 0 Stars
  • 95 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Library Status Latest Stable Version Latest Unstable Version License, (*1)

Summary

Exponential back-offs prevent overloading an unavailable service by doubling the timeout each iteration. This class uses an exponential back-off algorithm to calculate the timeout for the next request., (*2)

This library allows doing exponential backoff in non-blocking mode in Swoole., (*3)

Installation

composer require crowdstar/exponential-backoff:~3.0.0

Sample Usage

In following code pieces, we assume that you want to store return value of method MyClass::fetchData() in variable $result, and you want to do exponential backoff on that because something unexpected could happen when running method MyClass::fetchData()., (*4)

1. Retry When Return Value Is Empty

Following code is to try to fetch some non-empty data back with method MyClass::fetchData(). This piece of code will try a few more times (by default 4) until either we get some non-empty data back, or we have reached maximum numbers of retries., (*5)

run(
    function () {
        return MyClass::fetchData();
    }
);
?>

2. Retry When Certain Exceptions Thrown Out

Following code is to try to fetch some data back with method MyClass::fetchData(), which may throw out exceptions. This piece of code will try a few more times (by default 4) until either we get some data back, or we have reached maximum numbers of retries., (*6)

NOTE: Internal PHP errors (class Error) won't trigger exponential backoff. They should be fixed manually., (*7)

run(
        function () {
            return MyClass::fetchData();
        }
    );
} catch (Throwable $t) {
    // Handle the errors here.
}
?>

Don't Throw Out an Exception When Finally Failed

When method call MyClass::fetchData() finally fails with an exception caught, we can silence the exception without throwing it out by overriding method AbstractRetryCondition::throwable():, (*8)

run(
    function () {
        return MyClass::fetchData();
    }
);
?>

If needed, you can have more complex logic defined when overriding method AbstractRetryCondition::throwable()., (*9)

3. Retry When Customized Condition Met

Following code is to try to fetch some non-empty data back with method MyClass::fetchData(). This piece of code works the same as the first example, except that here it's implemented with a customized condition class instead of class \CrowdStar\Backoff\EmptyValueCondition., (*10)

run(
    function () {
        return MyClass::fetchData();
    }
);
?>

4. More Options When Doing Exponential Backoff

Following code is to try to fetch some data back with method MyClass::fetchData(). This piece of code works the same as the second example, except that here it's implemented with a customized condition class instead of class \CrowdStar\Backoff\ExceptionBasedCondition., (*11)

In this piece of code, we also show what options are available when doing exponential backoff with the package., (*12)

setType(ExponentialBackoff::TYPE_SECONDS)
    ->setType(ExponentialBackoff::TYPE_MICROSECONDS)
    ->setMaxAttempts(3)
    ->setMaxAttempts(4);

$result = $backoff->run(
    function () {
        return MyClass::fetchData();
    }
);
?>

5. To Disable Exponential Backoff Temporarily

There are two ways to disable exponential backoff temporarily for code piece like following:, (*13)

<?php
$result = MyClass::fetchData();
?>

First, you may disable exponential backoff temporarily by calling method \CrowdStar\Backoff\ExponentialBackoff::disable(). For example:, (*14)

disable();
$result = $backoff->run(function () {return MyClass::fetchData();});
?>

You may also disable exponential backoff temporarily by using class \CrowdStar\Backoff\NullCondition:, (*15)

setRetryCondition(new NullCondition()) // The method here is for demonstration purpose.
    ->run(function () {return MyClass::fetchData();});
?>

All these 3 code piece work the same, having return value of method call MyClass::fetchData() assigned to variable $result., (*16)

Sample Scripts

Sample scripts can be found under folder examples/. Before running them under CLI, please do a composer update first:, (*17)

composer update -n

The Versions

27/07 2018

dev-master

9999999-dev https://www.glu.com

Prevent overloading an unavailable service by doubling the timeout each iteration.

  Sources   Download

proprietary

The Requires

  • php >=7.1

 

The Development Requires

27/07 2018

3.0.2

3.0.2.0 https://www.glu.com

Prevent overloading an unavailable service by doubling the timeout each iteration.

  Sources   Download

proprietary

The Requires

  • php >=7.1

 

The Development Requires

11/06 2018

3.0.1

3.0.1.0 https://www.glu.com

Prevent overloading an unavailable service by doubling the timeout each iteration.

  Sources   Download

proprietary

The Requires

  • php >=7.1

 

The Development Requires

11/06 2018

3.0.0

3.0.0.0 https://www.glu.com

Prevent overloading an unavailable service by doubling the timeout each iteration.

  Sources   Download

proprietary

The Requires

  • php >=7.1

 

The Development Requires