retry

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

retry

Golang retry library.

About The Project

Retry - small library, that provides api to easy retry function execution and handling errors. Package has some benefits unlike known implementations:

  • laconic api
  • rich retry strategy set
  • easy customization
  • context package support

Usage

Retry function until success:

_ = retry.Exec(func(retryNumber int) (err error) {
    // your logic here
    return
})

Limit attempts:

err := retry.Exec(func(retryNumber int) (err error) {
    // your logic here
    return
}, retry.MaxAttempts(10))

Delay between retries:

err := retry.Exec(func(retryNumber int) (err error) {
    // your logic here
    return
}, retry.FixedDelay(time.Second))

Breaking retry loop:

err := retry.Exec(func(retryNumber int) (err error) {
    // your logic here
    if !canRetry {
        err = retry.Unrecoverable(err)
        return
    }
    return
})

Using context:

err := retry.ExecContext(ctx, func(ctx context.Context, retryNumber int) (err error) {
    // your logic here
    return
})

Cancellation:

ctx, cancel := context.WithCancel(context.Background())
go func() {
    // wait some event
    cancel()
}()
err := retry.ExecContext(ctx, func(ctx context.Context, retryNumber int) (err error) {
    // your logic here
    return
})

Similar projects

License

Retry is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Contact

  • Email: cherkashin.evgeny.viktorovich@gmail.com
  • Telegram: @evgeny_cherkashin

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec

func Exec(
	retryableFunc RetryableFunc,
	strategies ...Strategy,
) (err error)

func ExecContext

func ExecContext(
	ctx context.Context,
	retryableFunc RetryableContextFunc,
	strategies ...Strategy,
) (err error)

func GetUnrecoverableErrorCause

func GetUnrecoverableErrorCause(unrecoverableErr error) (cause error, ok bool)

func IsUnrecoverable

func IsUnrecoverable(err error) bool

func Sleep

func Sleep(ctx context.Context, delay time.Duration) bool

func Unrecoverable

func Unrecoverable(err error) error

Types

type RetryableContextFunc

type RetryableContextFunc func(ctx context.Context, retryNumber int) (err error)

type RetryableFunc

type RetryableFunc func(retryNumber int) (err error)

type Strategy

type Strategy interface {
	Attempt(ctx context.Context) (attempt bool)
}

func Compose

func Compose(strategies ...Strategy) Strategy

func DefaultStrategy

func DefaultStrategy() Strategy

func ExpStrategy

func ExpStrategy(seed time.Duration) Strategy

func FixedDelay

func FixedDelay(delay time.Duration) Strategy

func FromDelays

func FromDelays(delays ...time.Duration) Strategy

func Function

func Function(retryFunc StrategyFunc) Strategy

func Infinite

func Infinite() Strategy

func MaxAttempts

func MaxAttempts(attempts int) Strategy

func PowDelay

func PowDelay(seed time.Duration, base float64) Strategy

func RandomDelay

func RandomDelay(min time.Duration, max time.Duration) Strategy

type StrategyFunc

type StrategyFunc func(ctx context.Context) (attempt bool)

type UnrecoverableError

type UnrecoverableError struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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