Git Product home page Git Product logo

retry's Introduction

retry

Build Coverage

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
import io.github.pepperkit.retry.BackoffFunction;

new BackoffFunction.Exponential(3);

Fixed

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

3s -> 3s -> 3s -> 3s
import io.github.pepperkit.retry.BackoffFunction;

new BackoffFunction.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
import io.github.pepperkit.retry.BackoffFunction;

new BackoffFunction.Randomized(5);

Custom

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

@FunctionalInterface
public interface BackoffFunction {

    Duration delay(int attempt, Duration delay);
}

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.

import static io.github.pepperkit.retry.Retry.retry;

    retry(3)
        .backoff(new BackoffFunction.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.

import static io.github.pepperkit.retry.Retry.retry;

    retry(3)
        .backoff(new BackoffFunction.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.

import static io.github.pepperkit.retry.Retry.retry;

    retry(3)
        .backoff(new BackoffFunction.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.

retry's People

Contributors

aaukhatov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

retry's Issues

No retries on Error

handle(Class) accepts Throwable, however run(RunnableRetry) catches only Exception. So the following statement will not retry:

retry(3)
                .delay(Duration.ofMillis(500))
                .handle(AssertionError.class)
                .run(() -> {
                    get("/eventually/consistent/resources")
                            .then()
                            .statusCode(200);
                });

Catching Error is not recommended in most cases, but in this case it will help to make the framework useful in unit tests.

Add license to source code files

/*
 * Copyright (C) 2021 PepperKit
 *
 * This software may be modified and distributed under the terms
 * of the MIT license. See the LICENSE file for details.
 */

Support Jitter

Jitter is the deviation from true periodicity of a presumably periodic signal.
Adding jitter provides a way to break the synchronization across the clients thereby avoiding collisions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.