Coder Social home page Coder Social logo

retryhttp's Introduction

retryhttp Build Status Go Reference

retryhttp allows you to add HTTP retries to your service or application with no refactoring at all, just a few lines of configuration where your client is instantiated. This package's goals are:

How it works

retryhttp exports a Transport struct which implements the standard library's http.RoundTripper interface. By performing retries at the http.RoundTripper level, retries can be introduced to a service or script with just a few lines of configuration and no changes required to any of the code actually making HTTP requests. Regardless of which HTTP client you're using, it's very likely to have a configurable http.RoundTripper, which means this package can be integrated.

http.RoundTrippers are also highly composable. By default, this package uses http.DefaultTransport as its underlying RoundTripper, but you may choose to wrap a customized one that sets MaxIdleConns, or even something like this that captures metric spans to instrument your calls.

Get it

$ go get github.com/justinrixx/retryhttp

Example

// BEFORE
client := http.Client{
    // HTTP client options
}

// AFTER
client := http.Client{
    Transport: retryhttp.New(
        // optional retry configurations
        retryhttp.WithShouldRetryFn(func(attempt retryhttp.Attempt) bool {
            return attempt.Res != nil && attempt.Res.StatusCode == http.StatusServiceUnavailable
        }),
        retryhttp.WithDelayFn(func(attempt retryhttp.Attempt) time.Duration {
            return expBackoff(attempt.Count)
        }),
        retryhttp.WithMaxRetries(2),
    ),
    // other HTTP client options
}

This package was inspired by https://github.com/PuerkitoBio/rehttp/ but it aims to take away a couple footguns, provide widely-applicable defaults, and make one-off overriding options easy using context keys.

License

MIT

retryhttp's People

Contributors

justinrixx avatar

Stargazers

Luca Marques avatar Luca F. Marques avatar Ben Meier avatar Scott Plunkett avatar  avatar Vladislav Tishchenko avatar Daniel Lewan avatar Piotr Icikowski avatar Zach Brogan avatar David Laubreiter avatar Miguel Guerreiro avatar Abdulhafiz KAŞGARLI avatar  avatar Dawid Cygert avatar Pedro Henrique avatar gehrig avatar Clayton Kehoe avatar Bojan Zivanovic avatar Marius Orcsik avatar Josue Kouka avatar weldeon avatar Vipul Rawat avatar Devon DeJohn avatar Zach Callahan avatar  avatar  avatar

Watchers

 avatar  avatar

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.