Exponential Backoff

A small library to retry arbitrary tasks with exponential backoff (with jitter).

Лицензия

Лицензия

Группа

Группа

me.ccampo
Идентификатор

Идентификатор

exponential-backoff
Последняя версия

Последняя версия

1.1.3
Дата

Дата

Тип

Тип

jar
Описание

Описание

Exponential Backoff
A small library to retry arbitrary tasks with exponential backoff (with jitter).
Ссылка на сайт

Ссылка на сайт

https://github.com/ccampo133/exponential-backoff
Система контроля версий

Система контроля версий

https://github.com/ccampo133/exponential-backoff.git

Скачать exponential-backoff

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/me.ccampo/exponential-backoff/ -->
<dependency>
    <groupId>me.ccampo</groupId>
    <artifactId>exponential-backoff</artifactId>
    <version>1.1.3</version>
</dependency>
// https://jarcasting.com/artifacts/me.ccampo/exponential-backoff/
implementation 'me.ccampo:exponential-backoff:1.1.3'
// https://jarcasting.com/artifacts/me.ccampo/exponential-backoff/
implementation ("me.ccampo:exponential-backoff:1.1.3")
'me.ccampo:exponential-backoff:jar:1.1.3'
<dependency org="me.ccampo" name="exponential-backoff" rev="1.1.3">
  <artifact name="exponential-backoff" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.ccampo', module='exponential-backoff', version='1.1.3')
)
libraryDependencies += "me.ccampo" % "exponential-backoff" % "1.1.3"
[me.ccampo/exponential-backoff "1.1.3"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.jetbrains : annotations jar 13.0

test (3)

Идентификатор библиотеки Тип Версия
org.assertj : assertj-core jar 3.4.1
junit : junit jar 4.12
org.mockito : mockito-core jar 1.10.19

Модули Проекта

Данный проект не имеет модулей.

exponential-backoff

Build Status Maven Central

A small Java API for running arbitrary tasks with exponential backoff

Usage

Sometimes you want to run a task and retry if it fails. For things like network and external service calls, retrying immediately may not be the most efficient use of your time. It is unlikely your service is going to be able to respond just a few milliseconds later (it might be down, for example). With exponential backoff, successive attempts are retried after waiting an amount of time, exponentially greater than the previous wait time.

While this is great for increasing client reliability, in the case where you may have multiple concurrent clients, using plain exponential backoff may still result in poor performance (see: https://www.awsarchitectureblog.com/2015/03/backoff.html)

This is where the concept of jitter (randomness) comes in. Plainly, using using jitter with exponential backoff means choosing a random wait versus a plain exponential wait time. The algorithm actually chooses between the random time or the exponential time, with equal probability given to both. While this may seem counter-intuitive, using jitter can greatly increase performance for competing clients (again, see the link above).

This library allows you to run arbitrary tasks which will retry with exponential backoff (and potentially jitter). If an exception is thrown anywhere in your task (and not caught), the task will be retried, waiting some amount of time between the next execution (determined by the backoff algorithm). There are a few examples in the unit tests ExponentialBackOffTest.java, however the general usage is as follows:

final BackOffResult<String> result = ExponentialBackOff.<String>builder()
        .withBase(100)
        .withCap(5000)
        .withMaxAttempts(5)
        .withJitter()
        .withTask(() -> "Do something")
        .retryIf(result -> result.equals("retry"))
        .withExceptionHandler(e -> System.out.println("Do something with " + e))
        .execute();
        
if (result.status == BackOffResult.SUCCESSFUL) {
    System.out.println("All done! Returned " + result.data.orElse("empty"));
} else {
    System.out.println("Failed to complete task within 5 attempts");
}

See the JavaDocs or source code for more specific information on usage. The preferred approach is to use the builder to generate and execute the tasks.

Dependency Information

Found on Maven Central (http://search.maven.org)

Gradle:

compile 'me.ccampo:exponential-backoff:1.1.3'

Maven:

<dependency>
  <groupId>me.ccampo</groupId>
  <artifactId>exponential-backoff</artifactId>
  <version>1.1.3</version>
</dependency>

Development

To build (OS X or -nix)

./gradlew build

or on Windows

gradlew.bat build

Версии библиотеки

Версия
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0