retry

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

An interface for and collection of various implementations of retry behavior, designed to be easy to use and composable.

Some examples:

// A very basic example.
strategy := &retry.CountStrategy{Tries: 3}
for done := false; !done && strategy.Next() {
	done = trySomething()
}
// Compose a few strategies together
// Try not more than 3 times, with a 100ms delay between attempts
strategy := &retry.All{
	&retry.CountStrategy{Tries: 3},
	&retry.DelayStrategy{Delay: 100 * time.Millisecond},
}

for done := false; !done && strategy.Next() {
	done = trySomething()
}
// More complex composition:
// Delay of 100ms between tries
// Keep trying for up to 10s
// At least 3 tries
strategy := &retry.All{
	&retry.Any{
		&retry.MaximumTimeStrategy{Duration: 10 * time.Second},
		&retry.CountStrategy{Tries: 3},
	},
	&retry.DelayStrategy{Delay: 100 * time.Millisecond},
}

for done := false; !done && strategy.Next() {
	done = trySomething()
}
// Separate retry logic from a more complex function
func doComplexThings(strategy retry.Strategy)bool{
	for strategy.Next() {
		if success := trySomthingThatMightFail(); success {
			return true
		}
	}
	return false
}

doComplexThings(&retry.CountStrategy{Tries: 3})
doComplexThings(&retry.MaximumTimeStrategy{Duration: 10 * time.Second})

Documentation

Overview

An interface for and collection of various implementations of retry behavior, designed to be easy to use and composable. See README for example uses.

Index

Constants

This section is empty.

Variables

View Source
var (
	TimeFunc  = time.Now
	SleepFunc = time.Sleep
)

Set these to override how now is discovered and how sleeping is done This is mostly useful for testing, but you never know

Functions

func Do

func Do(ctx context.Context, strategy Strategy, action func() bool) bool

Useful helper method. Calls action until it returns true or the retry strategy returns false.

func DoWithReset

func DoWithReset(ctx context.Context, strategy ResettableStrategy, action func() bool) bool

Types

type All

type All []Strategy

A composite strategy. In order for Next or HasNext to succeed, all of the included strategies must succeed

func (All) HasNext

func (s All) HasNext() bool

func (All) Next

func (s All) Next() bool

type AllResettable

type AllResettable []ResettableStrategy

func (AllResettable) HasNext

func (s AllResettable) HasNext() bool

func (AllResettable) Next

func (s AllResettable) Next() bool

func (AllResettable) Reset

func (s AllResettable) Reset()

type AlwaysRetryStrategy

type AlwaysRetryStrategy struct{}

Retry forever. No waiting.

func (*AlwaysRetryStrategy) HasNext

func (s *AlwaysRetryStrategy) HasNext() bool

func (*AlwaysRetryStrategy) Next

func (s *AlwaysRetryStrategy) Next() bool

type Any

type Any []Strategy

A composite strategy. In order for Next or HasNext to succeed, any one of the included strategies must succeed

func (Any) HasNext

func (s Any) HasNext() bool

func (Any) Next

func (s Any) Next() bool

type AnyResettable

type AnyResettable []ResettableStrategy

func (AnyResettable) HasNext

func (s AnyResettable) HasNext() bool

func (AnyResettable) Next

func (s AnyResettable) Next() bool

func (AnyResettable) Reset

func (s AnyResettable) Reset()

type CancelableRetryStrategy

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

Always retry until canceled. Use And to combine this with other strategies to make them cancelable

func (*CancelableRetryStrategy) Cancel

func (s *CancelableRetryStrategy) Cancel()

func (*CancelableRetryStrategy) HasNext

func (s *CancelableRetryStrategy) HasNext() bool

func (*CancelableRetryStrategy) Next

func (s *CancelableRetryStrategy) Next() bool

type CountStrategy

type CountStrategy struct {
	Tries int
	// contains filtered or unexported fields
}

Try up to a fixed number of times

func (*CountStrategy) HasNext

func (s *CountStrategy) HasNext() bool

func (*CountStrategy) Next

func (s *CountStrategy) Next() bool

func (*CountStrategy) Reset

func (s *CountStrategy) Reset()

type DelayStrategy

type DelayStrategy struct {
	Wait time.Duration
	// contains filtered or unexported fields
}

Delay strategy has no limit. It implements a fixed wait time between retries.

func (*DelayStrategy) HasNext

func (s *DelayStrategy) HasNext() bool

func (*DelayStrategy) Next

func (s *DelayStrategy) Next() bool

type ExponentialBackoffStrategy

type ExponentialBackoffStrategy struct {
	InitialDelay time.Duration
	MaxDelay     time.Duration // default: no limit
	// contains filtered or unexported fields
}

Exponential backoff. No iteration limit, but it gets slower every time. Resettable.

func (*ExponentialBackoffStrategy) HasNext

func (s *ExponentialBackoffStrategy) HasNext() bool

func (*ExponentialBackoffStrategy) Next

func (s *ExponentialBackoffStrategy) Next() bool

func (*ExponentialBackoffStrategy) Reset

func (s *ExponentialBackoffStrategy) Reset()

type MaximumTimeStrategy

type MaximumTimeStrategy struct {
	Duration time.Duration
	// contains filtered or unexported fields
}

Maximum time strategy. No iteration limit. Limit on max time. Timer starts automatically on first try. Resettable

func (*MaximumTimeStrategy) HasNext

func (s *MaximumTimeStrategy) HasNext() bool

func (*MaximumTimeStrategy) Next

func (s *MaximumTimeStrategy) Next() bool

func (*MaximumTimeStrategy) Reset

func (s *MaximumTimeStrategy) Reset()

type ResettableStrategy

type ResettableStrategy interface {
	Strategy
	Reset()
}

Retry strategy expanded with reset functionality.

type Strategy

type Strategy interface {
	Next() bool
	HasNext() bool
}

This is the main interface around which this library is built. It defines a very simple interface for abstracting retry logic in your application.

Jump to

Keyboard shortcuts

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