aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-05-28 19:12:05 -0500
committerGitHub <noreply@github.com>2024-05-28 19:12:05 -0500
commitb33449b0282d7f466be22570c26e9e3c982b888a (patch)
treec75dac6ff1506f1da580633740c4fa7e95f8137e /src
parentb55e5545586f60419aada5bafaf90c34d76cbf67 (diff)
parent3901f5df412d53f273a4939b69838468d5f1989b (diff)
downloadslskd-b33449b0282d7f466be22570c26e9e3c982b888a.tar.gz
Merge pull request #1135 from slskd/retry
Add retry loop to initial server connection (fixes crash when starting the application while the Soulseek server is unreachable)
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Application.cs10
-rw-r--r--src/slskd/Core/ConnectionWatchdog.cs49
2 files changed, 37 insertions, 22 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 0ad8c800..f92b4647 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -446,13 +446,7 @@ namespace slskd
}
else
{
- var opt = OptionsAtStartup.Soulseek;
-
- await Client.ConnectAsync(
- address: opt.Address,
- port: opt.Port,
- username: opt.Username,
- password: opt.Password).ConfigureAwait(false);
+ ConnectionWatchdog.Start();
}
}
}
@@ -825,7 +819,7 @@ namespace slskd
else
{
Log.Error("Disconnected from the Soulseek server: {Message}", args.Exception?.Message ?? args.Message);
- ConnectionWatchdog.Start();
+ ConnectionWatchdog.Restart();
}
}
diff --git a/src/slskd/Core/ConnectionWatchdog.cs b/src/slskd/Core/ConnectionWatchdog.cs
index 19e2d47c..4ffc58e0 100644
--- a/src/slskd/Core/ConnectionWatchdog.cs
+++ b/src/slskd/Core/ConnectionWatchdog.cs
@@ -36,12 +36,18 @@ namespace slskd
bool IsEnabled { get; }
/// <summary>
- /// Starts monitoring the server connection.
+ /// Initializes the watchdog and makes the initial connection to the server.
/// </summary>
- /// <remarks>This should be called when the connection is disconnected.</remarks>
+ /// <remarks>This should be called at application startup.</remarks>
void Start();
/// <summary>
+ /// Starts monitoring the server connection following a disconnect.
+ /// </summary>
+ /// <remarks>This should be called when the connection is disconnected.</remarks>
+ void Restart();
+
+ /// <summary>
/// Stops monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the application is reasonably certain that the connection is connected.</remarks>
@@ -103,13 +109,23 @@ namespace slskd
}
/// <summary>
+ /// Initializes the watchdog and makes the initial connection to the server.
+ /// </summary>
+ /// <remarks>This should be called at application startup.</remarks>
+ public void Start()
+ {
+ WatchdogTimer.Enabled = true;
+ _ = AttemptReconnect(attempts: 0);
+ }
+
+ /// <summary>
/// Starts monitoring the server connection.
/// </summary>
/// <remarks>This should be called when the connection is disconnected.</remarks>
- public void Start()
+ public void Restart()
{
WatchdogTimer.Enabled = true;
- _ = AttemptReconnect();
+ _ = AttemptReconnect(attempts: 1);
}
/// <summary>
@@ -135,7 +151,7 @@ namespace slskd
}
}
- private async Task AttemptReconnect()
+ private async Task AttemptReconnect(int attempts = 1)
{
if (await SyncRoot.WaitAsync(0))
{
@@ -146,19 +162,24 @@ namespace slskd
return;
}
- var attempts = 1;
-
while (true)
{
- var (delay, jitter) = Compute.ExponentialBackoffDelay(
- iteration: attempts,
- maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
+ if (attempts > 0)
+ {
+ var (delay, jitter) = Compute.ExponentialBackoffDelay(
+ iteration: attempts,
+ maxDelayInMilliseconds: ReconnectMaxDelayMilliseconds);
- var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000);
- Log.Information($"Waiting about {(approximateDelay == 1 ? "a second" : $"{approximateDelay} seconds")} before reconnecting");
- await Task.Delay(delay + jitter);
+ var approximateDelay = (int)Math.Ceiling((double)(delay + jitter) / 1000);
+ Log.Information($"Waiting about {(approximateDelay == 1 ? "a second" : $"{approximateDelay} seconds")} before attempting to reconnect");
+ await Task.Delay(delay + jitter);
- Log.Information("Attempting to reconnect (#{Attempts})...", attempts);
+ Log.Information("Attempting to reconnect to the Soulseek server (#{Attempts})...", attempts);
+ }
+ else
+ {
+ Log.Information("Attempting to connect to the Soulseek server...");
+ }
try
{