Codebase list golang-github-matryer-try / 84f679db-eb48-40ed-b61f-ad5d8c022e69/upstream/1+git20161228.7.9ac251b try.go
84f679db-eb48-40ed-b61f-ad5d8c022e69/upstream/1+git20161228.7.9ac251b

Tree @84f679db-eb48-40ed-b61f-ad5d8c022e69/upstream/1+git20161228.7.9ac251b (Download .tar.gz)

try.go @84f679db-eb48-40ed-b61f-ad5d8c022e69/upstream/1+git20161228.7.9ac251braw · history · blame

package try

import "errors"

// MaxRetries is the maximum number of retries before bailing.
var MaxRetries = 10

var errMaxRetriesReached = errors.New("exceeded retry limit")

// Func represents functions that can be retried.
type Func func(attempt int) (retry bool, err error)

// Do keeps trying the function until the second argument
// returns false, or no error is returned.
func Do(fn Func) error {
	var err error
	var cont bool
	attempt := 1
	for {
		cont, err = fn(attempt)
		if !cont || err == nil {
			break
		}
		attempt++
		if attempt > MaxRetries {
			return errMaxRetriesReached
		}
	}
	return err
}

// IsMaxRetries checks whether the error is due to hitting the
// maximum number of retries or not.
func IsMaxRetries(err error) bool {
	return err == errMaxRetriesReached
}