-
Notifications
You must be signed in to change notification settings - Fork 1
/
retry.go
63 lines (55 loc) · 1.17 KB
/
retry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package retry
import (
"context"
"time"
bo "github.com/aperturerobotics/util/backoff"
"github.com/cenkalti/backoff"
"github.com/sirupsen/logrus"
)
// NewBackOff constructs a new backoff with a config.
func NewBackOff(conf *bo.Backoff) backoff.BackOff {
if conf == nil {
conf = &bo.Backoff{}
}
return conf.Construct()
}
// DefaultBackoff returns the default backoff.
func DefaultBackoff() backoff.BackOff {
return NewBackOff(nil)
}
// Retry uses a backoff to re-try a process.
// If the process returns nil or context canceled, it exits.
// If bo is nil, a default one is created.
// Success function will reset the backoff.
func Retry(
ctx context.Context,
le *logrus.Entry,
f func(ctx context.Context, success func()) error,
bo backoff.BackOff,
) error {
if bo == nil {
bo = DefaultBackoff()
}
for {
le.Debug("starting process")
err := f(ctx, bo.Reset)
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if err == nil {
return nil
}
b := bo.NextBackOff()
le.
WithError(err).
WithField("backoff", b.String()).
Warn("process failed, retrying")
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(b):
}
}
}