retry

package module
v0.0.0-...-cdaf564 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2018 License: MIT Imports: 10 Imported by: 0

README

retry

retry is a simple http-retry client that support backoff policy such as exponential backoff with max retries...

# retry
go get github.com/golovers/retry

# dependencies
go get github.com/cenkalti/backoff
go get github.com/sirupsen/logrus

Usage

c := retry.New()
req, _ := http.NewRequest(http.MethodGet, "https://www.github.com/pthethanh", nil)

// Using default backoff policy and default retry func
rs, err := c.Do(req)
logrus.Infof("response: %+v, err: %v", rs, err)

// Using custom backoff policy
rs, err = c.DoWithBackOff(req, backoff.NewExponentialBackOff())
logrus.Infof("response: %+v, err: %v", rs, err)

// Using custom backoff policy and custom retry func
rs, err = c.DoWithRetryFunc(req, backoff.NewConstantBackOff(1*time.Second), func(rs *http.Response) bool {
    return rs.StatusCode == http.StatusInternalServerError
})
logrus.Infof("response: %+v, err: %v", rs, err)

Documentation

Index

Constants

View Source
const DefaultMaxRetry uint64 = 10

DefaultMaxRetry is default max retry times

Variables

This section is empty.

Functions

func DefaultRetryFunc

func DefaultRetryFunc(rs *http.Response) bool

DefaultRetryFunc retry when the response status code is in range of 500 but not 501 which is http.StatusNotImplemented

Types

type BackOff

type BackOff = backoff.BackOff

BackOff is a backoff policy for retrying an operation.

func DefaultBackOff

func DefaultBackOff() BackOff

DefaultBackOff return a backoff policy with exponential backoff wrapped with a 10-times-max-retry.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a http retry client

func New

func New() *Client

New return a new default retry client

func NewWithClient

func NewWithClient(c *http.Client) *Client

NewWithClient return a new retry client which use the given http.Client

func (*Client) Do

func (c *Client) Do(r *http.Request) (*http.Response, error)

Do execute the given request with default backoff policy and default retry func

func (*Client) DoWithBackOff

func (c *Client) DoWithBackOff(r *http.Request, b BackOff) (*http.Response, error)

DoWithBackOff execute the given request with the given backoff policy. It uses the DefaultRetryFunc which will retry if response status code is in range of 500 but not http.StatusNotImplemented.

func (*Client) DoWithRetryFunc

func (c *Client) DoWithRetryFunc(r *http.Request, b BackOff, f Func) (*http.Response, error)

DoWithRetryFunc execute the given request with the given backoff policy. A retry is determined by the given retry Func.

func (*Client) WithLogger

func (c *Client) WithLogger(logger Logger) *Client

WithLogger ask the client to usse the given logger instead of default logger (logrus)

type Func

type Func = func(*http.Response) bool

Func is a function to determine if a retry is needed base on the http.Response

type Logger

type Logger interface {
	Errorf(format string, v ...interface{})
	Infof(format string, v ...interface{})
}

Logger is log interface that is used by the retry client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL