Download the PHP package crowdstar/exponential-backoff without Composer
On this page you can find all versions of the php package crowdstar/exponential-backoff. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download crowdstar/exponential-backoff
More information about crowdstar/exponential-backoff
Files in crowdstar/exponential-backoff
Package exponential-backoff
Short Description Prevent overloading an unavailable service by doubling the timeout each iteration.
License Apache-2.0
Homepage https://www.glu.com
Informations about the package exponential-backoff
- Summary
- Installation
- Sample Usage
- 1. Retry When Return Value Is Empty
- 2. Retry When Certain Exceptions Thrown Out
- Don't Throw Out an Exception When Finally Failed
- 3. Retry When Customized Condition Met
- 4. More Options When Doing Exponential Backoff
- 5. To Disable Exponential Backoff Temporarily
- Sample Scripts
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.
This library allows doing exponential backoff in non-blocking mode in Swoole.
Installation
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().
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.
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.
NOTE: Internal PHP errors (class Error) won't trigger exponential backoff. They should be fixed manually.
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():
If needed, you can have more complex logic defined when overriding method AbstractRetryCondition::throwable().
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.
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.
In this piece of code, we also show what options are available when doing exponential backoff with the package.
5. To Disable Exponential Backoff Temporarily
There are two ways to disable exponential backoff temporarily for code piece like following:
First, you may disable exponential backoff temporarily by calling method \CrowdStar\Backoff\ExponentialBackoff::disable(). For example:
You may also disable exponential backoff temporarily by using class \CrowdStar\Backoff\NullCondition:
All these 3 code piece work the same, having return value of method call MyClass::fetchData() assigned to variable $result.
Sample Scripts
Sample scripts can be found under folder examples/. Before running them under CLI, please do a composer update first: