diff --git a/vendor/github.com/eapache/go-resiliency/retrier/README.md b/vendor/github.com/eapache/go-resiliency/retrier/README.md new file mode 100644 index 0000000..dd30af7 --- /dev/null +++ b/vendor/github.com/eapache/go-resiliency/retrier/README.md @@ -0,0 +1,26 @@ +retrier +======= + +[![Build Status](https://travis-ci.org/eapache/go-resiliency.svg?branch=master)](https://travis-ci.org/eapache/go-resiliency) +[![GoDoc](https://godoc.org/github.com/eapache/go-resiliency/retrier?status.svg)](https://godoc.org/github.com/eapache/go-resiliency/retrier) +[![Code of Conduct](https://img.shields.io/badge/code%20of%20conduct-active-blue.svg)](https://eapache.github.io/conduct.html) + +The retriable resiliency pattern for golang. + +Creating a retrier takes two parameters: +- the times to back-off between retries (and implicitly the number of times to + retry) +- the classifier that determines which errors to retry + +```go +r := retrier.New(retrier.ConstantBackoff(3, 100*time.Millisecond), nil) + +err := r.Run(func() error { + // do some work + return nil +}) + +if err != nil { + // handle the case where the work failed three times +} +``` diff --git a/vendor/github.com/eapache/go-resiliency/retrier/backoffs.go b/vendor/github.com/eapache/go-resiliency/retrier/backoffs.go new file mode 100644 index 0000000..faf6f8c --- /dev/null +++ b/vendor/github.com/eapache/go-resiliency/retrier/backoffs.go @@ -0,0 +1,24 @@ +package retrier + +import "time" + +// ConstantBackoff generates a simple back-off strategy of retrying 'n' times, and waiting 'amount' time after each one. +func ConstantBackoff(n int, amount time.Duration) []time.Duration { + ret := make([]time.Duration, n) + for i := range ret { + ret[i] = amount + } + return ret +} + +// ExponentialBackoff generates a simple back-off strategy of retrying 'n' times, and doubling the amount of +// time waited after each one. +func ExponentialBackoff(n int, initialAmount time.Duration) []time.Duration { + ret := make([]time.Duration, n) + next := initialAmount + for i := range ret { + ret[i] = next + next *= 2 + } + return ret +} diff --git a/vendor/github.com/eapache/go-resiliency/retrier/classifier.go b/vendor/github.com/eapache/go-resiliency/retrier/classifier.go new file mode 100644 index 0000000..7dd71c7 --- /dev/null +++ b/vendor/github.com/eapache/go-resiliency/retrier/classifier.go @@ -0,0 +1,66 @@ +package retrier + +// Action is the type returned by a Classifier to indicate how the Retrier should proceed. +type Action int + +const ( + Succeed Action = iota // Succeed indicates the Retrier should treat this value as a success. + Fail // Fail indicates the Retrier should treat this value as a hard failure and not retry. + Retry // Retry indicates the Retrier should treat this value as a soft failure and retry. +) + +// Classifier is the interface implemented by anything that can classify Errors for a Retrier. +type Classifier interface { + Classify(error) Action +} + +// DefaultClassifier classifies errors in the simplest way possible. If +// the error is nil, it returns Succeed, otherwise it returns Retry. +type DefaultClassifier struct{} + +// Classify implements the Classifier interface. +func (c DefaultClassifier) Classify(err error) Action { + if err == nil { + return Succeed + } + + return Retry +} + +// WhitelistClassifier classifies errors based on a whitelist. If the error is nil, it +// returns Succeed; if the error is in the whitelist, it returns Retry; otherwise, it returns Fail. +type WhitelistClassifier []error + +// Classify implements the Classifier interface. +func (list WhitelistClassifier) Classify(err error) Action { + if err == nil { + return Succeed + } + + for _, pass := range list { + if err == pass { + return Retry + } + } + + return Fail +} + +// BlacklistClassifier classifies errors based on a blacklist. If the error is nil, it +// returns Succeed; if the error is in the blacklist, it returns Fail; otherwise, it returns Retry. +type BlacklistClassifier []error + +// Classify implements the Classifier interface. +func (list BlacklistClassifier) Classify(err error) Action { + if err == nil { + return Succeed + } + + for _, pass := range list { + if err == pass { + return Fail + } + } + + return Retry +} diff --git a/vendor/github.com/eapache/go-resiliency/retrier/retrier.go b/vendor/github.com/eapache/go-resiliency/retrier/retrier.go new file mode 100644 index 0000000..ff32874 --- /dev/null +++ b/vendor/github.com/eapache/go-resiliency/retrier/retrier.go @@ -0,0 +1,69 @@ +// Package retrier implements the "retriable" resiliency pattern for Go. +package retrier + +import ( + "math/rand" + "time" +) + +// Retrier implements the "retriable" resiliency pattern, abstracting out the process of retrying a failed action +// a certain number of times with an optional back-off between each retry. +type Retrier struct { + backoff []time.Duration + class Classifier + jitter float64 + rand *rand.Rand +} + +// New constructs a Retrier with the given backoff pattern and classifier. The length of the backoff pattern +// indicates how many times an action will be retried, and the value at each index indicates the amount of time +// waited before each subsequent retry. The classifier is used to determine which errors should be retried and +// which should cause the retrier to fail fast. The DefaultClassifier is used if nil is passed. +func New(backoff []time.Duration, class Classifier) *Retrier { + if class == nil { + class = DefaultClassifier{} + } + + return &Retrier{ + backoff: backoff, + class: class, + rand: rand.New(rand.NewSource(time.Now().UnixNano())), + } +} + +// Run executes the given work function, then classifies its return value based on the classifier used +// to construct the Retrier. If the result is Succeed or Fail, the return value of the work function is +// returned to the caller. If the result is Retry, then Run sleeps according to the its backoff policy +// before retrying. If the total number of retries is exceeded then the return value of the work function +// is returned to the caller regardless. +func (r *Retrier) Run(work func() error) error { + retries := 0 + for { + ret := work() + + switch r.class.Classify(ret) { + case Succeed, Fail: + return ret + case Retry: + if retries >= len(r.backoff) { + return ret + } + time.Sleep(r.calcSleep(retries)) + retries++ + } + } +} + +func (r *Retrier) calcSleep(i int) time.Duration { + // take a random float in the range (-r.jitter, +r.jitter) and multiply it by the base amount + return r.backoff[i] + time.Duration(((r.rand.Float64()*2)-1)*r.jitter*float64(r.backoff[i])) +} + +// SetJitter sets the amount of jitter on each back-off to a factor between 0.0 and 1.0 (values outside this range +// are silently ignored). When a retry occurs, the back-off is adjusted by a random amount up to this value. +func (r *Retrier) SetJitter(jit float64) { + if jit < 0 || jit > 1 { + return + } + r.jitter = jit +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 0ad7005..7ba4b16 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -32,6 +32,12 @@ "revision": "b86b1ec0dd4209a588dc1285cdd471e73525c0b3", "revisionTime": "2016-01-04T19:15:39Z" }, + { + "checksumSHA1": "zLRMD29twG3fr3jbEUFLPGy5IlU=", + "path": "github.com/eapache/go-resiliency/retrier", + "revision": "b86b1ec0dd4209a588dc1285cdd471e73525c0b3", + "revisionTime": "2016-01-04T19:15:39Z" + }, { "checksumSHA1": "WHl96RVZlOOdF4Lb1OOadMpw8ls=", "path": "github.com/eapache/go-xerial-snappy",