/
retry.go
57 lines (46 loc) · 1.23 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
package plugin
import (
"context"
"errors"
"fmt"
"time"
)
// retryFunc is the function signature for a function which is retryable. The
// stop bool indicates whether or not the retry should be halted indicating a
// terminal error. The error return can accompany either a true or false stop
// return to provide context when needed.
type retryFunc func(ctx context.Context) (stop bool, err error)
// retry will retry the passed function f until any of the following conditions
// are met:
// - the function returns stop=true and err=nil
// - the retryAttempts limit is reached
// - the context is cancelled
func retry(ctx context.Context, retryInterval time.Duration, retryAttempts int, f retryFunc) error {
var (
retryCount int
lastErr error
)
for {
if ctx.Err() != nil {
if lastErr != nil {
return fmt.Errorf("retry failed with %v; last error: %v", ctx.Err(), lastErr)
}
return ctx.Err()
}
stop, err := f(ctx)
if stop {
return err
}
if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
lastErr = err
}
if err == nil {
return nil
}
retryCount++
if retryCount == retryAttempts {
return errors.New("reached retry limit")
}
time.Sleep(retryInterval)
}
}