-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
174 lines (141 loc) · 4.5 KB
/
options.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
package dynalock
import (
"encoding/base64"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// WriteOption assign various settings to the write options
type WriteOption func(opts *WriteOptions)
// WriteOptions contains optional request parameters
type WriteOptions struct {
value *dynamodb.AttributeValue
ttl time.Duration
previous *KVPair // Optional, previous value used to assert if the record has been modified before an atomic update
}
// NewWriteOptions create write options, assign defaults then accept overrides
func NewWriteOptions(opts ...WriteOption) *WriteOptions {
// assign a place holder value to detect whether to assign the default TTL
writeOpts := &WriteOptions{
ttl: noTTLSet,
}
for _, opt := range opts {
opt(writeOpts)
}
if writeOpts.ttl == noTTLSet {
writeOpts.ttl = DefaultLockTTL
}
return writeOpts
}
// WriteWithTTL time to live (TTL) to the key which is written
func WriteWithTTL(ttl time.Duration) WriteOption {
return func(opts *WriteOptions) {
opts.ttl = ttl
}
}
// WriteWithNoExpires time to live (TTL) is set not set so it never expires
func WriteWithNoExpires() WriteOption {
return func(opts *WriteOptions) {
opts.ttl = 0
}
}
// WriteWithBytes byte slice to the key which is written
func WriteWithBytes(val []byte) WriteOption {
return func(opts *WriteOptions) {
opts.value = encodePayload(val)
}
}
// WriteWithAttributeValue dynamodb attribute value which is written
func WriteWithAttributeValue(av *dynamodb.AttributeValue) WriteOption {
return func(opts *WriteOptions) {
opts.value = av
}
}
// WriteWithPreviousKV previous KV which will be checked prior to update
func WriteWithPreviousKV(previous *KVPair) WriteOption {
return func(opts *WriteOptions) {
opts.previous = previous
if opts.previous != nil {
opts.ttl = time.Until(time.Unix(opts.previous.Expires, 0)) // update the TTL to the remaining time
}
}
}
// ReadOption assign various settings to the read options
type ReadOption func(opts *ReadOptions)
// ReadOptions contains optional request parameters
type ReadOptions struct {
consistent bool
}
// NewReadOptions create read options, assign defaults then accept overrides
// enable the read consistent flag by default
func NewReadOptions(opts ...ReadOption) *ReadOptions {
readOpts := &ReadOptions{
consistent: true,
}
for _, opt := range opts {
opt(readOpts)
}
return readOpts
}
// ReadConsistentDisable disable consistent reads
func ReadConsistentDisable() ReadOption {
return func(opts *ReadOptions) {
opts.consistent = false
}
}
// LockOption assign various settings to the lock options
type LockOption func(opts *LockOptions)
// LockOptions contains optional request parameters
type LockOptions struct {
value *dynamodb.AttributeValue
ttl time.Duration
renewLock chan struct{}
tryLockPollingEnable bool
renewEnable bool
}
// NewLockOptions create lock options, assign defaults then accept overrides
func NewLockOptions(opts ...LockOption) *LockOptions {
lockOpts := &LockOptions{
ttl: DefaultLockTTL,
renewEnable: true, // defaults to true to retain existing default behaviour
tryLockPollingEnable: true, // defaults to true to retain existing default behaviour
}
for _, opt := range opts {
opt(lockOpts)
}
return lockOpts
}
// LockWithBytes byte slice to the key which is written when the lock is acquired
func LockWithBytes(val []byte) LockOption {
return func(opts *LockOptions) {
opts.value = encodePayload(val)
}
}
// LockWithTTL time to live (TTL) to the key which is written when the lock is acquired
func LockWithTTL(ttl time.Duration) LockOption {
return func(opts *LockOptions) {
opts.ttl = ttl
}
}
// LockWithRenewLock renewal channel to the lock
func LockWithRenewLock(renewLockChan chan struct{}) LockOption {
return func(opts *LockOptions) {
opts.renewLock = renewLockChan
}
}
// LockWithNoRenew renewal of the lock in the background will not be enabled
func LockWithNoRenew() LockOption {
return func(opts *LockOptions) {
opts.renewEnable = false
}
}
// LockWithNoTryPolling lock will not poll to acquire a lock and block the caller, if the lock fails it will return straight away
func LockWithNoTryPolling() LockOption {
return func(opts *LockOptions) {
opts.tryLockPollingEnable = false
}
}
func encodePayload(payload []byte) *dynamodb.AttributeValue {
encodedValue := base64.StdEncoding.EncodeToString(payload)
return &dynamodb.AttributeValue{S: aws.String(encodedValue)}
}