retryrequest

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

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

Go to latest
Published: Feb 19, 2020 License: MIT Imports: 4 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Retry Request (Go)

Simple package to retry http.Client requests when:

  • Status code is invalid/missing
  • Status code is 500 range (except 501)
  • Connection timeout reached

This package respects the request context.Context. Requests will not retry if context has been canceled or DeadlineExceeded is reached.

This package mostly is a drop-in replacement to help with the common net.Error.Timeout() when calling TCP, API endpoints.

Usage:

Normal requests are performed the following way:

resp, err := client.Do(req)

To use this library simply call retryrequest.Do instead:

resp, err := retryrequest.Do(client, req, nil)

The third argument is the optional retry policy:

var DefaultPolicy = &Policy{
	Retry500Status:     true,
	RetryInvalidStatus: true,
	Attempts:           2,
	Delay:              time.Millisecond * 500,
}

Recommendation: use github.com/hashicorp/go-retryablehttp for more types of retry scenarios.

hashicorp/go-retryablehttp is too heavy of a wrapper for me (though it provides more features). This is a lightweight wrapper that is intended to be used to repeat the same request X times for endpoints that are often overloaded. hashicorp/go-retryablehttp also provides it's own internal pool of github.com/hashicorp/go-cleanhttp clients by default.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPolicy = &Policy{
	Retry500Status:     true,
	RetryInvalidStatus: true,
	Attempts:           2,
	Delay:              time.Millisecond * 500,
}

DefaultPolicy used when a policy is not provided

Functions

func Do

func Do(client *http.Client, req *http.Request, policy *Policy) (*http.Response, error)

Do HTTP request, retrying if server times out or sends a 500 error

Types

type Policy

type Policy struct {
	Retry500Status     bool
	RetryInvalidStatus bool
	Attempts           int
	Delay              time.Duration
}

Policy for retrying requests

Jump to

Keyboard shortcuts

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