diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-02-18 18:54:19 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-02-18 18:54:19 -0600 |
| commit | c4574d13c454cfcef27ed29da446159e7f46f919 (patch) | |
| tree | c872f6f9303ccf47661ebef902f4b9eee939b43f /src | |
| parent | 9d9259214d9c3ce01758528ddcde508d7d895783 (diff) | |
| download | slskd-c4574d13c454cfcef27ed29da446159e7f46f919.tar.gz | |
add onRetry delegate
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Retry.cs | 11 |
1 files changed, 8 insertions, 3 deletions
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 /// </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="baseDelayInMilliseconds">The base delay in milliseconds.</param> @@ -41,6 +42,7 @@ namespace slskd 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, @@ -51,7 +53,7 @@ namespace slskd { await task(); return Task.FromResult<object>(null); - }, isRetryable, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); + }, isRetryable, onRetry, onFailure, maxAttempts, baseDelayInMilliseconds, maxDelayInMilliseconds, cancellationToken); } /// <summary> @@ -59,6 +61,7 @@ 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="baseDelayInMilliseconds">The base delay in milliseconds.</param> @@ -69,6 +72,7 @@ namespace slskd 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, @@ -76,7 +80,6 @@ namespace slskd CancellationToken cancellationToken = default) { isRetryable ??= (_, _) => true; - onFailure ??= (_, _) => { }; var exceptions = new List<Exception>(); @@ -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)) { |
