retryablehttp

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2022 License: MIT Imports: 3 Imported by: 0

README

retryablehttp

Retryable HTTP Client In Go

Go Go Report Card

Simple HTTP client interface with automatic retries and constant backoff. Inspired by HashiCorp's go-retryablehttp library.

Usage

NewClient() creates and returns a retryable HTTP client instance with provided options.

c, err := retryablehttp.NewClient(
	retryablehttp.WithHTTPClient(http.DefaultClient),
	retryablehttp.WithMaxReqCount(3),
	retryablehttp.WithBackoff(100*time.Millisecond),
	retryablehttp.WithResHandler(func(res *http.Response) error {
		if res == nil {
            return ErrNilRes
        }

        statusCode := res.StatusCode
        if statusCode < 200 || statusCode > 299 {
            return ErrStatusCode(statusCode)
        }

        return nil
	}),
)

WithHTTPClient option configures underlying http client.

WithMaxReqCount option configures maximum request count.

WithBackoff option configures backoff duration which represents sleeping intervals between requests.

WithResponseHandler option configures response handler which handles responses.

Client has Do(*http.Request) (*http.Response, error) function which is identical to *http.Client. This makes our client broadly applicable with minimal effort.

res, err := c.Do(req)

Contribution

Any contribution or feedback is welcome.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilHTTPClient          = errors.New("http client is nil")
	ErrInvalidMaxReqCount     = errors.New("maximum request count is not valid")
	ErrInvalidBackoff         = errors.New("backoff is not valid")
	ErrNilResHandler          = errors.New("response handler is nil")
	ErrNilRes                 = errors.New("response is nil")
	ErrUnsuccessfulStatusCode = errors.New("unsuccessful status code")
)

errors

Functions

This section is empty.

Types

type Client

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

Client represents retryable http client.

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient creates and returns new retryable http client instance

func (*Client) Do

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

Do sends http request with automatic retries returns first successful or last unsuccessful response

type Option

type Option func(c *Client) error

Option configures client options.

func WithBackoff

func WithBackoff(backoff time.Duration) Option

WithBackoff configures client's backoff duration, which represents sleeping intervals between retries. Default backoff is 0.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient configures client's http client. Default http client is http.DefaultClient{}.

func WithMaxReqCount

func WithMaxReqCount(maxReqCount int) Option

WithMaxReqCount configures client's max request count. Default maximum request count is 1.

func WithResHandler

func WithResHandler(resHandler func(res *http.Response) error) Option

WithResHandler configures client's response handler function which handles http response. Default response handler:

func defaultResHandler(res *http.Response) error {
	if res == nil {
		return ErrNilRes
	}

	statusCode := res.StatusCode
	if statusCode < 200 || statusCode > 299 {
		return ErrStatusCode(statusCode)
	}

	return nil
}

Jump to

Keyboard shortcuts

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