generated from Thumbscrew/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddbretry.go
115 lines (102 loc) · 2.85 KB
/
ddbretry.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package ddbretry
import (
"context"
"errors"
"time"
ddb "github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type DynamoDBClient interface {
GetItem(context.Context, *ddb.GetItemInput, ...func(*ddb.Options)) (*ddb.GetItemOutput, error)
DeleteItem(context.Context, *ddb.DeleteItemInput, ...func(*ddb.Options)) (*ddb.DeleteItemOutput, error)
PutItem(context.Context, *ddb.PutItemInput, ...func(*ddb.Options)) (*ddb.PutItemOutput, error)
}
type RetryDynamoDBClient struct {
DynamoDBClient
Retries int
BackOffTime time.Duration
}
func NewRetryDynamoDBClient(client DynamoDBClient, retries int, backOff time.Duration) *RetryDynamoDBClient {
return &RetryDynamoDBClient{
DynamoDBClient: client,
Retries: retries,
BackOffTime: backOff,
}
}
func (c *RetryDynamoDBClient) GetItem(ctx context.Context, input *ddb.GetItemInput, o ...func(*ddb.Options)) (output *ddb.GetItemOutput, err error) {
retries := c.Retries
infinite := retries == -1
for retries >= 0 || infinite {
output, err = c.DynamoDBClient.GetItem(ctx, input, o...)
if err != nil {
if IsProvisionedThroughputExceededException(err) {
if retries > 0 {
retries--
time.Sleep(c.BackOffTime)
} else if infinite {
time.Sleep(c.BackOffTime)
} else {
return
}
} else {
return
}
} else {
return
}
}
return nil, NewInvalidRetryError(retries)
}
func (c *RetryDynamoDBClient) DeleteItem(ctx context.Context, input *ddb.DeleteItemInput, o ...func(*ddb.Options)) (output *ddb.DeleteItemOutput, err error) {
retries := c.Retries
infinite := retries == -1
for retries >= 0 || infinite {
output, err = c.DynamoDBClient.DeleteItem(ctx, input, o...)
if err != nil {
if IsProvisionedThroughputExceededException(err) {
if retries > 0 {
retries--
time.Sleep(c.BackOffTime)
} else if infinite {
time.Sleep(c.BackOffTime)
} else {
return
}
} else {
return
}
} else {
return
}
}
return nil, NewInvalidRetryError(retries)
}
func (c *RetryDynamoDBClient) PutItem(ctx context.Context, input *ddb.PutItemInput, o ...func(*ddb.Options)) (output *ddb.PutItemOutput, err error) {
retries := c.Retries
infinite := retries == -1
for retries >= 0 || infinite {
output, err = c.DynamoDBClient.PutItem(ctx, input, o...)
if err != nil {
if IsProvisionedThroughputExceededException(err) {
if retries > 0 {
retries--
time.Sleep(c.BackOffTime)
} else if infinite {
time.Sleep(c.BackOffTime)
} else {
return
}
} else {
return
}
} else {
return
}
}
return nil, NewInvalidRetryError(retries)
}
func IsProvisionedThroughputExceededException(err error) bool {
var provisionedThroughputExceededException *types.ProvisionedThroughputExceededException
ok := errors.As(err, &provisionedThroughputExceededException)
return ok
}