feat: add retry on connection restart

This commit is contained in:
braginini
2021-04-18 15:54:10 +02:00
parent ead16a35c9
commit 801ce0730d
4 changed files with 14 additions and 7 deletions
+32
View File
@@ -0,0 +1,32 @@
package util
import (
"math/rand"
"time"
)
// Retries a given toExec function calling onError on failed attempts
// onError shouldn be a lightweight function and shouldn't be blocking
func Retry(attempts int, sleep time.Duration, toExec func() error, onError func(e error)) error {
if err := toExec(); err != nil {
if s, ok := err.(stop); ok {
return s.error
}
if attempts--; attempts > 0 {
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
onError(err)
time.Sleep(sleep)
return Retry(attempts, 2*sleep, toExec, onError)
}
return err
}
return nil
}
type stop struct {
error
}