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 ¶
- Variables
- func AnyErrorClassifier(resp *http.Response, err error) bool
- func CacheBodyStrategy(r *http.Request) (func() io.ReadCloser, error)
- func Enable(client *http.Client)
- func MaxDuration(maxTime time.Duration) c.Middleware
- func Methods(methods ...string) c.Middleware
- func NewRetryTransport(next http.RoundTripper) http.RoundTripper
- func On500PlusClassifier(resp *http.Response, err error) bool
- func SetBackoffStrategy(backoff func(n int) time.Duration) c.Middleware
- func SetBodyStrategy(strategy func(r *http.Request) (func() io.ReadCloser, error)) c.Middleware
- func SetClassifier(classifier func(*http.Response, error) bool) c.Middleware
- func Times(times int) c.Middleware
- type BackoffStrategy
- func ConstantBackoff(duration time.Duration) BackoffStrategy
- func ExponentialBackoff(min, max time.Duration, factor float64) BackoffStrategy
- func ExponentialJitterBackoff(min, max time.Duration, factor float64) BackoffStrategy
- func LinearBackoff(step time.Duration, max time.Duration) BackoffStrategy
- func LinearJitterBackoff(step time.Duration, max time.Duration) BackoffStrategy
- type BodyStrategy
- type Classifier
Constants ¶
This section is empty.
Variables ¶
var ErrorOr500Plus = OrClassifier(AnyErrorClassifier, On500PlusClassifier)
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.