aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-06-11 15:50:45 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-06-11 15:50:45 -0500
commit07331bd2d16bfba8d3d0365d1e7860e807ba6d30 (patch)
treec249ca23515b0aedc66dd242f1b8088cac4105eb
parent94865acdf168cb11fb1e28c3a50673611ecfc369 (diff)
downloadslskd-07331bd2d16bfba8d3d0365d1e7860e807ba6d30.tar.gz
simplify exponential backoff logic
-rw-r--r--src/slskd/Common/Compute.cs14
-rw-r--r--src/slskd/Core/Service.cs14
-rw-r--r--tests/slskd.Tests.Unit/Common/ComputeTests.cs18
3 files changed, 24 insertions, 22 deletions
diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs
index dec0e661..ef1f1882 100644
--- a/src/slskd/Common/Compute.cs
+++ b/src/slskd/Common/Compute.cs
@@ -24,11 +24,17 @@ namespace slskd
/// </summary>
public static class Compute
{
- public static int ExponentialBackoffDelay(int iteration, int delayInSeconds, int backoffRate, int maxDelayInSeconds = int.MaxValue)
+ public static (int Delay, int Jitter) ExponentialBackoffDelay(int iteration, int maxDelayInMilliseconds = int.MaxValue)
{
- var computedDelay = Math.Pow(delayInSeconds * backoffRate, Math.Min(50, iteration)) - delayInSeconds;
+ iteration = Math.Min(100, iteration);
+
+ var computedDelay = Math.Floor((Math.Pow(2, iteration) - 1) / 2) * 1000;
var safeDelay = (int)Math.Min(computedDelay, int.MaxValue);
- return Math.Min(safeDelay, maxDelayInSeconds);
+ var clampedDelay = Math.Min(safeDelay, maxDelayInMilliseconds);
+
+ var jitter = new Random().Next(1000);
+
+ return (clampedDelay, jitter);
}
}
-}
+} \ No newline at end of file
diff --git a/src/slskd/Core/Service.cs b/src/slskd/Core/Service.cs
index 4c1d2a4f..a8324a75 100644
--- a/src/slskd/Core/Service.cs
+++ b/src/slskd/Core/Service.cs
@@ -38,9 +38,7 @@ namespace slskd
public class Service : IHostedService
{
- private static readonly int ReconnectDelaySeconds = 1;
- private static readonly int ReconnectMaxDelaySeconds = 300; // 5 minutes
- private static readonly int ReconnectBackoffRate = 2;
+ private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes
private static int reconnectAttempts = 0;
private static (int Directories, int Files) sharedCounts = (0, 0);
@@ -232,14 +230,12 @@ namespace slskd
}
else
{
- var delay = Compute.ExponentialBackoffDelay(
+ var (delay, jitter) = Compute.ExponentialBackoffDelay(
iteration: reconnectAttempts,
- backoffRate: ReconnectBackoffRate,
- delayInSeconds: ReconnectDelaySeconds,
- maxDelayInSeconds: ReconnectMaxDelaySeconds);
+ maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
- Logger.Information($"Waiting {delay} second(s) before reconnecting...");
- await Task.Delay(delay * 1000);
+ Logger.Information($"Waiting {delay + jitter} millisecond(s) before reconnecting...");
+ await Task.Delay(delay + jitter);
Interlocked.Increment(ref reconnectAttempts);
diff --git a/tests/slskd.Tests.Unit/Common/ComputeTests.cs b/tests/slskd.Tests.Unit/Common/ComputeTests.cs
index 99b05d3f..5a7922f6 100644
--- a/tests/slskd.Tests.Unit/Common/ComputeTests.cs
+++ b/tests/slskd.Tests.Unit/Common/ComputeTests.cs
@@ -5,16 +5,16 @@ namespace slskd.Tests.Unit
public class ComputeTests
{
[Theory]
- [InlineData(0, 1, 2, 300, 0)]
- [InlineData(1, 1, 2, 300, 1)]
- [InlineData(2, 1, 2, 300, 3)]
- [InlineData(3, 1, 2, 300, 7)]
- [InlineData(4, 1, 2, 300, 15)]
- [InlineData(5, 1, 2, 300, 31)]
- [InlineData(999999, 1, 2, 300, 300)]
- public void ExponentialBackoffDelay(int iteration, int delayInSeconds, int backoffRate, int maxDelayInSeconds, int expectedDelay)
+ [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)
{
- var computedDelay = Compute.ExponentialBackoffDelay(iteration, delayInSeconds, backoffRate, maxDelayInSeconds);
+ var (computedDelay, _) = Compute.ExponentialBackoffDelay(iteration, maxDelayInMs);
Assert.Equal(expectedDelay, computedDelay);
}
}