@@ -54,12 +54,19 @@ func (cs *ConsulStorage) prefixKey(key string) string {
54
54
55
55
// Lock acquires a distributed lock for the given key or blocks until it gets one
56
56
func (cs * ConsulStorage ) Lock (ctx context.Context , key string ) error {
57
+ cs .logger .Debugf ("trying lock for %s" , key )
58
+
57
59
if _ , isLocked := cs .GetLock (key ); isLocked {
58
60
return nil
59
61
}
60
62
61
- // prepare the lock
62
- lock , err := cs .ConsulClient .LockKey (cs .prefixKey (key ))
63
+ // prepare the distributed lock
64
+ cs .logger .Debugf ("creating Consul lock for %s" , key )
65
+ lock , err := cs .ConsulClient .LockOpts (& consul.LockOptions {
66
+ Key : cs .prefixKey (key ),
67
+ LockWaitTime : time .Duration (cs .Timeout ) * time .Second ,
68
+ LockTryOnce : true ,
69
+ })
63
70
if err != nil {
64
71
return errors .Wrapf (err , "could not create lock for %s" , cs .prefixKey (key ))
65
72
}
@@ -142,6 +149,8 @@ func (cs ConsulStorage) Store(key string, value []byte) error {
142
149
143
150
// Load retrieves the value for a key from Consul KV
144
151
func (cs ConsulStorage ) Load (key string ) ([]byte , error ) {
152
+ cs .logger .Debugf ("loading data from Consul for %s" , key )
153
+
145
154
kv , _ , err := cs .ConsulClient .KV ().Get (cs .prefixKey (key ), & consul.QueryOptions {RequireConsistent : true })
146
155
if err != nil {
147
156
return nil , errors .Wrapf (err , "unable to obtain data for %s" , cs .prefixKey (key ))
@@ -159,6 +168,8 @@ func (cs ConsulStorage) Load(key string) ([]byte, error) {
159
168
160
169
// Delete a key from Consul KV
161
170
func (cs ConsulStorage ) Delete (key string ) error {
171
+ cs .logger .Debugf ("deleting key %s from Consul" , key )
172
+
162
173
// first obtain existing keypair
163
174
kv , _ , err := cs .ConsulClient .KV ().Get (cs .prefixKey (key ), & consul.QueryOptions {RequireConsistent : true })
164
175
if err != nil {
0 commit comments