retry

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package retry implements a retry mechanism with backoff.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(p Policy, fn func() error) error

Run executes the function while the Policy allows. until it returns nil or Stop.

Example
package main

import (
	"time"

	"github.com/hamba/pkg/retry"
)

func main() {
	pol := retry.ExponentialPolicy(3, time.Millisecond)

	err := retry.Run(pol, func() error {
		// Do work

		return nil
	})
	if err != nil {
		// Handle the error
	}
}

func Stop

func Stop(err error) error

Stop wraps an error and stops retrying.

Example
package main

import (
	"errors"
	"time"

	"github.com/hamba/pkg/retry"
)

func main() {
	pol := retry.ExponentialPolicy(3, time.Millisecond)

	err := retry.Run(pol, func() error {
		// Do work that results in error

		return retry.Stop(errors.New("test error"))
	})
	if err != nil {
		// Handle the error
	}
}

Types

type Policy

type Policy interface {
	Next() (time.Duration, bool)
}

Policy represents a retry strategy.

func ExponentialPolicy

func ExponentialPolicy(attempts int, sleep time.Duration) Policy

ExponentialPolicy retires with an exponential growth in sleep.

Jump to

Keyboard shortcuts

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