Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLok committed May 23, 2024
1 parent 550168a commit eb2492b
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 155 deletions.
8 changes: 5 additions & 3 deletions common/lockmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Lock(key string) {

val, _ := lockMap.LoadOrStore(key, &lockData{});
lock := val.(*lockData);
// Unlock the lockManagerMutex before aquiring the lock to avoid a deadlock.
lockManagerMutex.Unlock();

lock.mutex.Lock();
Expand Down Expand Up @@ -75,16 +76,17 @@ func RemoveStaleLocksOnce() {

lockMap.Range(func(key, val any) bool {
lock := val.(*lockData);
// First check: If the conditions aren't met to remove a stale lock, return early.

// First check: If the lock isn't stale or is locked, return early.
if (time.Since(lock.timestamp) < staleDuration || lock.isLocked) {
return true;
}

// Lock the lock manager in case another thread is trying to lock/unlock.
lockManagerMutex.Lock();
defer lockManagerMutex.Unlock();
// Second check: If conditions are met to determine a stale lock, delete it.

// Second check: If the lock is stale and and is able to be locked, delete it.
if (time.Since(lock.timestamp) > staleDuration && lock.mutex.TryLock()) {
lockMap.Delete(key);
}
Expand Down
Loading

0 comments on commit eb2492b

Please sign in to comment.