retry

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2021 License: MIT Imports: 4 Imported by: 0

README

go-retry

test codecov go.dev reference go report card license

Provides a retry function that allows for step-by-step execution.

Installation

go get -u github.com/kenkyu392/go-retry

Usage

package main

import (
	"context"
	"log"

	"github.com/kenkyu392/go-retry"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	// Or you can use Do(retry.Duration(time.Second)...) to specify the delay before retrying.
	// Retry only the function in error, not all functions.
	errs := retry.DoWithContext(ctx,
		// Use exponential backoff with a maximum of 5 retries for the
		// delay time specification function.
		retry.ExponentialBackoff(5),
		// Execute the first step...
		func(ctx context.Context) error {
			// You can use Skipped or Canceled in a function to skip the current
			// function or cancel all remaining functions.
			return nil
		},
		// Execute the second step...
		func(ctx context.Context) error {
			return nil
		},
	)
	// All errors encountered during execution are returned in an array.
	for _, err := range errs {
		log.Print(err)
	}
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Canceled is used to cancel a retry.
	Canceled = errors.New("canceled")
	// Skipped is used to skip a function.
	Skipped = errors.New("skipped")
)

Functions

func Do

func Do(delayFn DurationFunc, fns ...func(context.Context) error) []error

Do wraps DoWithContext using the background context.

func DoWithContext

func DoWithContext(ctx context.Context, delayFn DurationFunc, fns ...func(context.Context) error) []error

DoWithContext executes the given functions in order. If the function returns an error, the function will be executed again after the time specified by delay. If the result of delayFn is negative, execute the next function without retrying.

Types

type DurationFunc added in v0.3.0

type DurationFunc func(retries int) time.Duration

DurationFunc ...

func Duration added in v0.3.0

func Duration(d time.Duration) DurationFunc

Duration creates a DurationFunc that returns a Duration.

func ExponentialBackoff added in v0.3.0

func ExponentialBackoff(maxRetries int) DurationFunc

ExponentialBackoff creates and returns a DurationFunc that exponentially backoff the retry interval. If maxRetries is negative, retry without limit.

Jump to

Keyboard shortcuts

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