Skip to content

Commit c44577f

Browse files
committed
Refactor local lock code
1 parent db99563 commit c44577f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

storage.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ 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-
if cs.IsLocked(key) {
57+
if _, isLocked := cs.GetLock(key); isLocked {
5858
return nil
5959
}
6060

@@ -84,16 +84,16 @@ func (cs *ConsulStorage) Lock(ctx context.Context, key string) error {
8484
return nil
8585
}
8686

87-
func (cs *ConsulStorage) IsLocked(key string) bool {
87+
func (cs *ConsulStorage) GetLock(key string) (*consul.Lock, bool) {
8888
cs.muLocks.RLock()
8989
defer cs.muLocks.RUnlock()
9090

9191
// if we already hold the lock, return early
92-
if _, exists := cs.locks[key]; exists {
93-
return true
92+
if lock, exists := cs.locks[key]; exists {
93+
return lock, true
9494
}
9595

96-
return false
96+
return nil, false
9797
}
9898

9999
// Unlock releases a specific lock
@@ -102,7 +102,7 @@ func (cs *ConsulStorage) Unlock(key string) error {
102102
defer cs.muLocks.Unlock()
103103

104104
// check if we own it and unlock
105-
lock, exists := cs.locks[key]
105+
lock, exists := cs.GetLock(key)
106106
if !exists {
107107
return errors.Errorf("lock %s not found", cs.prefixKey(key))
108108
}

0 commit comments

Comments
 (0)