From 6d8d4637621f933a63326d2fe0a69a6b71e6bdb1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Feb 2026 18:54:19 -0600 Subject: add onRetry delegate --- src/slskd/Common/Retry.cs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'src') 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 /// /// The logic to execute. /// A function returning a value indicating whether the last Exception is retryable. + /// An action to execute before beginning a retry attempt. /// An action to execute on failure. /// The maximum number of retry attempts. /// The maximum delay in milliseconds. /// The token to monitor for cancellation requests. /// The execution context. - public static async Task Do(Func task, Func isRetryable = null, Action onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue, CancellationToken cancellationToken = default) + public static async Task Do( + Func task, + Func isRetryable = null, + Action onRetry = null, + Action onFailure = null, + int maxAttempts = 3, + int baseDelayInMilliseconds = 1000, + int maxDelayInMilliseconds = int.MaxValue, + CancellationToken cancellationToken = default) { await Do(async () => { await task(); return Task.FromResult(null); - }, isRetryable, onFailure, maxAttempts, maxDelayInMilliseconds, cancellationToken); + }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); } /// @@ -51,16 +60,24 @@ namespace slskd /// /// The logic to execute. /// A function returning a value indicating whether the last Exception is retryable. + /// An action to execute before beginning a retry attempt. /// An action to execute on failure. /// The maximum number of retry attempts. /// The maximum delay in milliseconds. /// The token to monitor for cancellation requests. /// The Type of the logic return value. /// The execution context. - public static async Task Do(Func> task, Func isRetryable = null, Action onFailure = null, int maxAttempts = 3, int maxDelayInMilliseconds = int.MaxValue, CancellationToken cancellationToken = default) + public static async Task Do( + Func> task, + Func isRetryable = null, + Action onRetry = null, + Action onFailure = null, + int maxAttempts = 3, + int baseDelayInMilliseconds = 1000, + int maxDelayInMilliseconds = int.MaxValue, + CancellationToken cancellationToken = default) { isRetryable ??= (_, _) => true; - onFailure ??= (_, _) => { }; var exceptions = new List(); @@ -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)) { -- cgit v1.2.3