From 400cf795c01184ed28471a0b0e91bda07be8411c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 28 May 2024 19:05:51 -0500 Subject: add ability to use ConnectionWatchdog to make initial connection --- src/slskd/Core/ConnectionWatchdog.cs | 49 +++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'src') 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,11 +36,17 @@ namespace slskd bool IsEnabled { get; } /// - /// Starts monitoring the server connection. + /// Initializes the watchdog and makes the initial connection to the server. /// - /// This should be called when the connection is disconnected. + /// This should be called at application startup. void Start(); + /// + /// Starts monitoring the server connection following a disconnect. + /// + /// This should be called when the connection is disconnected. + void Restart(); + /// /// Stops monitoring the server connection. /// @@ -102,14 +108,24 @@ namespace slskd GC.SuppressFinalize(this); } + /// + /// Initializes the watchdog and makes the initial connection to the server. + /// + /// This should be called at application startup. + public void Start() + { + WatchdogTimer.Enabled = true; + _ = AttemptReconnect(attempts: 0); + } + /// /// Starts monitoring the server connection. /// /// This should be called when the connection is disconnected. - public void Start() + public void Restart() { WatchdogTimer.Enabled = true; - _ = AttemptReconnect(); + _ = AttemptReconnect(attempts: 1); } /// @@ -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 { -- cgit v1.2.3