Package retry implements retry logic helpers, which can be used to wrap operations that can intermittently fail, but can be retried at a higher level.

    Consider replacing with This would have slightly different values for backoff consts.



    View Source
    const UnlimitedAttempts = Attempts(0)

      UnlimitedAttempts is used to specify no limit to the number of attempts.


      View Source
      var TimeAfterContextKey = timeAfterContextKey{}

        TimeAfterContextKey is to be used as a key in the context to provide a value that is compatible with time.After. The main purpose is to mock out time.After in the tests.


        func Always

        func Always(error) bool

          Always always retries, regardless of error.

          func WithPolicy

          func WithPolicy(ctx context.Context, shouldRetry ShouldRetry, bp BackoffPolicy, f func() error) error

            WithPolicy retries f until either it succeeds, or shouldRetry returns false, or the number of retries is capped by the backoff policy. Returns the error returned by the final attempt. It annotates the error message in case the retry budget is exhausted.


            type Attempts

            type Attempts uint

              Attempts is the number of times to attempt something before giving up. A value of 0 represents an effectively unlimited number of attempts, or you can use the equivalent retry.UnlimitedAttempts.

              type BackoffPolicy

              type BackoffPolicy struct {
              	// contains filtered or unexported fields

                BackoffPolicy describes how to back off when retrying, and how many times to retry.

                func ExponentialBackoff

                func ExponentialBackoff(baseDelay, maxDelay time.Duration, attempts Attempts) BackoffPolicy

                  ExponentialBackoff returns an exponential backoff implementation.

                  Starting from baseDelay, it will delay by an additional fixed multiple with each retry, never delaying by more than maxDelay. For example, ExponentialBackoff(time.Second, time.Hour, 5) will produce delays of roughly: 1s, 1.5s, 2s, 3s, 4s.

                  Note that delays are randomized, so the exact values are not guaranteed. attempts=0 means unlimited attempts. See UnlimitedAttempts.

                  func Immediately

                  func Immediately(attempts Attempts) BackoffPolicy

                    Immediately returns a retrier that retries right away.

                    type ShouldRetry

                    type ShouldRetry func(error) bool

                      ShouldRetry encapsulates the decision of whether an error is retry-able. If an error should not be retried, the function must return false.

                      Source Files