From c4574d13c454cfcef27ed29da446159e7f46f919 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 | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs index eff88659..457d32fb 100644 --- a/src/slskd/Common/Retry.cs +++ b/src/slskd/Common/Retry.cs @@ -32,6 +32,7 @@ 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 base delay in milliseconds. @@ -41,6 +42,7 @@ namespace slskd public static async Task Do( Func task, Func isRetryable = null, + Action onRetry = null, Action onFailure = null, int maxAttempts = 3, int baseDelayInMilliseconds = 1000, @@ -51,7 +53,7 @@ namespace slskd { await task(); return Task.FromResult(null); - }, isRetryable, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); + }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); } /// @@ -59,6 +61,7 @@ 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 base delay in milliseconds. @@ -69,6 +72,7 @@ namespace slskd public static async Task Do( Func> task, Func isRetryable = null, + Action onRetry = null, Action onFailure = null, int maxAttempts = 3, int baseDelayInMilliseconds = 1000, @@ -76,7 +80,6 @@ namespace slskd CancellationToken cancellationToken = default) { isRetryable ??= (_, _) => true; - onFailure ??= (_, _) => { }; var exceptions = new List(); @@ -92,6 +95,8 @@ namespace slskd if (attempts > 0) { var (delay, jitter) = Compute.ExponentialBackoffDelay(attempts, baseDelayInMilliseconds, maxDelayInMilliseconds); + + onRetry?.Invoke(attempts + 1, delay + jitter); await Task.Delay(delay + jitter, cancellationToken); } @@ -103,7 +108,7 @@ namespace slskd try { - onFailure(attempts + 1, ex); + onFailure?.Invoke(attempts + 1, ex); if (!isRetryable(attempts + 1, ex)) { -- cgit v1.2.3