retrier

package
v1.81.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package retrier allow to retry execute a function in case of errors.

Index

Examples

Constants

View Source
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

func DefaultRetryIf(err error) bool

DefaultRetryIf is the default function to check the retry condition.

Types

type Option

type Option func(c *Retrier) error

Option is the interface that allows to set the options.

func WithAttempts

func WithAttempts(attempts uint) Option

WithAttempts set the maximum number of retries.

func WithDelay

func WithDelay(delay time.Duration) Option

WithDelay set the delay after the first failed attempt.

func WithDelayFactor

func WithDelayFactor(delayFactor float64) Option

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

func WithJitter(jitter time.Duration) Option

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

func WithRetryIfFn(retryIfFn RetryIfFn) Option

WithRetryIfFn set the function used to decide when retry.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

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 New

func New(opts ...Option) (*Retrier, error)

New creates a new instance.

func (*Retrier) Run

func (r *Retrier) Run(ctx context.Context, task TaskFn) error

Run attempts to execute the task according to the retry rules.

Example
package main

import (
	"context"
	"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 fmt.Errorf("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

type RetryIfFn

type RetryIfFn func(err error) bool

RetryIfFn is the signature of the function used to decide when retry.

type TaskFn

type TaskFn func(ctx context.Context) error

TaskFn is the type of function to be executed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL