diff --git a/contrib/include-package-try/main.go b/contrib/include-package-try/main.go new file mode 100644 index 00000000..c0872115 --- /dev/null +++ b/contrib/include-package-try/main.go @@ -0,0 +1,38 @@ +// Sourced from https://github.com/matryer/try + +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 +}