retry

package
v0.0.0-...-3db8769 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2022 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package retry provides general retry logic and a designated error structure that contains each retrial's error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LastErrorOf

func LastErrorOf(e error) error

LastErrorOf receives an error implementation and, when this is *Errors returned by retrial function, returns the last execution error. This simply returns the given error value when non-*Errors value is given; returns nil when nil is given.

When the last returned error is important to check the state, a developer may check the last error somewhat like below:

err := Retry(5, func() error {
  // Do something
  return nil
})
lastErr := LastErrorOf(err)
if(lastErr != nil) {
  // All trial fails and lastErr represents the last execution error.
  // lastError == nil means the successful execution
}

func Retry

func Retry(trial int, function func() error) error

Retry tries to execute the given function as many times as the maximum trial count. It quits retrying when the function returns no error, which is nil. When all trials fail, Errors is returned to notify such error occurrences.

func WithBackOff

func WithBackOff(trial int, function func() error, meanInterval time.Duration, randFactor float64) error

WithBackOff executes the given function at an interval until the function returns no error or the retrial count reaches the specified threshold. The interval differs every time. The base interval and randomization factor are specified by meanInterval and randFactor.

func WithInterval

func WithInterval(trial int, function func() error, interval time.Duration) error

WithInterval executes the given function at a fixed interval until the function returns no error or the retrial count reaches the specified threshold.

func WithPolicy

func WithPolicy(policy *Policy, function func() error) error

WithPolicy receives a retrial policy and an executable function. The passed function is repeatedly executed until no error is returned or the retrial count exceeds the given configuration value. Unlike other retrial functions, this function is among the most flexible since a developer has maximum freedom on the configuration.

Types

type Errors

type Errors []error

Errors is an alias of a slice of errors that contains ordered errors that occurred during retrials. This satisfies error interface, and a call to Error() returns concatenated message of all belonging errors.

Since this is an alias of []error, each belonging error is accessible in a way such as:

for i, err := range *errs { ... }

func (*Errors) Error

func (e *Errors) Error() string

Error returns the concatenated message of all belonging errors. All err.Err() strings are joined with "\n".

type Policy

type Policy struct {
	Trial      int           `json:"trial" yaml:"trial"`
	Interval   time.Duration `json:"interval" yaml:"interval"`
	RandFactor float64       `json:"random_factor" yaml:"random_factor"`
}

Policy represents a configuration value of the retrial logic.

func NewPolicy

func NewPolicy() *Policy

NewPolicy creates and returns a new retrial policy. Rather than selecting specific retrial functions -- Retry(), WithInterval() and WithBackOff() -- depending on usages, developers are free to pass a configurable retrial policy to WithPolicy().

With the given policy, WithPolicy() will decide how retrials are executed so developers may modify the retrial behavior by changing the policy. This is especially effective when the Policy is mapped from JSON/YAML file in a way such as below:

policy := NewPolicy()
configBytes, _ := ioutil.ReadFile(filename)
json.Unmarshal(configBytes, policy)

To manually set each configurable field, Call WithXxx methods of returning Policy.

policy := NewPolicy().
    WithTrial(3).
    WithInterval(3*time.Second)

func (*Policy) WithInterval

func (p *Policy) WithInterval(d time.Duration) *Policy

WithInterval sets the interval for each retrial. When RandFactor is set, this interval is used as the base interval.

func (*Policy) WithRandFactor

func (p *Policy) WithRandFactor(factor float64) *Policy

WithRandFactor sets randomization factor. Make sure to set Interval with WithInterval() or JSON/YAML deserialization.

func (*Policy) WithTrial

func (p *Policy) WithTrial(cnt int) *Policy

WithTrial sets the maximum trial count.

Jump to

Keyboard shortcuts

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