retry

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2021 License: MIT Imports: 5 Imported by: 1

README

Retry

PkgGoDev Go version GoReportCard example Test Status GolangCI Lint License

Description

Yet another retrier \o/

Installation

go get -u github.com/vthiery/retry

Usage

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/vthiery/retry"
)

var nonRetryableError = errors.New("a non-retryable error")

func main() {
	// Define the retry strategy, with 10 attempts and an exponential backoff
	retry := retry.New(
		retry.WithMaxAttempts(10),
		retry.WithBackoff(
			retry.NewExponentialBackoff(
				100*time.Millisecond, // minWait
				1*time.Second,        // maxWait
				2*time.Millisecond,   // maxJitter
			),
		),
		retry.WithPolicy(
			func(err error) bool {
				return !errors.Is(err, nonRetryableError)
			},
		),
	)

	// A cancellable context can be used to stop earlier
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Define the function that can be retried
	fn := func(ctx context.Context) error {
		fmt.Println("doing something...")
		return errors.New("actually, can't do it 🤦")
	}

	// Call the `retry.Do` to attempt to perform `fn`
	if err := retry.Do(ctx, fn); err != nil {
		fmt.Printf("failed to perform `fn`: %v\n", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// Next returns the duration to wait before performing the next attempt.
	Next(attempt int) time.Duration
}

Backoff describes the backoff interface.

func NewConstantBackoff

func NewConstantBackoff(wait, maxJitter time.Duration) Backoff

NewConstantBackoff returns an instance of ConstantBackoff.

func NewExponentialBackoff

func NewExponentialBackoff(minWait, maxWait, maxJitter time.Duration) Backoff

NewExponentialBackoff returns an instance of ExponentialBackoff.

type NoAttemptsAllowedError

type NoAttemptsAllowedError struct {
	MaxAttempts int
}

NoAttemptsAllowedError is used to signal that no attempts are allowed.

func (*NoAttemptsAllowedError) Error

func (e *NoAttemptsAllowedError) Error() string

Error returns the error message.

type Option

type Option func(r *Retry)

Option is an option to add to the Retry.

func WithBackoff

func WithBackoff(backoff Backoff) Option

WithBackoff sets a backoff strategy.

func WithMaxAttempts

func WithMaxAttempts(maxAttempts int) Option

WithMaxAttempts sets a the maximum number of attempts to perform.

func WithPolicy

func WithPolicy(policy Policy) Option

WithPolicy sets the retry policy.

type Policy

type Policy func(err error) bool

Policy describes the retry policy based on the error. It must return true for retryable errors and false otherwise.

type Retry

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

Retry allows to retry a function until the amount of attempts is exhausted, using a backoff mechanism to wait between successive attempts. By default, there retrier will:

  • attempts ad infinitum
  • not observe wait between successive attempts
  • retry on all errors

func New

func New(opts ...Option) *Retry

New creates an instance of Retry. By defaults, there is no retry limit and no backoff.

func (*Retry) Do

func (r *Retry) Do(ctx context.Context, fn func(context.Context) error) error

Do attempts to execute the function `fn` until the amount of attempts is exhausted and wait between attempts according to the backoff strategy set on Retry.

Jump to

Keyboard shortcuts

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