Skip to content

Commit

Permalink
Refactor local lock code
Browse files Browse the repository at this point in the history
  • Loading branch information
pteich committed Jul 6, 2021
1 parent db99563 commit c44577f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (cs *ConsulStorage) prefixKey(key string) string {

// Lock acquires a distributed lock for the given key or blocks until it gets one
func (cs *ConsulStorage) Lock(ctx context.Context, key string) error {
if cs.IsLocked(key) {
if _, isLocked := cs.GetLock(key); isLocked {
return nil
}

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

func (cs *ConsulStorage) IsLocked(key string) bool {
func (cs *ConsulStorage) GetLock(key string) (*consul.Lock, bool) {
cs.muLocks.RLock()
defer cs.muLocks.RUnlock()

// if we already hold the lock, return early
if _, exists := cs.locks[key]; exists {
return true
if lock, exists := cs.locks[key]; exists {
return lock, true
}

return false
return nil, false
}

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

// check if we own it and unlock
lock, exists := cs.locks[key]
lock, exists := cs.GetLock(key)
if !exists {
return errors.Errorf("lock %s not found", cs.prefixKey(key))
}
Expand Down

0 comments on commit c44577f

Please sign in to comment.