retry

package module
v0.0.0-...-4f244ca Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2017 License: MIT Imports: 2 Imported by: 27

README

retry

A tiny library for retrying failing operations with Go.

License ReportCard Build Coverage GoDoc

Get

go get -u github.com/LyricTian/retry

Usage

package main

import (
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/LyricTian/retry"
)

func main() {
	var (
		count int
		value string
	)

	err := retry.DoFunc(3, func() error {
		if count > 1 {
			value = "foo"
			return nil
		}
		count++
		return errors.New("not allowed")
	})

	if err != nil {
		log.Fatalln(err.Error())
	}

	fmt.Println(value)
	// Output: foo
}

MIT License

    Copyright (c) 2017 Lyric

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrMaxRetries exceeded retry limit
	ErrMaxRetries = errors.New("exceeded retry limit")
)

Functions

func Do

func Do(retries int, trier Trier, sleeps ...Sleep) error

Do try to execute the interface, specify the number of attempts, and the execution interval

func DoFunc

func DoFunc(retries int, fn Func, sleeps ...Sleep) error

DoFunc try to execute a function, specify the number of attempts, and the execution interval

Example
var (
	count int
	value string
)

err := DoFunc(3, func() error {
	if count > 1 {
		value = "foo"
		return nil
	}
	count++
	return errors.New("not allowed")
}, func(i int) time.Duration {
	return time.Millisecond * time.Duration(i)
})

if err != nil {
	log.Fatalln(err.Error())
}

fmt.Println(value)
Output:

foo

Types

type Func

type Func func() error

Func try to execute the function

type Sleep

type Sleep func(i int) time.Duration

Sleep sleep callback function

type Trier

type Trier interface {
	Try() error
}

Trier try to execute the interface

Jump to

Keyboard shortcuts

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