retry

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2018 License: BSD-3-Clause Imports: 2 Imported by: 0

README

ReTry

circleci codecov

Percolate's Go retry package

Description

ReTry is a simple Go package for implementing retry logic. It's partially based on the Python package, retry.

Installation

go get github.com/percolate/retry

Usage

It's easy! Configure an instance of Re to your liking, and pass its Try method a Func of your choosing.

Example

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "time"

    "github.com/percolate/retry"
)

func main() {

    url := "http://example.com"
    delay := time.Duration(10*time.Millisecond)

    var body []byte
    err := retry.Re{Max: 3, Delay: delay}.Try(func() error {
        resp, err := http.Get(url)
        if err != nil {
            return err
        }

        defer resp.Body.Close()
        body, err = ioutil.ReadAll(resp.Body)
        if err != nil {
            return err
        }

        return nil
    })

    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(body)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func func() error

Func is a function type that takes no arguments and returns an error. This is meant to wrap the functionality that should be retried.

type Re

type Re struct {
	// Max specifies the maximum number of attempts. By default, there is no
	// maximum (inifinite retries).
	Max uint

	// Delay specifies the initial Delay between attempts. By default, the
	// initial Delay is set to 0.
	Delay time.Duration

	// MaxDelay specifies a maximum value for the Delay. By default, no
	// maximum is applied.
	MaxDelay time.Duration

	// Backoff specifies a multiplier to be applied to the Delay between
	// attempts. The default Backoff is 1.
	Backoff float64

	// Jitter specifies a fixed number of seconds added to the Delay between
	// attempts. By default, no Jitter is added.
	Jitter time.Duration

	// RetryableErrors specifies an array of merry.Error's that should be caught by the
	// retry. If not specified, all RetryableErrors are caught.
	RetryableErrors []merry.Error
}

Re configures the settings to be used for retries

func (Re) Try

func (r Re) Try(f Func) merry.Error

Try executes the provided function using the Re fields

Jump to

Keyboard shortcuts

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