Documentation ¶
Index ¶
- Constants
- Variables
- func StreamClientInterceptor(max uint, interval time.Duration, opts ...Option) grpc.StreamClientInterceptor
- func UnaryClientInterceptor(max uint, interval time.Duration, opts ...Option) grpc.UnaryClientInterceptor
- type BackoffFunc
- type CallOption
- type OnRetryCallback
- type Option
- type RetriableFunc
Constants ¶
const (
AttemptMetadataKey = "x-retry-attempt"
)
Variables ¶
var DefaultRetriableCodes = []codes.Code{codes.ResourceExhausted, codes.Unavailable, codes.Unknown}
Functions ¶
func StreamClientInterceptor ¶
func StreamClientInterceptor(max uint, interval time.Duration, opts ...Option) grpc.StreamClientInterceptor
StreamClientInterceptor returns a new retrying stream client interceptor for server side streaming calls.
The default configuration of the interceptor is to not retry *at all*. This behaviour can be changed through options (e.g. WithMax) on creation of the interceptor or on call (through grpc.CallOptions).
Retry logic is available *only for ServerStreams*, i.e. 1:n streams, as the internal logic needs to buffer the messages sent by the client. If retry is enabled on any other streams (ClientStreams, BidiStreams), the retry interceptor will fail the call.
func UnaryClientInterceptor ¶
func UnaryClientInterceptor(max uint, interval time.Duration, opts ...Option) grpc.UnaryClientInterceptor
UnaryClientInterceptor returns a new retrying unary client interceptor.
The default configuration of the interceptor is to not retry *at all*. This behaviour can be changed through options (e.g. WithMax) on creation of the interceptor or on call (through grpc.CallOptions).
Types ¶
type BackoffFunc ¶
BackoffFunc denotes a family of functions that control the backoff duration between call retries.
They are called with an identifier of the attempt, and should return a time the system client should hold off for. If the time returned is longer than the `context.Context.Deadline` of the request the deadline of the request takes precedence and the wait will be interrupted before proceeding with the next iteration. The context can be used to extract request scoped metadata and context values.
func BackoffExponential ¶
func BackoffExponential(scalar time.Duration) BackoffFunc
BackoffExponential produces increasing intervals for each attempt. The scalar is multiplied times 2 raised to the current attempt. So the first retry with a scalar of 100ms is 100ms, while the 5th attempt would be 1.6s.
func BackoffExponentialWithJitter ¶
func BackoffExponentialWithJitter(scalar time.Duration, jitterFraction float64) BackoffFunc
BackoffExponentialWithJitter creates an exponential backoff like BackoffExponential does, but adds jitter.
func BackoffLinear ¶
func BackoffLinear(waitBetween time.Duration) BackoffFunc
BackoffLinear is very simple: it waits for a fixed period of time between calls.
func BackoffLinearWithJitter ¶
func BackoffLinearWithJitter(waitBetween time.Duration, jitterFraction float64) BackoffFunc
BackoffLinearWithJitter waits a set period of time, allowing for jitter (fractional adjustment). For example waitBetween=1s and jitter=0.10 can generate waits between 900ms and 1100ms.
type CallOption ¶
type CallOption struct { grpc.EmptyCallOption // make sure we implement private after() and before() fields so we don't panic. // contains filtered or unexported fields }
CallOption is a grpc.CallOption that is local to grpc_retry.
type OnRetryCallback ¶
OnRetryCallback is the type of function called when a retry occurs.
type Option ¶
type Option func(*options)
func WithBackoff ¶
func WithBackoff(bf BackoffFunc) Option
WithBackoff sets the `BackoffFunc` used to control time between retries.
func WithHeader ¶
WithHeader denotes if set the parent header to new call.
func WithOnRetryCallback ¶
func WithOnRetryCallback(fn OnRetryCallback) Option
WithOnRetryCallback sets the callback to use when a retry occurs.
By default, when no callback function provided, we will just print a log to trace
func WithPerRetryTimeout ¶
WithPerRetryTimeout sets the RPC timeout per call (including initial call) on this call, or this interceptor.
The context.Deadline of the call takes precedence and sets the maximum time the whole invocation will take, but WithPerRetryTimeout can be used to limit the RPC time per each call.
For example, with context.Deadline = now + 10s, and WithPerRetryTimeout(3 * time.Seconds), each of the retry calls (including the initial one) will have a deadline of now + 3s.
A value of 0 disables the timeout overrides completely and returns to each retry call using the parent `context.Deadline`.
Note that when this is enabled, any DeadlineExceeded errors that are propagated up will be retried.
func WithRetriable ¶
func WithRetriable(retriableFunc RetriableFunc) Option
WithRetriable sets which error should be retried.
type RetriableFunc ¶
RetriableFunc denotes a family of functions that control which error should be retried.