retry

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxRetries      = 5
	DefaultInitialDuration = 2 * time.Minute
)

Variables

View Source
var ErrorNoMoreAttempts = errors.New("request cannot by retried")

Functions

func DefaultBackoff

func DefaultBackoff(d time.Duration) time.Duration

DefaultBackoff will double the duration d if it is greater than zero, otherwise it returns 1 minute.

func NewRoundTripper

func NewRoundTripper(inner http.RoundTripper, o ...Option) http.RoundTripper

Types

type BackoffFn

type BackoffFn func(time.Duration) time.Duration

A BackoffFn takes the previous delay and calculates a new, longer delay.

This is used to extend the time between retries.

type Option

type Option interface {
	Apply(*Options)
}

func Backoff

func Backoff(bo BackoffFn) Option

func InitialDelay

func InitialDelay(d time.Duration) Option

func MaxRetries

func MaxRetries(max int) Option

func RetryAfter

func RetryAfter(ra RetryAfterFn) Option

func Strategy

func Strategy(rs RetryStrategyFn) Option

type Options

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

func MakeOptions

func MakeOptions(os ...Option) *Options

type Request

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

func NewRequest

func NewRequest(r *http.Request, client func(*http.Request) (*http.Response, error), o *Options) *Request

func (*Request) Do

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

func (*Request) Done

func (r *Request) Done() bool

Done indicates whether or not the request can be attempted any more times.

Initially Done will be false. It will move to true when the following occurs:

  1. Do returns a 2xx or 3xx response
  2. Do returns an error
  3. The number of attempts exceeds MaxRetries
  4. No RetryStrategy was returned or only NoRetry, and RetryAfter() had no delay.

If Done returns true, Do must never be called again, otherwise Do will return ErrorNoMoreAttempts.

If Done returns false, Do needs to be called.

If Do has never been called, this method will always return false.

type RetryAfterFn

type RetryAfterFn func(*http.Response) time.Duration

A RetryAfterFn is used to parse the delay from the Retry-After header.

The returned time.Duration will be used as the delay.

If the header is not set, or the value cannot be parsed, a Duration of 0 must be returned.

type RetryStrategy

type RetryStrategy int
const (
	NoRetry RetryStrategy = iota
	RetryImmediate
	RetryWithInitialDelay
)

func (RetryStrategy) String

func (s RetryStrategy) String() string

String implements the Stringer interface.

type RetryStrategyFn

type RetryStrategyFn func(*http.Response) (RetryStrategy, error)

A RetryStrategyFn is used to determine whether or not a retry is needed, and if so, whether an initial delay is needed or not.

Jump to

Keyboard shortcuts

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