Actually make ArchiCacheable catch cancellation

We intend to give the caller best result, operation canceled has no value for him, he can check if cancellation token he provided himself got canceled or not
This commit is contained in:
Archi 2023-11-14 21:15:18 +01:00
parent adbf0748f8
commit ac9d4a7783
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA

View file

@ -23,6 +23,7 @@ using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using JetBrains.Annotations;
namespace ArchiSteamFarm.Helpers;
@ -58,7 +59,13 @@ public sealed class ArchiCacheable<T> : IDisposable {
return (true, InitializedValue);
}
await InitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try {
await InitSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
} catch (OperationCanceledException e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);
return ReturnFailedValueFor(cacheFallback);
}
try {
if (IsInitialized && IsRecent) {
@ -68,18 +75,17 @@ public sealed class ArchiCacheable<T> : IDisposable {
(bool success, T? result) = await ResolveFunction(cancellationToken).ConfigureAwait(false);
if (!success) {
return cacheFallback switch {
ECacheFallback.DefaultForType => (false, default(T?)),
ECacheFallback.FailedNow => (false, result),
ECacheFallback.SuccessPreviously => (false, InitializedValue),
_ => throw new InvalidOperationException(nameof(cacheFallback))
};
return ReturnFailedValueFor(cacheFallback, result);
}
InitializedValue = result;
InitializedAt = DateTime.UtcNow;
return (true, result);
} catch (OperationCanceledException e) {
ASF.ArchiLogger.LogGenericDebuggingException(e);
return ReturnFailedValueFor(cacheFallback);
} finally {
InitSemaphore.Release();
}
@ -103,4 +109,17 @@ public sealed class ArchiCacheable<T> : IDisposable {
InitSemaphore.Release();
}
}
private (bool Success, T? Result) ReturnFailedValueFor(ECacheFallback cacheFallback, T? result = default) {
if (!Enum.IsDefined(typeof(ECacheFallback), cacheFallback)) {
throw new InvalidEnumArgumentException(nameof(cacheFallback), (int) cacheFallback, typeof(ECacheFallback));
}
return cacheFallback switch {
ECacheFallback.DefaultForType => (false, default(T?)),
ECacheFallback.FailedNow => (false, result),
ECacheFallback.SuccessPreviously => (false, InitializedValue),
_ => throw new InvalidOperationException(nameof(cacheFallback))
};
}
}