aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-02-18 18:54:19 -0600
committerJP Dillingham <jp@dillingham.me>2026-03-15 11:11:41 -0500
commit6d8d4637621f933a63326d2fe0a69a6b71e6bdb1 (patch)
tree2e656bbc1ba19345968fb5dd1d0b5899517fdd92 /src
parent0165b5f6cbae759acfe337170f831a11a45634e9 (diff)
downloadslskd-6d8d4637621f933a63326d2fe0a69a6b71e6bdb1.tar.gz
add onRetry delegate
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Retry.cs31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs
index 300c1c51..d026a0c4 100644
--- a/src/slskd/Common/Retry.cs
+++ b/src/slskd/Common/Retry.cs
@@ -32,18 +32,27 @@ namespace slskd
/// </summary>
/// <param name="task">The logic to execute.</param>
/// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param>
+ /// <param name="onRetry">An action to execute before beginning a retry attempt.</param>
/// <param name="onFailure">An action to execute on failure.</param>
/// <param name="maxAttempts">The maximum number of retry attempts.</param>
/// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The execution context.</returns>
- public static async Task Do(Func<Task> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue, CancellationToken cancellationToken = default)
+ public static async Task Do(
+ Func<Task> task,
+ Func<int, Exception, bool> isRetryable = null,
+ Action<int, int> onRetry = null,
+ Action<int, Exception> onFailure = null,
+ int maxAttempts = 3,
+ int baseDelayInMilliseconds = 1000,
+ int maxDelayInMilliseconds = int.MaxValue,
+ CancellationToken cancellationToken = default)
{
await Do<object>(async () =>
{
await task();
return Task.FromResult<object>(null);
- }, isRetryable, onFailure, maxAttempts, maxDelayInMilliseconds, cancellationToken);
+ }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken);
}
/// <summary>
@@ -51,16 +60,24 @@ namespace slskd
/// </summary>
/// <param name="task">The logic to execute.</param>
/// <param name="isRetryable">A function returning a value indicating whether the last Exception is retryable.</param>
+ /// <param name="onRetry">An action to execute before beginning a retry attempt.</param>
/// <param name="onFailure">An action to execute on failure.</param>
/// <param name="maxAttempts">The maximum number of retry attempts.</param>
/// <param name="maxDelayInMilliseconds">The maximum delay in milliseconds.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <typeparam name="T">The Type of the logic return value.</typeparam>
/// <returns>The execution context.</returns>
- public static async Task<T> Do<T>(Func<Task<T>> task, Func<int, Exception, bool> isRetryable = null, Action<int, Exception> onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue, CancellationToken cancellationToken = default)
+ public static async Task<T> Do<T>(
+ Func<Task<T>> task,
+ Func<int, Exception, bool> isRetryable = null,
+ Action<int, int> onRetry = null,
+ Action<int, Exception> onFailure = null,
+ int maxAttempts = 3,
+ int baseDelayInMilliseconds = 1000,
+ int maxDelayInMilliseconds = int.MaxValue,
+ CancellationToken cancellationToken = default)
{
isRetryable ??= (_, _) => true;
- onFailure ??= (_, _) => { };
var exceptions = new List<Exception>();
@@ -75,7 +92,9 @@ namespace slskd
{
if (attempts > 0)
{
- var (delay, jitter) = Compute.ExponentialBackoffDelay(attempts, maxDelayInMilliseconds);
+ var (delay, jitter) = Compute.ExponentialBackoffDelay(attempts, baseDelayInMilliseconds, maxDelayInMilliseconds);
+
+ onRetry?.Invoke(attempts + 1, delay + jitter);
await Task.Delay(delay + jitter, cancellationToken);
}
@@ -87,7 +106,7 @@ namespace slskd
try
{
- onFailure(attempts + 1, ex);
+ onFailure?.Invoke(attempts + 1, ex);
if (!isRetryable(attempts + 1, ex))
{