Skip to content

Repository files navigation

retry

BuildCoverage

This is a simple and lightweight retry library for Java. It helps you transparently retry failed operations.

Dependency

Maven

<dependency>
<groupId>io.github.pepperkit</groupId>
<artifactId>retry</artifactId>
<version>1.0.0</version>
</dependency>

Gradle

implementation 'io.github.pepperkit:retry:1.0.0'

Backoff Functions

The backoff function determines how much to wait between the retries.

Exponential

It waits progressively longer intervals between subsequent retries.

3s -> 9s -> 27s -> 81s
importio.github.pepperkit.retry.BackoffFunction;
newBackoffFunction.Exponential(3);

Fixed

This is an elementary implementation, just return a constant value.

3s -> 3s -> 3s -> 3s
importio.github.pepperkit.retry.BackoffFunction;
newBackoffFunction.Fixed();

Randomized

This function returns a random interval value. The algorithm doesn't declare how much the rate of an operation will be reduced.

12s -> 4s -> 3s -> 9s
importio.github.pepperkit.retry.BackoffFunction;
newBackoffFunction.Randomized(5);

Custom

You can implement your own version of backoff function by the interface.

@FunctionalInterfacepublicinterfaceBackoffFunction {
Durationdelay(intattempt, Durationdelay);
}

Usage

Methods Definition

  • retry() - initiates the retry function
  • backoff(BackoffFunction function) - a function to compute next delay interval
  • delay(Duration duration) - an initial delay interval
  • maxDelay(Duration duration) - the maximum delay interval value
  • handle(Class<? extends Throwable>) - specifies a type of Exception which has to be handled to retry (if it doesn't specify any exception will be handled by default)
  • abortIf(Class<? extends Throwable>) - when the exception has occurred the retryable function will be interrupted
  • onFailure(Consumer<? super Throwable>) - specifies a function to handle the exception
  • run() - perform an action (function) which can be retried
  • call() - compute a result which can be retried

Exponential Backoff

The exponential backoff algorithm implementation. It uses to gradually reduce the rate of the operation if the exception (or any) occurred.

importstaticio.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(newBackoffFunction.Exponential())
.delay(Duration.ofMillis(500))
.maxDelay(Duration.ofSeconds(3))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// webClient.get() throws HttpServiceUnavailable exception// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred// we have some assumption, this service can be available later...
});

Fixed Backoff

This is a fixed backoff algorithm implementation. There each attempt of the operation will be retried by the same timeout if the exception (or any) occurred.

importstaticio.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(newBackoffFunction.Fixed())
.delay(Duration.ofMillis(500))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred// we have some assumption, this service can be available later...
});

Randomized Backoff

This is a randomized backoff algorithm implementation.

importstaticio.github.pepperkit.retry.Retry.retry;
retry(3)
.backoff(newBackoffFunction.Randomized(5))
.delay(Duration.ofMillis(500))
.maxDelay(Duration.ofSeconds(3))
.handle(HttpServiceUnavailable.class)
.run(()->{
webClient.get("https://some.example.com/v1/resource");
// e.g. try to get this resource 3 times when the HttpServiceUnavailable exception has occurred// we have some assumption, this service can be available later...
});

License

The library is licensed under the terms of the MIT License.

About

This is a free, simple retry library for Java - is pure implementation without any dependency. It helps you transparently retry failed operations.

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages