Documentation ¶
Overview ¶
Package retrier provides the ability to automatically repeat a user-defined function based on the error status.
The default behavior is to retry in case of any error.
This package also offers ready-made DefaultRetryIf function that can be used for most cases.
Additionally, it allows you to set the maximum number of retries, the delay after the first failed attempt, the time multiplication factor to determine the successive delay value, and the jitter used to introduce randomness and avoid request collisions.
Index ¶
Examples ¶
Constants ¶
const ( // DefaultAttempts is the default maximum number of retry attempts. DefaultAttempts = 4 // DefaultDelay is the default delay to apply after the first failed attempt. DefaultDelay = 1 * time.Second // DefaultDelayFactor is the default multiplication factor to get the successive delay value. DefaultDelayFactor = 2 // DefaultJitter is the default maximum random Jitter time between retries. DefaultJitter = 1 * time.Millisecond // DefaultTimeout is the default timeout applied to each function call via context. DefaultTimeout = 1 * time.Second )
Variables ¶
This section is empty.
Functions ¶
func DefaultRetryIf ¶
DefaultRetryIf is the default function to check the retry condition.
Types ¶
type Option ¶
Option is the interface that allows to set the options.
func WithAttempts ¶
WithAttempts set the maximum number of retries.
func WithDelayFactor ¶
WithDelayFactor set the multiplication factor to get the successive delay value. A delay factor greater than 1 means an exponential delay increase. if the delay factor is 2 and the first delay is 1, then the delays will be: [1, 2, 4, 8, ...].
func WithJitter ¶
WithJitter sets the maximum random Jitter time between retries. This is useful to avoid the Thundering herd problem (https://en.wikipedia.org/wiki/Thundering_herd_problem).
func WithRetryIfFn ¶
WithRetryIfFn set the function used to decide when retry.
func WithTimeout ¶
WithTimeout sets the timeout applied to each function call via context.
type Retrier ¶
type Retrier struct {
// contains filtered or unexported fields
}
Retrier represents an instance of the HTTP retrier.
func (*Retrier) Run ¶
Run attempts to execute the task according to the retry rules.
Example ¶
package main import ( "context" "errors" "fmt" "log" "time" "github.com/Vonage/gosrvlib/pkg/retrier" ) func main() { var count int // example function that returns nil only at the third attempt. task := func(_ context.Context) error { if count == 2 { return nil } count++ return errors.New("ERROR") } opts := []retrier.Option{ retrier.WithRetryIfFn(retrier.DefaultRetryIf), retrier.WithAttempts(5), retrier.WithDelay(10 * time.Millisecond), retrier.WithDelayFactor(1.1), retrier.WithJitter(5 * time.Millisecond), retrier.WithTimeout(2 * time.Millisecond), } r, err := retrier.New(opts...) if err != nil { log.Fatal(err) } timeout := 1 * time.Second ctx, cancel := context.WithTimeout(context.TODO(), timeout) err = r.Run(ctx, task) cancel() if err != nil { log.Fatal(err) } fmt.Println(count) }
Output: 2