Skip to content

Commit

Permalink
Update locking
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Sep 25, 2023
1 parent d2a6be0 commit 967b650
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public LoginManager(Client client,
var cachedValue = _cachedUserInfo.Get();
if (cachedValue is not null) return cachedValue;

using var waiter = _verifySemaphore.CustomWait(timeout: TimeSpan.FromSeconds(30), cancellationToken);
if (!waiter.HasEntered) return null;
using var waiter = _verifySemaphore.CustomWait(cancellationToken);
cachedValue = _cachedUserInfo.Get();
if (cachedValue is not null) return cachedValue;

var isAuthenticated = await _msgFactory.IsAuthenticated();
if (!isAuthenticated) return null;
Expand Down
11 changes: 11 additions & 0 deletions src/NexusMods.Common/SemaphoreSlimWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ public static SemaphoreSlimWaiter CustomWait(
var entered = semaphoreSlim.Wait(timeout, cancellationToken);
return new SemaphoreSlimWaiter(semaphoreSlim, entered);
}

/// <summary>
/// Custom wait infinitely using <see cref="SemaphoreSlimWaiter"/>.
/// </summary>
public static SemaphoreSlimWaiter CustomWait(
this SemaphoreSlim semaphoreSlim,
CancellationToken cancellationToken = default)
{
semaphoreSlim.Wait(cancellationToken);
return new SemaphoreSlimWaiter(semaphoreSlim, entered: true);
}
}

0 comments on commit 967b650

Please sign in to comment.