/
dynamodb_lock.go
119 lines (102 loc) · 2.91 KB
/
dynamodb_lock.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
package lock
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// DynamodbLock is to lock job and check duplicated message
type DynamodbLock struct {
TableName string
dynamodb *dynamodb.DynamoDB
}
// Lock put a record into dynamodb with lock_id
func (dl DynamodbLock) Lock(lockID, eventID string) error {
// DynamoDB's expression attribute and placeholders name or values:
// http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ExpressionPlaceholders.html
//
// Use the '#' character to dereference an attribute name.
// Tokens that begin with the ':' character are expression attribute values.
//
// ConditionExpression:
// http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html
param := &dynamodb.UpdateItemInput{
TableName: aws.String(dl.TableName),
Key: map[string]*dynamodb.AttributeValue{
"Id": {
S: aws.String(lockID),
},
"Type": {
S: aws.String("lock"),
},
},
ExpressionAttributeNames: map[string]*string{
"#eventid": aws.String("EventId"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":EventId": {
S: aws.String(eventID),
},
},
ConditionExpression: aws.String("attribute_not_exists(EventId)"),
UpdateExpression: aws.String("set #eventid = :EventId"),
ReturnConsumedCapacity: aws.String("NONE"),
ReturnItemCollectionMetrics: aws.String("NONE"),
ReturnValues: aws.String("NONE"),
}
_, err := dl.dynamodb.UpdateItem(param)
if err == nil {
return nil
}
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ConditionalCheckFailedException" {
return fmt.Errorf("Already '%s' is locked, error_code:%s, reason:%s",
lockID,
awsErr.Code(),
awsErr.Message(),
)
}
}
return err
}
// Unlock delete record from dynamodb with lock_id
func (dl DynamodbLock) Unlock(lockID string) error {
params := &dynamodb.DeleteItemInput{
TableName: aws.String(dl.TableName),
Key: map[string]*dynamodb.AttributeValue{
"Id": {
S: aws.String(lockID),
},
"Type": {
S: aws.String("lock"),
},
},
ReturnConsumedCapacity: aws.String("NONE"),
ReturnItemCollectionMetrics: aws.String("NONE"),
ReturnValues: aws.String("NONE"),
}
_, err := dl.dynamodb.DeleteItem(params)
return err
}
// NewDynamodbLock build DynamodbLock
func NewDynamodbLock(profile, region, table string) Locker {
var conf *aws.Config
if profile != "" {
conf = &aws.Config{
Region: aws.String(region),
Credentials: credentials.NewSharedCredentials("", profile),
}
} else {
conf = &aws.Config{
Region: aws.String(region),
}
}
// configure Dynamodb
ddb := dynamodb.New(session.New(), conf)
return DynamodbLock{
TableName: table,
dynamodb: ddb,
}
}