-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.go
188 lines (169 loc) · 4.47 KB
/
backend.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"errors"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/boltdb/bolt"
"github.com/matryer/try"
)
type backend interface {
Init() error
Store(*pipeline) error
Get(uuid []byte) ([]byte, error)
}
type backendConfig struct {
Type string `json:"type"`
BoltDBConfig boltDBConfig `json:"boldDBConfig,omitempty"`
DynamoDBConfig dynamoDBConfig `json:"dynamoDBConfig,omitempty"`
}
func (bc backendConfig) Create() (backend, error) {
switch bc.Type {
case "boltdb":
return &boltDBBackend{
BucketName: bc.BoltDBConfig.BucketName,
DatabaseName: bc.BoltDBConfig.DatabaseName,
}, nil
case "dynamodb":
session, err := session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: aws.Config{Region: &bc.DynamoDBConfig.Region},
},
)
if err != nil {
return nil, err
}
if endpoint := os.Getenv("DYNAMODB_ENDPOINT"); endpoint != "" {
session.Config.Endpoint = aws.String(endpoint)
}
return &dynamoDBBackend{
svc: dynamodb.New(session),
TableName: bc.DynamoDBConfig.TableName,
}, nil
}
return nil, errors.New("Invalid backend type " + bc.Type)
}
// BoltDB
type boltDBConfig struct {
BucketName string `json:"bucketName"`
DatabaseName string `json:"databaseName"`
}
type boltDBBackend struct {
db *bolt.DB
BucketName string
DatabaseName string
}
func (bb *boltDBBackend) Init() error {
var err error
bb.db, err = startBoltDB(bb.DatabaseName, bb.BucketName)
return err
}
func (bb *boltDBBackend) Store(p *pipeline) error {
key, err := (*p).ID.MarshalText()
if err != nil {
return err
}
value := (*p).Config
return bb.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bb.BucketName))
if b == nil {
return errors.New("Bucket does not exist")
}
return b.Put(key, value)
})
}
func (bb *boltDBBackend) Get(uuid []byte) ([]byte, error) {
var value []byte
err := bb.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bb.BucketName))
value = b.Get(uuid)
return nil
})
return value, err
}
// DynamoDB
type dynamoDBConfig struct {
Region string `json:"region"`
TableName string `json:"tableName"`
}
type dynamoDBBackend struct {
svc dynamodbiface.DynamoDBAPI
TableName string
Retries int
}
func (ddb *dynamoDBBackend) Init() error {
session, err := session.NewSessionWithOptions(
session.Options{
SharedConfigState: session.SharedConfigEnable,
},
)
if err != nil {
return err
}
if endpoint := os.Getenv("DYNAMODB_ENDPOINT"); endpoint != "" {
session.Config.Endpoint = aws.String(endpoint)
}
ddb.svc = dynamodb.New(session)
return nil
}
func (ddb *dynamoDBBackend) Store(p *pipeline) error {
key, err := (*p).ID.MarshalText()
if err != nil {
return err
}
value := (*p).Config
dynamoValue := &dynamodb.PutItemInput{
TableName: aws.String(ddb.TableName),
Item: map[string]*dynamodb.AttributeValue{
"UUID": {
B: key,
},
"Config": {
B: value,
},
},
}
return try.Do(func(attempt int) (bool, error) {
_, err := ddb.svc.PutItem(dynamoValue)
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeProvisionedThroughputExceededException ||
awsErr.Code() == dynamodb.ErrCodeInternalServerError &&
attempt < ddb.Retries {
// Backoff time as recommended by https://docs.aws.amazon.com/general/latest/gr/api-retries.html
time.Sleep(time.Duration(2^attempt*100) * time.Millisecond)
return true, err
}
}
return false, err
})
}
func (ddb *dynamoDBBackend) Get(uuid []byte) ([]byte, error) {
var item *dynamodb.GetItemOutput
err := try.Do(func(attempt int) (bool, error) {
var err error
item, err = ddb.svc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(ddb.TableName),
Key: map[string]*dynamodb.AttributeValue{
"UUID": {
B: uuid,
},
},
})
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeProvisionedThroughputExceededException ||
awsErr.Code() == dynamodb.ErrCodeInternalServerError &&
attempt < ddb.Retries {
// Backoff time as recommended by https://docs.aws.amazon.com/general/latest/gr/api-retries.html
time.Sleep(time.Duration(2^attempt*100) * time.Millisecond)
return true, err
}
}
return false, err
})
return item.Item["Config"].B, err
}