aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-03-15 11:18:08 -0500
committerGitHub <noreply@github.com>2026-03-15 11:18:08 -0500
commit032d9a3513642b85379c6b33a479cb7e0a5adc22 (patch)
tree3d87f453f75d858dfa65c24d1e5fc883916736ff
parent0165b5f6cbae759acfe337170f831a11a45634e9 (diff)
parentd1de2611e3662a318d7f1347996e4cd26ae376a3 (diff)
downloadslskd-032d9a3513642b85379c6b33a479cb7e0a5adc22.tar.gz
Merge pull request #1671 from slskd/retry-do
Add the ability to specify base delay for exponential backoff, add onRetry delegate for Retry.Do
-rw-r--r--src/slskd/Common/Compute.cs6
-rw-r--r--src/slskd/Common/Retry.cs33
-rw-r--r--src/slskd/Core/ConnectionWatchdog.cs1
-rw-r--r--src/slskd/Integrations/FTP/FTPService.cs1
-rw-r--r--src/slskd/Integrations/Pushbullet/PushbulletService.cs1
-rw-r--r--src/slskd/Integrations/Webhooks/WebhookService.cs1
-rw-r--r--src/slskd/Relay/RelayClient.cs7
-rw-r--r--tests/slskd.Tests.Unit/Common/ComputeTests.cs55
8 files changed, 85 insertions, 20 deletions
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
/// </summary>
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);
}
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs
index 300c1c51..457d32fb 100644
--- a/src/slskd/Common/Retry.cs
+++ b/src/slskd/Common/Retry.cs
@@ -32,18 +32,28 @@ 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>
/// <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 +61,25 @@ 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>
/// <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 +94,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 +108,7 @@ namespace slskd
try
{
- onFailure(attempts + 1, ex);
+ onFailure?.Invoke(attempts + 1, ex);
if (!isRetryable(attempts + 1, ex))
{
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);
diff --git a/tests/slskd.Tests.Unit/Common/ComputeTests.cs b/tests/slskd.Tests.Unit/Common/ComputeTests.cs
index 021d4faa..9090ad36 100644
--- a/tests/slskd.Tests.Unit/Common/ComputeTests.cs
+++ b/tests/slskd.Tests.Unit/Common/ComputeTests.cs
@@ -5,17 +5,54 @@ namespace slskd.Tests.Unit.Common
public class ComputeTests
{
[Theory]
- [InlineData(1, 300000, 0)]
- [InlineData(2, 300000, 1000)]
- [InlineData(3, 300000, 3000)]
- [InlineData(4, 300000, 7000)]
- [InlineData(5, 300000, 15000)]
- [InlineData(6, 300000, 31000)]
- [InlineData(999999, 300000, 300000)]
- public void ExponentialBackoffDelay(int iteration, int maxDelayInMs, int expectedDelay)
+ // base = 1000 (original cases)
+ [InlineData(1, 1000, 300000, 0)]
+ [InlineData(2, 1000, 300000, 1000)]
+ [InlineData(3, 1000, 300000, 3000)]
+ [InlineData(4, 1000, 300000, 7000)]
+ [InlineData(5, 1000, 300000, 15000)]
+ [InlineData(6, 1000, 300000, 31000)]
+ [InlineData(999999, 1000, 300000, 300000)]
+ // base = 500 — delay should be half of base=1000 cases
+ [InlineData(1, 500, 300000, 0)]
+ [InlineData(2, 500, 300000, 500)]
+ [InlineData(3, 500, 300000, 1500)]
+ [InlineData(4, 500, 300000, 3500)]
+ [InlineData(5, 500, 300000, 7500)]
+ [InlineData(6, 500, 300000, 15500)]
+ [InlineData(999999, 500, 300000, 300000)]
+ // base = 2000 — delay should be double of base=1000 cases
+ [InlineData(1, 2000, 300000, 0)]
+ [InlineData(2, 2000, 300000, 2000)]
+ [InlineData(3, 2000, 300000, 6000)]
+ [InlineData(4, 2000, 300000, 14000)]
+ [InlineData(5, 2000, 300000, 30000)]
+ [InlineData(6, 2000, 300000, 62000)]
+ [InlineData(999999, 2000, 300000, 300000)]
+ public void ExponentialBackoffDelay_Returns_Expected_Delay(int iteration, int baseDelayInMilliseconds, int maxDelayInMs, int expectedDelay)
{
- var (computedDelay, _) = Compute.ExponentialBackoffDelay(iteration, maxDelayInMs);
+ var (computedDelay, _) = Compute.ExponentialBackoffDelay(iteration, baseDelayInMilliseconds, maxDelayInMs);
Assert.Equal(expectedDelay, computedDelay);
}
+
+ [Fact]
+ public void ExponentialBackoffDelay_Uses_Default_Base_Delay_Of_1000()
+ {
+ var (withDefault, _) = Compute.ExponentialBackoffDelay(iteration: 3);
+ var (withExplicit, _) = Compute.ExponentialBackoffDelay(iteration: 3, baseDelayInMilliseconds: 1000);
+
+ Assert.Equal(withExplicit, withDefault);
+ }
+
+ [Theory]
+ [InlineData(1000)]
+ [InlineData(500)]
+ [InlineData(2000)]
+ public void ExponentialBackoffDelay_Returns_Jitter_Within_Base_Delay(int baseDelayInMilliseconds)
+ {
+ var (_, jitter) = Compute.ExponentialBackoffDelay(5, baseDelayInMilliseconds, int.MaxValue);
+
+ Assert.InRange(jitter, 0, baseDelayInMilliseconds - 1);
+ }
}
}