forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathretry.go
54 lines (44 loc) · 1.3 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
package helixddb
import (
"context"
"errors"
"time"
smithytime "github.com/aws/smithy-go/time"
"github.com/cenkalti/backoff/v4"
)
// RetryTimeout defines the maximum amount of time that requests will
// attempt to automatically retry for. In other words, this is the maximum
// amount of time that dynamo operations will block.
// RetryTimeout is only considered by methods that do not take a context.
// Higher values are better when using tables with lower throughput.
var RetryTimeout = 1 * time.Minute
func defaultContext() (context.Context, context.CancelFunc) {
if RetryTimeout == 0 {
return context.Background(), (func() {})
}
return context.WithDeadline(context.Background(), time.Now().Add(RetryTimeout))
}
func retry(ctx context.Context, f func() error) error {
var err error
var next time.Duration
b := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
for {
if err = f(); err == nil {
return nil
}
if !canRetry(err) {
return err
}
if next = b.NextBackOff(); next == backoff.Stop {
return err
}
if err = smithytime.SleepWithContext(ctx, next); err != nil {
return err
}
}
}
// errRetry is a sentinel error to retry, should never be returned to user
var errRetry = errors.New("dynamo: retry")
func canRetry(err error) bool {
return errors.Is(err, errRetry)
}