Documentation
¶
Overview ¶
Package retrybp integrates with https://github.com/avast/retry-go that provides some baseplate-specific logic and changes some defaults.
Index ¶
- Constants
- func CappedExponentialBackoff(args CappedExponentialBackoffArgs) retry.Option
- func ContextErrorFilter(err error, next retry.RetryIfFunc) bool
- func Do(ctx context.Context, fn func() error, defaults ...retry.Option) error
- func Filters(filters ...Filter) retry.Option
- func FixedDelay(delay time.Duration) retry.Option
- func FixedDelayFunc(delay time.Duration) retry.DelayTypeFunc
- func GetOptions(ctx context.Context) (options []retry.Option, ok bool)
- func NetworkErrorFilter(err error, next retry.RetryIfFunc) bool
- func PoolExhaustedFilter(err error, next retry.RetryIfFunc) bool
- func RetryableErrorFilter(err error, next retry.RetryIfFunc) bool
- func Unrecoverable(err error) error
- func UnrecoverableErrorFilter(err error, next retry.RetryIfFunc) bool
- func WithOptions(ctx context.Context, options ...retry.Option) context.Context
- type CappedExponentialBackoffArgs
- type Filter
- type RetryAfterError
- type RetryableError
Constants ¶
const ( // DefaultFilterDecision is the default decision returned by Filters at the end // of the filter chain. DefaultFilterDecision = false )
Variables ¶
This section is empty.
Functions ¶
func CappedExponentialBackoff ¶ added in v0.7.0
func CappedExponentialBackoff(args CappedExponentialBackoffArgs) retry.Option
CappedExponentialBackoff is an exponentially backoff delay implementation that makes sure the delays are properly capped.
func ContextErrorFilter ¶
func ContextErrorFilter(err error, next retry.RetryIfFunc) bool
ContextErrorFilter returns false if the error is context.Cancelled or context.DeadlineExceeded, otherwise it calls the next filter in the chain.
func Do ¶
Do retries the given function using retry.Do with the default retry.Options provided and overriding them with any options set on the context via WithOptions.
The changes this has compared to retry.Do are:
1. Pulling options from the context. This allows it to be used in middleware where you are not calling Do directly but still want to be able to configure retry behavior per-call.
2. If retry.Do returns a batch of errors (retry.Error), put those in a errorsbp.Batch from baseplate.go.
func Filters ¶
Filters returns a `retry.RetryIf` function that checks the error against the given filters and returns either the decision reached by a filter or the DefaultFilterDecision.
You should not use this with any other retry.RetryIf options as one will override the other.
func FixedDelay ¶ added in v0.7.0
FixedDelay is a delay option to use fixed delay between retries.
To achieve the same result via upstream retry package's API, you would need to combine retry.Delay and retry.DelayType(retry.FixedDelay), which is confusing and error-prone. As a result we provide this API to make things easier.
If you want to combine FixedDelay with a random jitter, you could use FixedDelayFunc with retry.RandomDelay, example:
retry.DelayType(retry.CombineDelay(retry.RandomDelay, retrybp.FixedDelayFunc(delay)))
func FixedDelayFunc ¶ added in v0.7.0
func FixedDelayFunc(delay time.Duration) retry.DelayTypeFunc
FixedDelayFunc is an retry.DelayTypeFunc implementation causing fixed delays.
func GetOptions ¶
GetOptions returns the list of retry.Options set on the context.
func NetworkErrorFilter ¶
func NetworkErrorFilter(err error, next retry.RetryIfFunc) bool
NetworkErrorFilter returns true if the error is a net.Error error otherwise it calls the next filter in the chain.
This filter assumes that the error is due to a problem with the specific connection that was used to make the requset and using a new connection would fix the problem, which is why it retries on every net.Error error rather than inspecting the error for more details to determine if it is appropriate to retry or not.
This should only be used for idempotent requests. You don't want to retry calls that have side effects if the network connection broke down sometime between sending and receiving since you have no way of knowing if the callee receieved and is already processing your request.
func PoolExhaustedFilter ¶
func PoolExhaustedFilter(err error, next retry.RetryIfFunc) bool
PoolExhaustedFilter returns true if the error is an clientpool.ErrExhausted error otherwise it calls the next filter in the chain.
This is safe to use even if a request is not idempotent as this error happens before any network calls are made. It is best paired with some backoff though to give the pool some time to recover.
DEPRECATED: clientpool.ErrExhausted implements RetryableError, so RetryableErrorFilter covers the functionality of this filter and should be used instead.
func RetryableErrorFilter ¶ added in v0.8.0
func RetryableErrorFilter(err error, next retry.RetryIfFunc) bool
RetryableErrorFilter is a Filter implementation that checks RetryableError.
If err is not an implementation of RetryableError, or if its Retryable() returns nil, it defers to the next filter. Otherwise it use the Retryable() result.
It also checks against thrift exceptions with an optional boolean field named "retryable" defined, and use that field as the decision (unset means no decision).
In addition, it also checks retry.IsRecoverable, in case retry.Unrecoverable was used instead of retrybp.Unrecoverable.
In most cases this should be the first in the filter chain, because functions could use Unrecoverable to wrap errors that would return true in other filter implementations to explicitly override those filter behaviors.
func Unrecoverable ¶ added in v0.8.0
Unrecoverable wraps an error and mark it as unrecoverable by implementing RetryableError and returning false on Retryable().
It's similar to retry.Unrecoverable, but properly implements error unwrapping API in go 1.13+. As a result, it's preferred over retry.Unrecoverable.
func UnrecoverableErrorFilter ¶
func UnrecoverableErrorFilter(err error, next retry.RetryIfFunc) bool
UnrecoverableErrorFilter returns false if the error is an retry.Unrecoverable error otherwise it calls the next filter in the chain.
This uses retry.IsRecoverable which relies on wrapping the error with retry.Unrecoverable. It also does not use the "errors" helpers so if the the error returned by retry.Unrecoverable is further wrapped, this will not be able to make a decision.
DEPRECATED: Please use retrybp.Unrecoverable and RetryableErrorFilter instead.
Types ¶
type CappedExponentialBackoffArgs ¶ added in v0.7.0
type CappedExponentialBackoffArgs struct {
// The initial delay.
// If <=0, retry.DefaultDelay will be used.
// If retry.DefaultDelay <= 0, 1 nanosecond will be used.
InitialDelay time.Duration
// The cap of InitialDelay<<n. If <=0, it will only be capped at MaxExponent.
//
// Please note that it doesn't cap the MaxJitter part,
// so the actual max delay could be MaxDelay+MaxJitter.
MaxDelay time.Duration
// We calculate the delay before jitter by using InitialDelay<<n
// (n being the number of retries). MaxExponent caps the n part.
//
// If <=0, it will be calculated based on InitialDelay to make sure that it
// won't overflow signed int64.
// If it's set to a value too high that would overflow,
// it will also be adjusted automatically.
//
// Please note that MaxExponent doesn't limit the number of actual retries.
// It only caps the number of retries used in delay value calculation.
MaxExponent int
// Max random jitter to be added to each retry delay.
// If <=0, no random jitter will be added.
MaxJitter time.Duration
// When IgnoreRetryAfterError is set to false (default),
// and the error caused the retry implements RetryAfterError,
// and the returned RetryAfterDuration > 0,
// it's guaranteed that the delay value is at least RetryAfterDuration + jitter.
//
// If the returned RetryAfterDuration conflicts with (is larger than) MaxDelay
// or the calculated delay result from MaxExponent,
// RetryAfterDuration takes priority.
IgnoreRetryAfterError bool
}
CappedExponentialBackoffArgs defines the args used in CappedExponentialBackoff retry option.
All args are optional.
type Filter ¶
type Filter func(err error, next retry.RetryIfFunc) bool
Filter is a function that is passed an error and attempts to determine if the request should be retried given the error.
Filters should only implement a single check that will generally determine whether the request should be retried or not. If it cannot make that decision on it's own, it should call the next RetryIfFunc in the chain.
If a filter is doing more than that, there's a good chance that it is doing too much.
type RetryAfterError ¶ added in v0.8.0
type RetryAfterError interface {
error
// If RetryAfterDuration returns a duration <= 0,
// it's considered as not having retry-after info.
RetryAfterDuration() time.Duration
}
RetryAfterError defines a type of errors that contain retry-after information (for example, HTTP's Retry-After header).
httpbp.ClientError is an error type that implements this interface.
type RetryableError ¶ added in v0.8.0
type RetryableError interface {
error
// Errors should return 0 if there's not enough information to make a
// decision, or >0 to indicate that it's retryable, and <0 means it's not.
Retryable() int
}
RetryableError defines an optional error interface to return retryable info.