retry

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

retry

Golang retry library.

About The Project

Retry - small library, that provides api for easy function retry and error handling. 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 err
})

Limit attempts:

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

Delay between retries:

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

Combine few retry strategies:

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

Breaking retry loop directly from function:

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

Using context:

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

Context 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 err
})

Underwater rocks

Arguments order

Be careful:

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

not similar to

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

Trace steps in the first case are:

  1. Function call
  2. Max attempts check
  3. Delay
  4. Function call
  5. Max attempts check
  6. Break loop

Trace steps in the second case are:

  1. Function call
  2. Delay
  3. Max attempts check
  4. Function call
  5. Delay
  6. Max attempts check
  7. Break loop

So, we can see, that second block of code will have unnecessary delay. It is highly recommended to store strategies, that limit attempts, before strategies, that do delays.

Strategy reusing

Some strategies have internal state, so it is highly recommended not to share strategies between several Exec calls.

Incorrect:

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

Correct:

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

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 Sleep

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

func Unrecoverable

func Unrecoverable(err error) error

Types

type AndStrategy

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

func And

func And(strategies ...Strategy) (strategy AndStrategy)

func (AndStrategy) Attempt

func (s AndStrategy) Attempt(ctx context.Context, retryNumber int, err error) (attempt bool)

type DelayedStrategy

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

func Delays

func Delays(delays ...time.Duration) (strategy *DelayedStrategy)

func (*DelayedStrategy) Attempt

func (s *DelayedStrategy) Attempt(ctx context.Context, _ int, _ error) (attempt bool)

type FixedDelayStrategy

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

func FixedDelay

func FixedDelay(delay time.Duration) (strategy FixedDelayStrategy)

func (FixedDelayStrategy) Attempt

func (s FixedDelayStrategy) Attempt(ctx context.Context, _ int, _ error) (attempt bool)

type FuncStrategy

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

func Function

func Function(retryFunc StrategyFunc) (strategy FuncStrategy)

func (FuncStrategy) Attempt

func (s FuncStrategy) Attempt(ctx context.Context, retryNumber int, err error) (attempt bool)

type InfiniteAttemptsStrategy

type InfiniteAttemptsStrategy struct {
}

func Infinite

func Infinite() (strategy *InfiniteAttemptsStrategy)

func (*InfiniteAttemptsStrategy) Attempt

func (s *InfiniteAttemptsStrategy) Attempt(_ context.Context, _ int, _ error) (attempt bool)

type IsStrategy

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

func Is

func Is(err error) (strategy IsStrategy)

func (*IsStrategy) Attempt

func (s *IsStrategy) Attempt(_ context.Context, _ int, err error) (attempt bool)

type LinearDelayStrategy

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

func LinearDelay

func LinearDelay(seed time.Duration, delta time.Duration) (strategy *LinearDelayStrategy)

func (*LinearDelayStrategy) Attempt

func (s *LinearDelayStrategy) Attempt(ctx context.Context, _ int, _ error) (attempt bool)

type MaxRetriesStrategy

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

func MaxAttempts

func MaxAttempts(attempts int) (strategy *MaxRetriesStrategy)

func (*MaxRetriesStrategy) Attempt

func (s *MaxRetriesStrategy) Attempt(_ context.Context, _ int, _ error) (attempt bool)

type NotStrategy

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

func Not

func Not(originStrategy Strategy) (strategy NotStrategy)

func (NotStrategy) Attempt

func (s NotStrategy) Attempt(ctx context.Context, retryNumber int, err error) (attempt bool)

type OrStrategy

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

func Or

func Or(strategies ...Strategy) (strategy OrStrategy)

func (OrStrategy) Attempt

func (s OrStrategy) Attempt(ctx context.Context, retryNumber int, err error) (attempt bool)

type PowDelayStrategy

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

func PowDelay

func PowDelay(seed time.Duration, base float64) (strategy *PowDelayStrategy)

func (*PowDelayStrategy) Attempt

func (s *PowDelayStrategy) Attempt(ctx context.Context, _ int, _ error) (attempt bool)

type RandomDelayStrategy

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

func RandomDelay

func RandomDelay(minDelay time.Duration, maxDelay time.Duration) (strategy RandomDelayStrategy)

func (RandomDelayStrategy) Attempt

func (s RandomDelayStrategy) Attempt(ctx context.Context, _ int, _ error) (attempt bool)

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, retryNumber int, err error) (attempt bool)
}

func Default

func Default() Strategy

func ExpDelay

func ExpDelay(seed time.Duration) (strategy Strategy)

type StrategyFunc

type StrategyFunc func(ctx context.Context, retryNumber int, _ error) (attempt bool)

type TypeStrategy

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

func Type

func Type(err error) (strategy TypeStrategy)

func (*TypeStrategy) Attempt

func (s *TypeStrategy) Attempt(_ context.Context, _ int, err error) (attempt bool)

Jump to

Keyboard shortcuts

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