-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo.go
103 lines (91 loc) · 3.33 KB
/
dynamo.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
package main
import (
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/hashicorp/go-multierror"
"github.com/wolfeidau/dynalock"
)
type dynamoUriLocker struct {
dynalock dynalock.Store
name string
}
// NewDynamoURILocker initializes a dynamoUriLocker
func NewDynamoURILocker(table string, partition string, name string) (*dynamoUriLocker, error) {
sess, err := session.NewSession()
if err != nil {
return nil, fmt.Errorf("failed to create AWS session: %+v", err)
}
var d dynalock.Store
customEndpoint := os.Getenv("AWS_DYNAMODB_ENDPOINT_URL")
if customEndpoint != "" {
d = dynalock.New(dynamodb.New(sess, &aws.Config{
Endpoint: aws.String(customEndpoint),
Region: aws.String(os.Getenv("AWS_DEFAULT_REGION")),
}), table, partition)
} else {
d = dynalock.New(dynamodb.New(sess, &aws.Config{
Region: aws.String(os.Getenv("AWS_DEFAULT_REGION")),
}), table, partition)
}
ll := &dynamoUriLocker{
dynalock: d,
name: name,
}
return ll, nil
}
func (ll *dynamoUriLocker) Lock(uri string) (bool, string, error) {
log.Printf("Attempting to lock %s with value of %s ...\n", ll.name, uri)
var resultErr *multierror.Error
success, value, firstPutErr := ll.dynalock.AtomicPut(ll.name, dynalock.WriteWithNoExpires(), dynalock.WriteWithBytes([]byte(uri)))
if firstPutErr != nil {
resultErr = multierror.Append(resultErr, firstPutErr)
log.Printf("Couldn't obtain lock outright, trying figure out what the current value is. %+v\n", resultErr.ErrorOrNil())
value, getErr := ll.dynalock.Get(ll.name)
if getErr != nil {
resultErr = multierror.Append(resultErr, getErr)
log.Printf("Error reading current lock value too. %+v\n", resultErr.ErrorOrNil())
return false, "", resultErr.ErrorOrNil()
}
if string(value.BytesValue()) == uri {
success, value, putErr := ll.dynalock.AtomicPut(ll.name, dynalock.WriteWithNoExpires(), dynalock.WriteWithBytes([]byte(uri)), dynalock.WriteWithPreviousKV(value))
if putErr == nil {
log.Printf("Lock confirmed: %+v, %+v, %+v", success, value, resultErr.ErrorOrNil())
return false, uri, nil
}
resultErr = multierror.Append(resultErr, putErr)
log.Printf("Error confirming lock: %+v, %+v, %+v", success, value, resultErr.ErrorOrNil())
return false, "", resultErr.ErrorOrNil()
}
log.Printf("Lock value mismatch found. %+v\n", resultErr.ErrorOrNil())
return false, string(value.BytesValue()), nil
}
log.Printf("Lock obtained: %+v, %+v, %+v", success, value, resultErr.ErrorOrNil())
return success, uri, resultErr.ErrorOrNil()
}
func (ll *dynamoUriLocker) Unlock(uri string) (string, error) {
log.Printf("Attempting to unlock %s with value of %s ...\n", ll.name, uri)
value, getErr := ll.dynalock.Get(ll.name)
if getErr != nil {
return "", getErr
}
currentLockHolder := string(value.BytesValue())
if currentLockHolder != uri {
return currentLockHolder, fmt.Errorf("Couldn't unlock with provided value of %s, lock currently held by %s", uri, currentLockHolder)
}
_, err := ll.dynalock.AtomicDelete(ll.name, value)
return "", err
}
func (ll *dynamoUriLocker) Read() (string, error) {
value, getErr := ll.dynalock.Get(ll.name)
if getErr != nil {
return "", getErr
}
return string(value.BytesValue()), nil
}
func (ll *dynamoUriLocker) Provider() string {
return "dynamo"
}