forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
47 lines (41 loc) · 1.08 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
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package utils
import (
"context"
"time"
"go.uber.org/multierr"
)
// RetryableFunc presents a retryable operation.
type RetryableFunc func() error
// Backoffer implements a backoff policy for retrying operations.
type Backoffer interface {
// NextBackoff returns a duration to wait before retrying again
NextBackoff(err error) time.Duration
// Attempt returns the remain attempt times
Attempt() int
}
// WithRetry retries a given operation with a backoff policy.
//
// Returns nil if `retryableFunc` succeeded at least once. Otherwise, returns a
// multierr containing all errors encountered.
func WithRetry(
ctx context.Context,
retryableFunc RetryableFunc,
backoffer Backoffer,
) error {
var allErrors error
for backoffer.Attempt() > 0 {
err := retryableFunc()
if err != nil {
allErrors = multierr.Append(allErrors, err)
select {
case <-ctx.Done():
return allErrors // nolint:wrapcheck
case <-time.After(backoffer.NextBackoff(err)):
}
} else {
return nil
}
}
return allErrors // nolint:wrapcheck
}