-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
132 lines (104 loc) · 2.88 KB
/
client.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ddbrew
import (
"context"
"encoding/json"
"errors"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
type DDBClient struct {
*dynamodb.Client
}
var DdbClient *DDBClient
type DDBClientOption struct {
Local string
}
func checkAndFixURLSchema(endpoint string) string {
if strings.HasPrefix(endpoint, "https://") || strings.HasPrefix(endpoint, "http://") {
return endpoint
}
return "http://" + endpoint
}
func InitClient(opt *DDBClientOption) error {
var cfg aws.Config
var err error
endpoint := checkAndFixURLSchema(opt.Local)
if opt.Local != "" {
cfg, err = config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolver(aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
return aws.Endpoint{URL: endpoint}, nil
})))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
} else {
cfg, err = config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
}
DdbClient = &DDBClient{
Client: dynamodb.NewFromConfig(cfg),
}
return nil
}
type BatchWriteOutput struct {
SuccessCount int
UnprocessedRecord []string
}
func (d *DDBClient) BatchWrite(ctx context.Context, req BatchRequest) (output BatchWriteOutput, err error) {
res, err := d.BatchWriteItem(ctx, req.BatchWriteItemInput())
if err != nil {
return output, err
}
unprocessedNum := len(res.UnprocessedItems[req.TableName])
var reTryResult BatchWriteOutput
if unprocessedNum > 0 {
if req.Retry > 0 {
time.Sleep(1 * time.Second)
retryReq := BatchRequest{
TableName: req.TableName,
WriteRequests: res.UnprocessedItems[req.TableName],
Retry: req.Retry - 1,
}
reTryResult, err = d.BatchWrite(ctx, retryReq)
if err != nil {
return output, err
}
} else if req.Retry == 0 {
for _, item := range res.UnprocessedItems[req.TableName] {
parsedJl := map[string]interface{}{}
if item.PutRequest != nil {
err = attributevalue.UnmarshalMap(item.PutRequest.Item, &parsedJl)
if err != nil {
continue
}
}
if item.DeleteRequest != nil {
err = attributevalue.UnmarshalMap(item.DeleteRequest.Key, &parsedJl)
if err != nil {
continue
}
}
jsonByte, err := json.Marshal(parsedJl)
if err != nil {
return output, err
}
output.UnprocessedRecord = append(output.UnprocessedRecord, string(jsonByte))
}
output.SuccessCount = req.Number() - len(output.UnprocessedRecord)
return output, err
} else {
return output, errors.New("undefined retry value")
}
}
output.SuccessCount = req.Number() - unprocessedNum + reTryResult.SuccessCount
output.UnprocessedRecord = reTryResult.UnprocessedRecord
return output, nil
}