retry

package module
v0.0.0-...-fc10025 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2019 License: MIT Imports: 2 Imported by: 0

README

go-retry

GoDoc

Retry operation executor with init and clean up functions

Example

ctx := context.Background()

value := 5

Retry(
    ctx,
    func(_ context.Context, v interface{}) error {
        if &value != v {
            return fmt.Errorf("Value diff")
        }
        return nil
    },
    Init(func(ctx context.Context) (interface{}, error) {
        return &value, nil
    }),
    Clean(func(ctx context.Context, v interface{}, e error) error {
        if &value != v {
            return fmt.Errorf("Value diff")
        }
        return e
    }),
)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxAttemptsReached = errors.New("Max attempts reached")
)

Functions

func DefaultCleaner

func DefaultCleaner(ctx context.Context, _ interface{}, _ error) error

func DefaultInitiator

func DefaultInitiator(ctx context.Context) (interface{}, error)

func Retry

func Retry(ctx context.Context, actor Actor, opts ...Option) error

Retry executes "actor" until it returns nil

Types

type Actor

type Actor func(ctx context.Context, value interface{}) error

Actor func to be called on every attempt

type Cleaner

type Cleaner func(ctx context.Context, value interface{}, lastActorErr error) error

Cleaner func to be called once after every attempt lastActorErr contains the last error emited by Actor, if any. Nil otherwise

type Initiator

type Initiator func(ctx context.Context) (interface{}, error)

Initiator init func to be called once before every attempt

type Option

type Option func(opts *Options) error

func Clean

func Clean(clean Cleaner) Option

func Init

func Init(init Initiator) Option

func MaxAttempts

func MaxAttempts(max int) Option

type Options

type Options struct {
	MaxAttempts int
	Init        Initiator
	Clean       Cleaner
}

func (*Options) SetDefaults

func (opts *Options) SetDefaults()

Jump to

Keyboard shortcuts

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