contribs: include try.go to enable DB retries
Include try.go package to enable retrying of DB connections after failure.
This commit is contained in:
parent
97347ec44d
commit
9f40327562
38
contrib/include-package-try/main.go
Normal file
38
contrib/include-package-try/main.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user