Skip to content

Commit c94fa7b

Browse files
committed
Switch to try-once Consul lock
Try to fix deadlock from #18
1 parent 677be37 commit c94fa7b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
387387
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
388388
github.com/klauspost/compress v1.13.0/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
389389
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
390+
github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE=
390391
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
391392
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
392393
github.com/klauspost/cpuid/v2 v2.0.7 h1:U89pAFid7wpIWvTFJnMKgU+Sabb7DLEgHI7Xt8apo3Y=

module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (ConsulStorage) CaddyModule() caddy.ModuleInfo {
2525
// Provision is called by Caddy to prepare the module
2626
func (cs *ConsulStorage) Provision(ctx caddy.Context) error {
2727
cs.logger = ctx.Logger(cs).Sugar()
28-
cs.logger.Infof("TLS storage is using Consul at %cs", cs.Address)
28+
cs.logger.Infof("TLS storage is using Consul at %s", cs.Address)
2929

3030
// override default values from ENV
3131
if aesKey := os.Getenv(EnvNameAESKey); aesKey != "" {

storage.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ func (cs *ConsulStorage) prefixKey(key string) string {
5454

5555
// Lock acquires a distributed lock for the given key or blocks until it gets one
5656
func (cs *ConsulStorage) Lock(ctx context.Context, key string) error {
57+
cs.logger.Debugf("trying lock for %s", key)
58+
5759
if _, isLocked := cs.GetLock(key); isLocked {
5860
return nil
5961
}
6062

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+
})
6370
if err != nil {
6471
return errors.Wrapf(err, "could not create lock for %s", cs.prefixKey(key))
6572
}
@@ -142,6 +149,8 @@ func (cs ConsulStorage) Store(key string, value []byte) error {
142149

143150
// Load retrieves the value for a key from Consul KV
144151
func (cs ConsulStorage) Load(key string) ([]byte, error) {
152+
cs.logger.Debugf("loading data from Consul for %s", key)
153+
145154
kv, _, err := cs.ConsulClient.KV().Get(cs.prefixKey(key), &consul.QueryOptions{RequireConsistent: true})
146155
if err != nil {
147156
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) {
159168

160169
// Delete a key from Consul KV
161170
func (cs ConsulStorage) Delete(key string) error {
171+
cs.logger.Debugf("deleting key %s from Consul", key)
172+
162173
// first obtain existing keypair
163174
kv, _, err := cs.ConsulClient.KV().Get(cs.prefixKey(key), &consul.QueryOptions{RequireConsistent: true})
164175
if err != nil {

0 commit comments

Comments
 (0)