retry

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package retry provides policy for repeating function call to handle transient errors.

Example
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/Prastiwar/Go-flow/policy/retry"
)

func main() {
	var errPersistentError = errors.New("persistent-error")

	p := retry.NewPolicy(
		retry.WithCount(2),
		retry.WithWaitTimes(time.Second, 2*time.Second),
	)

	startedTime := time.Now()
	err := p.Execute(func() error {
		fmt.Println("executed after: " + time.Since(startedTime).Truncate(time.Second).String())
		startedTime = time.Now()

		return errPersistentError
	})

	fmt.Println(err == errPersistentError)

}
Output:
executed after: 0s
executed after: 1s
executed after: 2s
true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPolicy

func NewPolicy(opts ...Option) *policy

NewPolicy returns a new retry policy with configured options.

Types

type CancelPredicate

type CancelPredicate func(attempt int, err error) bool

CancelPredicate controls retrying continuation - return true to stop retrying and false to continue.

type Option

type Option func(*policy)

func WithCancelPredicate

func WithCancelPredicate(handler CancelPredicate) Option

WithCancelPredicate configures CancelPredicate to control retry continuation.

func WithCount

func WithCount(count int) Option

WithCount configures how many times should retry be executed. If set to 0 function will be executed one time and no retry will occur.

func WithWaitTimes

func WithWaitTimes(waitTimes ...time.Duration) Option

WithWaitTimes configures Waiter that works as wait times enumerator for specified waitTimes. If there is difference between retry count and wait times - it will return the edge index on exceeded attempt.

func WithWaiter

func WithWaiter(waiter Waiter) Option

WithWaiter configures Waiter function to provide program wait time before retry execution.

type Policy

type Policy interface {
	Execute(fn func() error) error
}

Policy is implemented by any value that has a Execute method. The implementation controls how to retry function and which features like retry count and cancel control are included.

type Waiter

type Waiter func(attempt int, err error) time.Duration

Waiter is a function called before next retry attempt will occur. Return time.Duration defining how much time program should wait before next retry execution.

Jump to

Keyboard shortcuts

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