retry

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

The retry package encapsulates the mechanism around retrying operation.

It is a golang implementation for nodejs: https://www.npmjs.com/package/retry.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Operation

type Operation struct {
	Retries    int           // The maximum amount of times to retry the operation
	MinInteval time.Duration // default is 1s
	MaxInteval time.Duration // defaults is equals to MinInteval
	Factor     float64       // The exponential factor to use, default is 1
	Randomize  bool          // Randomizes the timeouts by multiplying with a factor between 0.5 to 1.5
}

Operation defines the options for retry sleepInteval = min(random * minInteval * pow(factor, attempt), maxInteval)

func (*Operation) Attempt

func (o *Operation) Attempt(fn RetryFunc) error

Attampt accepts the function fn that is to be retried and executes it.

Example
package main

import (
	"fmt"
	"time"

	"github.com/subchen/go-stack/retry"
)

func main() {
	operation := &retry.Operation{
		Retries:    3,
		MinInteval: 1 * time.Second,
		Factor:     1.2,
		Randomize:  true,
	}

	err := operation.Attempt(func(attempt int) error {
		fmt.Printf("%v: %d: do something\n", time.Now(), attempt)

		return fmt.Errorf("some error")
	})

	if err != nil {
		panic(err)
	}
}

type RetryFunc

type RetryFunc func(attempt int) error

RetryFunc is func, attampt is 0-based

Jump to

Keyboard shortcuts

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