diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-05-28 19:05:51 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2024-05-28 19:05:51 -0500 |
| commit | 400cf795c01184ed28471a0b0e91bda07be8411c (patch) | |
| tree | cf354febaf47a5e1b34bbc4229e98c4728073656 /src | |
| parent | b55e5545586f60419aada5bafaf90c34d76cbf67 (diff) | |
| download | slskd-400cf795c01184ed28471a0b0e91bda07be8411c.tar.gz | |
add ability to use ConnectionWatchdog to make initial connection
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/ConnectionWatchdog.cs | 49 |
1 files changed, 35 insertions, 14 deletions
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 { |
