From 967b650c0ce39b7267d3d2b1896ca370962dc79e Mon Sep 17 00:00:00 2001 From: erri120 Date: Mon, 25 Sep 2023 15:56:32 +0200 Subject: [PATCH] Update locking --- .../LoginManager.cs | 5 +++-- src/NexusMods.Common/SemaphoreSlimWaiter.cs | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Networking/NexusMods.Networking.NexusWebApi.NMA/LoginManager.cs b/src/Networking/NexusMods.Networking.NexusWebApi.NMA/LoginManager.cs index 4d016575fb..e4b91c3433 100644 --- a/src/Networking/NexusMods.Networking.NexusWebApi.NMA/LoginManager.cs +++ b/src/Networking/NexusMods.Networking.NexusWebApi.NMA/LoginManager.cs @@ -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; diff --git a/src/NexusMods.Common/SemaphoreSlimWaiter.cs b/src/NexusMods.Common/SemaphoreSlimWaiter.cs index 532e443bbf..87c6f2fddb 100644 --- a/src/NexusMods.Common/SemaphoreSlimWaiter.cs +++ b/src/NexusMods.Common/SemaphoreSlimWaiter.cs @@ -47,4 +47,15 @@ public static SemaphoreSlimWaiter CustomWait( var entered = semaphoreSlim.Wait(timeout, cancellationToken); return new SemaphoreSlimWaiter(semaphoreSlim, entered); } + + /// + /// Custom wait infinitely using . + /// + public static SemaphoreSlimWaiter CustomWait( + this SemaphoreSlim semaphoreSlim, + CancellationToken cancellationToken = default) + { + semaphoreSlim.Wait(cancellationToken); + return new SemaphoreSlimWaiter(semaphoreSlim, entered: true); + } }