All Projects → stechstudio → backoff

stechstudio / backoff

Licence: MIT license
PHP library providing retry functionality with multiple backoff strategies and jitter support

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to backoff

php-backoff
Simple back off / retry functionality
Stars: ✭ 24 (-81.82%)
Mutual labels:  jitter, retry, backoff
waiter
Delayed iteration for polling and retries.
Stars: ✭ 17 (-87.12%)
Mutual labels:  retry, backoff
Resty
Simple HTTP and REST client library for Go
Stars: ✭ 5,368 (+3966.67%)
Mutual labels:  retry, backoff
awsretry
Decorate your AWS Boto3 Calls with AWSRetry.backoff(). This will allows your calls to get around the AWS Eventual Consistency Errors.
Stars: ✭ 42 (-68.18%)
Mutual labels:  retry, backoff
Backoff
Python library providing function decorators for configurable backoff and retry
Stars: ✭ 1,670 (+1165.15%)
Mutual labels:  retry, backoff
Mug
A small Java 8 util library, complementary to Guava (BiStream, Substring, MoreStreams, Parallelizer).
Stars: ✭ 236 (+78.79%)
Mutual labels:  retry
jest-retry
Jest retry pattern for flaky E2E tests
Stars: ✭ 36 (-72.73%)
Mutual labels:  retry
Retry4j
Lightweight Java library for retrying unreliable logic
Stars: ✭ 179 (+35.61%)
Mutual labels:  retry
Resilient.js
Fault tolerant and reactive HTTP client for node.js and browsers
Stars: ✭ 172 (+30.3%)
Mutual labels:  retry
retryx
Promise-based retry workflow library.
Stars: ✭ 21 (-84.09%)
Mutual labels:  retry
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (-80.3%)
Mutual labels:  retry
View-Load-ReTry
这个加载框架有点不一样,针对View进行加载,加载页面还保持了原View的属性,侧重点在灵活,哪里需要加载哪里,加载状态页面完全自定义,无任何限制,针对加载结果可以按需配置对应页面,LeakCanary检测无内存泄漏
Stars: ✭ 116 (-12.12%)
Mutual labels:  retry
Toxy
Hackable HTTP proxy for resiliency testing and simulated network conditions
Stars: ✭ 2,698 (+1943.94%)
Mutual labels:  retry
Afnetworking Retrypolicy
Nice category that adds the ability to set the retry interval, retry count and progressiveness.
Stars: ✭ 197 (+49.24%)
Mutual labels:  retry
retrygroup
Package retrygroup provides synchronization, Context cancelation for groups of retry goroutines working on subtasks of a common task.
Stars: ✭ 18 (-86.36%)
Mutual labels:  retry
Loadmorewrapper
📦 make recyclerView supports load more and customize the footer view, without changes to the original adater of recyclerView. 在不改动 RecyclerView 原有的 adapter 的情况下,使 RecyclerView 滑动到底部的时候能够加载更多和自定义底部视图。
Stars: ✭ 179 (+35.61%)
Mutual labels:  retry
retries
Forget about your retry boilerplate
Stars: ✭ 14 (-89.39%)
Mutual labels:  retry
DepthKit-for-Max
A patch for using DepthKit volumetric videos in Max/MSP/Jitter
Stars: ✭ 21 (-84.09%)
Mutual labels:  jitter
esa-httpclient
An asynchronous event-driven HTTP client based on netty.
Stars: ✭ 82 (-37.88%)
Mutual labels:  retry
Robust-Deep-Learning-Pipeline
Deep Convolutional Bidirectional LSTM for Complex Activity Recognition with Missing Data. Human Activity Recognition Challenge. Springer SIST (2020)
Stars: ✭ 20 (-84.85%)
Mutual labels:  jitter

PHP Backoff

Latest Version on Packagist Build Software License Quality Score Total Downloads

Easily wrap your code with retry functionality. This library provides:

  1. 4 backoff strategies (plus the ability to use your own)
  2. Optional jitter / randomness to spread out retries and minimize collisions
  3. Wait time cap
  4. Callbacks for custom retry logic or error handling

Installation

composer require stechstudio/backoff

Defaults

This library provides sane defaults so you can hopefully just jump in for most of your use cases.

By default the backoff is quadratic with a 100ms base time (attempt^2 * 100), a max of 5 retries, and no jitter.

Quickstart

The simplest way to use Backoff is with the global backoff helper function:

$result = backoff(function() {
    return doSomeWorkThatMightFail();
});

If successful $result will contain the result of the closure. If max attempts are exceeded the inner exception is re-thrown.

You can of course provide other options via the helper method if needed.

Method parameters are $callback, $maxAttempts, $strategy, $waitCap, $useJitter.

Backoff class usage

The Backoff class constructor parameters are $maxAttempts, $strategy, $waitCap, $useJitter.

$backoff = new Backoff(10, 'exponential', 10000, true);
$result = $backoff->run(function() {
    return doSomeWorkThatMightFail();
});

Or if you are injecting the Backoff class with a dependency container, you can set it up with setters after the fact. Note that setters are chainable.

// Assuming a fresh instance of $backoff was handed to you
$result = $backoff
    ->setStrategy('constant')
    ->setMaxAttempts(10)
    ->enableJitter()
    ->run(function() {
        return doSomeWorkThatMightFail();
    });

Changing defaults

If you find you want different defaults, you can modify them via static class properties:

Backoff::$defaultMaxAttempts = 10;
Backoff::$defaultStrategy = 'exponential';
Backoff::$defaultJitterEnabled = true;

You might want to do this somewhere in your application bootstrap for example. These defaults will be used anytime you create an instance of the Backoff class or use the backoff() helper function.

Strategies

There are four built-in strategies available: constant, linear, polynomial, and exponential.

The default base time for all strategies is 100 milliseconds.

Constant

$strategy = new ConstantStrategy(500);

This strategy will sleep for 500 milliseconds on each retry loop.

Linear

$strategy = new LinearStrategy(200);

This strategy will sleep for attempt * baseTime, providing linear backoff starting at 200 milliseconds.

Polynomial

$strategy = new PolynomialStrategy(100, 3);

This strategy will sleep for (attempt^degree) * baseTime, so in this case (attempt^3) * 100.

The default degree if none provided is 2, effectively quadratic time.

Exponential

$strategy = new ExponentialStrategy(100);

This strategy will sleep for (2^attempt) * baseTime.

Specifying strategy

In our earlier code examples we specified the strategy as a string:

backoff(function() {
    ...
}, 10, 'constant');

// OR

$backoff = new Backoff(10, 'constant');

This would use the ConstantStrategy with defaults, effectively giving you a 100 millisecond sleep time.

You can create the strategy instance yourself in order to modify these defaults:

backoff(function() {
    ...
}, 10, new LinearStrategy(500));

// OR

$backoff = new Backoff(10, new LinearStrategy(500));

You can also pass in an integer as the strategy, will translates to a ConstantStrategy with the integer as the base time in milliseconds:

backoff(function() {
    ...
}, 10, 1000);

// OR

$backoff = new Backoff(10, 1000);

Finally, you can pass in a closure as the strategy if you wish. This closure should receive an integer attempt and return a sleep time in milliseconds.

backoff(function() {
    ...
}, 10, function($attempt) {
    return (100 * $attempt) + 5000;
});

// OR

$backoff = new Backoff(10);
$backoff->setStrategy(function($attempt) {
    return (100 * $attempt) + 5000;
});

Wait cap

You may want to use a fast growing backoff time (like exponential) but then also set a max wait time so that it levels out after a while.

This cap can be provided as the fourth argument to the backoff helper function, or using the setWaitCap() method on the Backoff class.

Jitter

If you have a lot of clients starting a job at the same time and encountering failures, any of the above backoff strategies could mean the workers continue to collide at each retry.

The solution for this is to add randomness. See here for a good explanation:

https://www.awsarchitectureblog.com/2015/03/backoff.html

You can enable jitter by passing true in as the fifth argument to the backoff helper function, or by using the enableJitter() method on the Backoff class.

We use the "FullJitter" approach outlined in the above article, where a random number between 0 and the sleep time provided by your selected strategy is used.

Custom retry decider

By default Backoff will retry if an exception is encountered, and if it has not yet hit max retries.

You may provide your own retry decider for more advanced use cases. Perhaps you want to retry based on time rather than number of retries, or perhaps there are scenarios where you would want retry even when an exception was not encountered.

Provide the decider as a callback, or an instance of a class with an __invoke method. Backoff will hand it four parameters: the current attempt, max attempts, the last result received, and the exception if one was encountered. Your decider needs to return true or false.

$backoff->setDecider(function($attempt, $maxAttempts, $result, $exception = null) {
    return someCustomLogic();
});

Error handler callback

You can provide a custom error handler to be notified anytime an exception occurs, even if we have yet to reach max attempts. This is a useful place to do logging for example.

$backoff->setErrorHandler(function($exception, $attempt, $maxAttempts) {
    Log::error("On run $attempt we hit a problem: " . $exception->getMessage());
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].