Documentation
¶
Overview ¶
Package expbackoff implements exponential backoff. It was copied from google.golang.org/grpc.
Index ¶
- Variables
- func Backoff(config Config, retries int) time.Duration
- func New(ctx context.Context, opts ...func(*Config)) (backoff func())
- func NewWithReset(ctx context.Context, opts ...func(*Config)) (backoff func(), reset func())
- func SetAfterForT(t *testing.T, fn func(d time.Duration) <-chan time.Time)
- func SetRandFloatForT(t *testing.T, fn func() float64)
- func WithBaseDelay(d time.Duration) func(*Config)
- func WithConfig(c Config) func(*Config)
- func WithFastConfig() func(*Config)
- func WithMaxDelay(d time.Duration) func(*Config)
- type Config
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ BaseDelay: 1.0 * time.Second, Multiplier: 1.6, Jitter: 0.2, MaxDelay: 120 * time.Second, }
DefaultConfig is a backoff configuration with the default values specified at https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md.
This should be useful for callers who want to configure backoff with non-default values only for a subset of the options.
Copied from google.golang.org/grpc@v1.48.0/backoff/backoff.go.
var FastConfig = Config{ BaseDelay: 100 * time.Millisecond, Multiplier: 1.6, Jitter: 0.2, MaxDelay: 5 * time.Second, }
FastConfig is a common configuration for fast backoff.
Functions ¶
func Backoff ¶
Backoff returns the amount of time to wait before the next retry given the number of retries. Copied from google.golang.org/grpc@v1.48.0/internal/backoff/backoff.go.
func New ¶
New returns a backoff function configured via functional options applied to DefaultConfig. The backoff function will exponentially sleep longer each time it is called. The backoff function returns immediately after the context is cancelled.
Usage:
backoff := expbackoff.New(ctx)
for ctx.Err() == nil {
resp, err := doThing(ctx)
if err != nil {
backoff()
continue
} else {
return resp
}
}
func NewWithReset ¶
NewWithReset returns a backoff and a reset function configured via functional options applied to DefaultConfig. The backoff function will exponentially sleep longer each time it is called. Calling the reset function will reset the backoff sleep duration to Config.BaseDelay. The backoff function returns immediately after the context is cancelled.
Usage:
backoff, reset := expbackoff.NewWithReset(ctx)
for ctx.Err() == nil {
resp, err := doThing(ctx)
if err != nil {
backoff()
continue
} else {
reset()
// Do something with the response.
}
}
func SetAfterForT ¶
SetAfterForT sets the after internal function for testing.
func SetRandFloatForT ¶
SetRandFloatForT sets the random float internal function for testing.
func WithBaseDelay ¶
WithBaseDelay configures the backoff with the provided max delay.
func WithConfig ¶
WithConfig configures the backoff with the provided config.
func WithFastConfig ¶
func WithFastConfig() func(*Config)
WithFastConfig configures the backoff with FastConfig.
func WithMaxDelay ¶
WithMaxDelay configures the backoff with the provided max delay.
Types ¶
type Config ¶
type Config struct {
// BaseDelay is the amount of time to backoff after the first failure.
BaseDelay time.Duration
// Multiplier is the factor with which to multiply backoffs after a
// failed retry. Should ideally be greater than 1.
Multiplier float64
// Jitter is the factor with which backoffs are randomized.
Jitter float64
// MaxDelay is the upper bound of backoff delay.
MaxDelay time.Duration
}
Config defines the configuration options for backoff.