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 From 41032280e1fe34aa09707b3d3d116e11b794f727 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 18 Feb 2026 18:54:53 -0600 Subject: specify baseDelay --- src/slskd/Core/ConnectionWatchdog.cs | 1 + src/slskd/Integrations/FTP/FTPService.cs | 1 + src/slskd/Integrations/Pushbullet/PushbulletService.cs | 1 + src/slskd/Integrations/Webhooks/WebhookService.cs | 1 + src/slskd/Relay/RelayClient.cs | 7 +++++-- 5 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Core/ConnectionWatchdog.cs b/src/slskd/Core/ConnectionWatchdog.cs index d2f7caa8..1cf1779f 100644 --- a/src/slskd/Core/ConnectionWatchdog.cs +++ b/src/slskd/Core/ConnectionWatchdog.cs @@ -264,6 +264,7 @@ namespace slskd { var (delay, jitter) = Compute.ExponentialBackoffDelay( iteration: attempts, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds); var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000); diff --git a/src/slskd/Integrations/FTP/FTPService.cs b/src/slskd/Integrations/FTP/FTPService.cs index cb2b3a3f..ebf34708 100644 --- a/src/slskd/Integrations/FTP/FTPService.cs +++ b/src/slskd/Integrations/FTP/FTPService.cs @@ -76,6 +76,7 @@ namespace slskd.Integrations.FTP isRetryable: (attempts, ex) => true, onFailure: (attempts, ex) => Log.LogInformation("Failed attempt #{Attempts} to upload {Filename} to FTP: {Message}", attempts, fileAndParentDirectory, ex.Message), maxAttempts: FtpOptions.RetryAttempts, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 30000); } catch (RetryException ex) diff --git a/src/slskd/Integrations/Pushbullet/PushbulletService.cs b/src/slskd/Integrations/Pushbullet/PushbulletService.cs index 29033381..4c9df98c 100644 --- a/src/slskd/Integrations/Pushbullet/PushbulletService.cs +++ b/src/slskd/Integrations/Pushbullet/PushbulletService.cs @@ -153,6 +153,7 @@ namespace slskd.Integrations.Pushbullet isRetryable: (attempts, ex) => true, onFailure: (attempts, ex) => Log.LogWarning("Failed attempt #{Attempts} to send Pushbullet notification {Title} {Body}: {Message}", attempts, title, body, ex.Message), maxAttempts: PushbulletOptions.RetryAttempts, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 30000); Log.LogInformation("Sent Pushbullet notification {Title} {Body}", title, body); diff --git a/src/slskd/Integrations/Webhooks/WebhookService.cs b/src/slskd/Integrations/Webhooks/WebhookService.cs index 62739d7a..8a009623 100644 --- a/src/slskd/Integrations/Webhooks/WebhookService.cs +++ b/src/slskd/Integrations/Webhooks/WebhookService.cs @@ -125,6 +125,7 @@ public class WebhookService } }, maxAttempts: webhook.Value.Retry.Attempts, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 30000); sw.Stop(); diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index ae34e344..3e1903fb 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -162,8 +162,9 @@ namespace slskd.Relay Log.Warning("Failed attempt #{Attempts} to connect to relay controller: {Message}", attempts, ex.Message); }, maxAttempts: int.MaxValue, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 30000, - StartCancellationTokenSource.Token); + cancellationToken: StartCancellationTokenSource.Token); } finally { @@ -482,6 +483,7 @@ namespace slskd.Relay isRetryable: (_, _) => true, onFailure: (_, ex) => Log.Error(ex, "Failed to handle file download notification for {Filename} ({Token})", filename, token), maxAttempts: 3, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 60000); Log.Information("File {Filename} successfully downloaded to {Destination}", filename, destinationFile); @@ -592,8 +594,9 @@ namespace slskd.Relay isRetryable: (_, _) => true, onFailure: (count, ex) => Log.Warning("Failed attempt #{Attempts} to obtain share upload token: {Message}", count, ex.Message), maxAttempts: 3, + baseDelayInMilliseconds: 1000, maxDelayInMilliseconds: 5000, - cancellationToken); + cancellationToken: cancellationToken); Log.Debug("Share upload token {Token}", token); -- cgit v1.2.3 From db77adb4c086fe1166449c13af88e2e3bef6590a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 17 Feb 2026 22:10:12 -0600 Subject: add base delay --- src/slskd/Common/Retry.cs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs index d026a0c4..457d32fb 100644 --- a/src/slskd/Common/Retry.cs +++ b/src/slskd/Common/Retry.cs @@ -35,6 +35,7 @@ namespace slskd /// 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. /// The maximum delay in milliseconds. /// The token to monitor for cancellation requests. /// The execution context. @@ -63,6 +64,7 @@ namespace slskd /// 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. /// The maximum delay in milliseconds. /// The token to monitor for cancellation requests. /// The Type of the logic return value. -- cgit v1.2.3 From 6f4c91344d5e4be512dec16600e805cc17178365 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 17 Feb 2026 22:10:00 -0600 Subject: allow base delay to be sent to ExponentialBackoffDelay --- src/slskd/Common/Compute.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index befceb88..f614d011 100644 --- a/src/slskd/Common/Compute.cs +++ b/src/slskd/Common/Compute.cs @@ -26,14 +26,14 @@ namespace slskd /// public static class Compute { - public static (int Delay, int Jitter) ExponentialBackoffDelay(int iteration, int maxDelayInMilliseconds = int.MaxValue) + public static (int Delay, int Jitter) ExponentialBackoffDelay(int iteration, int baseDelayInMilliseconds = 1000, int maxDelayInMilliseconds = int.MaxValue) { iteration = Math.Min(100, iteration); - var computedDelay = Math.Floor((Math.Pow(2, iteration) - 1) / 2) * 1000; + var computedDelay = Math.Floor((Math.Pow(2, iteration) - 1) / 2) * baseDelayInMilliseconds; var clampedDelay = (int)Math.Min(computedDelay, maxDelayInMilliseconds); - var jitter = new Random().Next(1000); + var jitter = Random.Shared.Next(baseDelayInMilliseconds); return (clampedDelay, jitter); } -- cgit v1.2.3