retry

package
v0.0.0-...-654d6e8 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package retry is for executing a function repeatedly until it was successful or canceled by the context.

Index

Examples

Constants

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

func Retry(retryFunc RetryFunc, opts ...Option) error

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

func Context(ctx context.Context) Option

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

func RetryTimes(n uint) Option

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

func RetryWithLinearBackoff(interval time.Duration) Option

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

type RetryFunc

type RetryFunc func() error

RetryFunc is function that retry executes

Jump to

Keyboard shortcuts

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