Documentation
¶
Overview ¶
Package retry provides general retry logic and a designated error structure that contains each retrial's error.
Index ¶
- func LastErrorOf(e error) error
- func Retry(trial int, function func() error) error
- func WithBackOff(trial int, function func() error, meanInterval time.Duration, ...) error
- func WithInterval(trial int, function func() error, interval time.Duration) error
- func WithPolicy(policy *Policy, function func() error) error
- type Errors
- type Policy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LastErrorOf ¶
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 ¶
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 ¶
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 ¶
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 { ... }
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 ¶
WithInterval sets the interval for each retrial. When RandFactor is set, this interval is used as the base interval.
func (*Policy) WithRandFactor ¶
WithRandFactor sets randomization factor. Make sure to set Interval with WithInterval() or JSON/YAML deserialization.