forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
63 lines (54 loc) · 1.43 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 dynamo
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cenkalti/backoff"
"golang.org/x/net/context"
)
// 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() (aws.Context, context.CancelFunc) {
if RetryTimeout == 0 {
return aws.BackgroundContext(), (func() {})
}
return context.WithDeadline(aws.BackgroundContext(), time.Now().Add(RetryTimeout))
}
func retry(ctx aws.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 = aws.SleepWithContext(ctx, next); err != nil {
return err
}
}
}
func canRetry(err error) bool {
if ae, ok := err.(awserr.RequestFailure); ok {
switch ae.StatusCode() {
case 500, 503:
return true
case 400:
switch ae.Code() {
case "ProvisionedThroughputExceededException",
"ThrottlingException":
return true
}
}
}
return false
}