retry

package module
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: MIT Imports: 5 Imported by: 0

README

Retry

A simple abstraction retry library to handle the complicated

GoDoc Dependabot Status Build Status

Documentation

Overview

Package retry allows for abstracted retry mechanism to hide the gory details of what is happening underneath.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbortedRetries added in v1.0.0

func AbortedRetries(err error) error

AbortedRetries wraps the message passed and returns an error that be read by the error handler within the retry client.

func ExceededRetries added in v1.0.0

func ExceededRetries(err error) error

ExceededRetries wraps the message passed and returns an error that be read by the error handler within the retry client.

func HasAborted added in v1.0.0

func HasAborted(err error) bool

HasAborted checks the error to validate if the executed function has notified that it should abort now.

func HasExceeded added in v1.0.0

func HasExceeded(err error) bool

HasExceeded checks error to validate if the exectued function has notified that it exceeded the limit.

Types

type Option

type Option func(r *retry) error

Option allows for additional functionality to be added to the Retryer on creation

func WithExponentialBackoff

func WithExponentialBackoff(delay time.Duration, multiplier float64) Option

WithExponentialBackoff will start from a fixed delay and increase the delay amount by increasing it by the multiplier amount

func WithFixedDelay

func WithFixedDelay(delay time.Duration) Option

WithFixedDelay will set the delay experienced after each failed attempted

func WithJitter

func WithJitter(delay time.Duration) Option

WithJitter generates a random sleep interval between [0, delay) to help spread retry attemps over different intervals

type Retryer

type Retryer interface {
	// Do will execute the function until it has reached the permissable limit
	// that is passed. If the function was to return nil, the retry will exit early
	// otherwise, the error is passed to an error handler and the next attempt is
	// started after the post execution function have run.
	// If the attempt limit has been reached, ErrAttemptsExceeded is returned.
	Do(limit int, f func() error) error

	// DoWithContext extendes the Do method by ensuring that any attempts are
	// aborted if the passed context is done.
	DoWithContext(ctx context.Context, limit int, f func() error) error
}

Retryer abstracts the retry functionality of executing a function

func Must

func Must(opts ...Option) Retryer

Must is a convenience function for New to avoid having to handle the error and allow for inline creation. If an error was to be returned from the wrapped New function, it would cause this function to panic instead.

Example
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/MovieStoreGuy/retry"
)

func main() {
	r := retry.Must(
		retry.WithFixedDelay(100*time.Millisecond), // Ensures each failed attempt waits 100ms
		retry.WithJitter(10*time.Millisecond),      // Ensures each failed attempt waits at most 10ms
	)

	_ = r.Do(3, func() error {
		fmt.Print(`tick...`)
		return errors.New(`boom`)
	})

}
Output:

tick...tick...tick...

func New

func New(opts ...Option) (Retryer, error)

New creates a new retry with the configured options provided. An error is returned if any of the options failed to apply

Example
package main

import (
	"fmt"

	"github.com/MovieStoreGuy/retry"
)

func main() {
	r, err := retry.New()
	if err != nil {
		panic(err)
	}
	err = r.Do(4, func() error {
		fmt.Println(`Woohoo`)
		return nil
	})
	if err != nil {
		panic(err)
	}
}
Output:

Woohoo

Directories

Path Synopsis
http

Jump to

Keyboard shortcuts

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