retry

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2018 License: MIT Imports: 9 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.

Documentation

Overview

Package retry contains set of cliware middlewares and other utilities used for retry logic. Any of it to actually be applied, client has to use RoundTripper returned via retry.NewRoundTripper function as it implements all logic and applies all config values set by middlewares.

This RoundTripper wraps your actual round tripper and uses it to send requests, so you can still apply you custom settings.

Setting this RoundTripper is something that client library should do (like github.com/delicb/gwc), so, in general, for writing simple endpoints there should be no need to use it.

Index

Constants

This section is empty.

Variables

ErrorOr500Plus is classifier that combines AnyError and On500Plus classifiers. This means that classifier will classify any response that returned error or status code >= 500 to be retried.

Functions

func AnyErrorClassifier

func AnyErrorClassifier(resp *http.Response, err error) bool

AnyErrorClassifier is classifier that indicates that all requests that return errors should be retried.

func CacheBodyStrategy

func CacheBodyStrategy(r *http.Request) (func() io.ReadCloser, error)

CacheBodyStrategy caches initial request body in buffer and returns it every time it is needed.

func Enable

func Enable(client *http.Client)

Enable modifiers provided client so that it can support all retry mechanisms implemented in this package. What it does it to replace client.Transport with RoundTripper that knows how to read and use values set via different middlewares. Original transport is still used for request sending, but it might be used multiple times (depending on retry logic).

func MaxDuration

func MaxDuration(maxTime time.Duration) c.Middleware

MaxDuration sets maximum amount of time to retry. This time includes everything, all requests, etc...

func Methods

func Methods(methods ...string) c.Middleware

Methods sets list of HTTP methods for which it is valid to retry failed requests. For example, if only GET is defined as valid retry HTTP methods, no POST or PUT (or any other) request will be retried. This might be useful if you are not sure about server idempotence.

func NewRetryTransport

func NewRetryTransport(next http.RoundTripper) http.RoundTripper

NewRetryTransport returns RoundTripper that wraps around provided RoundTripper and adds retry logic around it. All retry parameters are read from http.Request context (with sane defaults).

func On500PlusClassifier

func On500PlusClassifier(resp *http.Response, err error) bool

On500PlusClassifier is classifier that indicates that all requests whose response code is 500 or higher should be repeated.

func SetBackoffStrategy

func SetBackoffStrategy(backoff func(n int) time.Duration) c.Middleware

SetBackoffStrategy sets backoff strategy for requests that classifier marked to be retried.

func SetBodyStrategy

func SetBodyStrategy(strategy func(r *http.Request) (func() io.ReadCloser, error)) c.Middleware

SetBodyStrategy sets strategy of how to handle request body for retries requests.

func SetClassifier

func SetClassifier(classifier func(*http.Response, error) bool) c.Middleware

SetClassifier sets provided classifier to request.

func Times

func Times(times int) c.Middleware

Times sets maximum number of times for HTTP request sending retry.

Types

type BackoffStrategy

type BackoffStrategy func(n int) time.Duration

BackoffStrategy is function that calculates for how long should delay be before sending next request.

func ConstantBackoff

func ConstantBackoff(duration time.Duration) BackoffStrategy

ConstantBackoff returns backoff that returns always same, provided, parameter.

func ExponentialBackoff

func ExponentialBackoff(min, max time.Duration, factor float64) BackoffStrategy

ExponentialBackoff returns BackoffStrategy that exponentially increases time.

func ExponentialJitterBackoff

func ExponentialJitterBackoff(min, max time.Duration, factor float64) BackoffStrategy

ExponentialJitterBackoff returns BackoffStrategy that exponentially increases time and adds random jitter to result.

func LinearBackoff

func LinearBackoff(step time.Duration, max time.Duration) BackoffStrategy

LinearBackoff returns BackoffStrategy that multiplies provided step with repeat attempt number.

func LinearJitterBackoff

func LinearJitterBackoff(step time.Duration, max time.Duration) BackoffStrategy

LinearJitterBackoff returns BackoffStrategy that multiplies provided step with attempt number and adds random jitter to result.

type BodyStrategy

type BodyStrategy func(r *http.Request) (func() io.ReadCloser, error)

BodyStrategy defines what to do with request body in event when request has to be send again (because Classifier determined so). Strategy accepts request (this will be original request always) and returns function that should provide io.ReadCloser that will be set to every subsequent request body. This function will be called multiple times (as many times as request is retried).

type Classifier

type Classifier func(resp *http.Response, err error) (shouldRetry bool)

Classifier is function that determines if request should be retried. Boolean return value indicates if request should be repeated or not.

func AndClassifier

func AndClassifier(classifiers ...Classifier) Classifier

AndClassifier is classifier that combines other classifiers. Returned classifier will indicate that request should be repeated if all of provided classifiers returned true.

func OrClassifier

func OrClassifier(classifiers ...Classifier) Classifier

OrClassifier is classifier that combines other classifiers. Returned classifier will indicate that request should be repeated if any or provided classifiers returned true.

Jump to

Keyboard shortcuts

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