Documentation ¶
Overview ¶
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
Index ¶
- Constants
- func Retry(retryFunc RetryFunc, opts ...Option) error
- type BackoffStrategy
- type Option
- func Context(ctx context.Context) Option
- func RetryTimes(n uint) Option
- func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
- func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
- func RetryWithLinearBackoff(interval time.Duration) Option
- type RetryConfig
- type RetryFunc
Examples ¶
Constants ¶
const ( // DefaultRetryTimes times of retry DefaultRetryTimes = 5 // DefaultRetryDuration time duration of two retries DefaultRetryLinearInterval = time.Second * 3 )
Variables ¶
This section is empty.
Functions ¶
func Retry ¶
Retry executes the retryFunc repeatedly until it was successful or canceled by the context The default times of retries is 5 and the default duration between retries is 3 seconds. Play: https://go.dev/play/p/nk2XRmagfVF
Example ¶
number := 0 increaseNumber := func() error { number++ if number == 3 { return nil } return errors.New("error occurs") } err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50)) if err != nil { return } fmt.Println(number)
Output: 3
Types ¶
type BackoffStrategy ¶
type BackoffStrategy interface { // CalculateInterval returns the time.Duration after which the next retry attempt should be made. CalculateInterval() time.Duration }
BackoffStrategy is an interface that defines a method for calculating backoff intervals.
type Option ¶
type Option func(*RetryConfig)
Option is for adding retry config
func Context ¶
Context set retry context config. Play: https://go.dev/play/p/xnAOOXv9GkS
Example ¶
ctx, cancel := context.WithCancel(context.TODO()) number := 0 increaseNumber := func() error { number++ if number > 3 { cancel() } return errors.New("error occurs") } Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50), Context(ctx), ) fmt.Println(number)
Output: 4
func RetryTimes ¶
RetryTimes set times of retry. Play: https://go.dev/play/p/ssfVeU2SwLO
Example ¶
number := 0 increaseNumber := func() error { number++ if number == 3 { return nil } return errors.New("error occurs") } err := Retry(increaseNumber, RetryTimes(2)) if err != nil { fmt.Println(err) }
Output: function retry.ExampleRetryTimes.func1 run failed after 2 times retry
func RetryWithCustomBackoff ¶
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
RetryWithCustomBackoff set abitary custom backoff strategy Play: todo
Example ¶
number := 0 increaseNumber := func() error { number++ if number == 3 { return nil } return errors.New("error occurs") } err := Retry(increaseNumber, RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50})) if err != nil { return } fmt.Println(number)
Output: 3
func RetryWithExponentialWithJitterBackoff ¶
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
RetryWithExponentialWithJitterBackoff set exponential strategy backoff Play: todo
Example ¶
number := 0 increaseNumber := func() error { number++ if number == 3 { return nil } return errors.New("error occurs") } err := Retry(increaseNumber, RetryWithExponentialWithJitterBackoff(time.Microsecond*50, 2, time.Microsecond*25)) if err != nil { return } fmt.Println(number)
Output: 3
func RetryWithLinearBackoff ¶
RetryWithLinearBackoff set linear strategy backoff Play: todo
Example ¶
number := 0 increaseNumber := func() error { number++ if number == 3 { return nil } return errors.New("error occurs") } err := Retry(increaseNumber, RetryWithLinearBackoff(time.Microsecond*50)) if err != nil { return } fmt.Println(number)
Output: 3
type RetryConfig ¶
type RetryConfig struct {
// contains filtered or unexported fields
}
RetryConfig is config for retry