diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-07-12 14:58:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-12 14:58:53 -0500 |
| commit | 367b2cee4d475277fb278e6537f1014436d5a09f (patch) | |
| tree | 2bcdd9f727ebfbb57787ee203e6760a3e0f1e910 /src/slskd/Core | |
| parent | eed42e9c4916e2ba653e40147de546e7af3af563 (diff) | |
| parent | 11456386b8c3dcd6d054b2ceff5861a972fbe3cd (diff) | |
| download | slskd-367b2cee4d475277fb278e6537f1014436d5a09f.tar.gz | |
Merge pull request #1385 from slskd/dns
Adjust auto-reconnect logic
Diffstat (limited to 'src/slskd/Core')
| -rw-r--r-- | src/slskd/Core/API/Controllers/ServerController.cs | 31 | ||||
| -rw-r--r-- | src/slskd/Core/ConnectionWatchdog.cs | 217 | ||||
| -rw-r--r-- | src/slskd/Core/State.cs | 10 |
3 files changed, 194 insertions, 64 deletions
diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index 9b8e2764..da777b02 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -19,9 +19,9 @@ using Microsoft.Extensions.Options; namespace slskd.Core.API { - using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Soulseek; @@ -37,15 +37,18 @@ namespace slskd.Core.API { public ServerController( ISoulseekClient soulseekClient, + ConnectionWatchdog connectionWatchdog, IOptionsSnapshot<Options> optionsSnapshot, IStateSnapshot<State> stateSnapshot) { Client = soulseekClient; + ConnectionWatchdog = connectionWatchdog; OptionsSnapshot = optionsSnapshot; StateSnapshot = stateSnapshot; } private ISoulseekClient Client { get; } + private ConnectionWatchdog ConnectionWatchdog { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } private IStateSnapshot<State> StateSnapshot { get; } @@ -57,23 +60,28 @@ namespace slskd.Core.API [Route("")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] + [ProducesResponseType(StatusCodes.Status205ResetContent)] [ProducesResponseType(403)] - public async Task<IActionResult> Connect() + public IActionResult Connect() { if (Program.IsRelayAgent) { return Forbid(); } - if (!Client.State.HasFlag(SoulseekClientStates.Connected)) + // if the watchdog is already enabled, we're already attempting to connect. the user might have changed something + // or perhaps is just being impatient; either way, restart the retry loop so they can see results faster + // remember, we may be in a ~5 minute delay at this moment + if (ConnectionWatchdog.IsEnabled) { - var opt = OptionsSnapshot.Value.Soulseek; + ConnectionWatchdog.Restart(); + return StatusCode(StatusCodes.Status205ResetContent); + } - await Client.ConnectAsync( - address: opt.Address, - port: opt.Port, - username: opt.Username, - password: opt.Password); + if (!ConnectionWatchdog.IsEnabled) + { + ConnectionWatchdog.Start(); + return Ok(); } return Ok(); @@ -96,8 +104,13 @@ namespace slskd.Core.API return Forbid(); } + // stop the watchdog so that it will exit any retry logic + ConnectionWatchdog.Stop(abortReconnect: true); + if (Client.State.HasFlag(SoulseekClientStates.Connected)) { + // the IntentionalDisconnectException is used to indicate that the disconnect was intentional, which + // prevents the watchdog from trying to reconnect Client.Disconnect(message, new IntentionalDisconnectException(message)); } diff --git a/src/slskd/Core/ConnectionWatchdog.cs b/src/slskd/Core/ConnectionWatchdog.cs index 4ffc58e0..b7a4fa34 100644 --- a/src/slskd/Core/ConnectionWatchdog.cs +++ b/src/slskd/Core/ConnectionWatchdog.cs @@ -26,38 +26,14 @@ namespace slskd using Soulseek; /// <summary> - /// Monitors the connection to the Soulseek server and reconnects with exponential backoff, if necessary. - /// </summary> - public interface IConnectionWatchdog : IDisposable - { - /// <summary> - /// Gets a value indicating whether the watchdog is monitoring the server connection. - /// </summary> - bool IsEnabled { get; } - - /// <summary> - /// Initializes the watchdog and makes the initial connection to the server. - /// </summary> - /// <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> - void Stop(); - } - - /// <summary> /// Monitors the connection to the Soulseek network and reconnects with exponential backoff, if necessary. /// </summary> - public class ConnectionWatchdog : IConnectionWatchdog + /// <remarks> + /// This class is intended to be Start()ed either at application startup or when the connection is disconnected, and + /// Stop()ed when the application is connected again; it doesn't "run" all the time because there are cases where + /// a user has manually disconnected, was kicked by another login somewhere, etc. where we don't want to reconnect. + /// </remarks> + public class ConnectionWatchdog { private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes @@ -66,15 +42,15 @@ namespace slskd /// </summary> /// <param name="soulseekClient"></param> /// <param name="optionsMonitor"></param> - /// <param name="state"></param> + /// <param name="applicationState"></param> public ConnectionWatchdog( ISoulseekClient soulseekClient, IOptionsMonitor<Options> optionsMonitor, - IStateMonitor<State> state) + IManagedState<State> applicationState) { Client = soulseekClient; - Options = optionsMonitor; - State = state; + OptionsMonitor = optionsMonitor; + ApplicationState = applicationState; WatchdogTimer = new System.Timers.Timer() { @@ -83,7 +59,11 @@ namespace slskd Enabled = false, }; - WatchdogTimer.Elapsed += (sender, args) => _ = AttemptReconnect(); + // the timer is used here to ensure that we keep trying if the connection logic fails for some reason. i'm questioning + // this at the moment and may continue to do so + WatchdogTimer.Elapsed += (sender, args) => _ = AttemptConnection(source: nameof(WatchdogTimer)); + + OptionsMonitor.OnChange(options => OptionsChanged(options)); } /// <summary> @@ -91,13 +71,20 @@ namespace slskd /// </summary> public bool IsEnabled => WatchdogTimer.Enabled; + /// <summary> + /// Gets a value indicating whether the watchdog is actively attempting to connect. + /// </summary> + public bool IsAttemptingConnection => SyncRoot.CurrentCount == 0; + private ISoulseekClient Client { get; } private bool Disposed { get; set; } private ILogger Log { get; } = Serilog.Log.ForContext<Application>(); - private IOptionsMonitor<Options> Options { get; set; } - private IStateMonitor<State> State { get; } + private IOptionsMonitor<Options> OptionsMonitor { get; set; } + private IManagedState<State> ApplicationState { get; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1, 1); + private object StateLock { get; } = new object(); private System.Timers.Timer WatchdogTimer { get; } + private CancellationTokenSource CancellationTokenSource { get; set; } /// <summary> /// Disposes this instance. @@ -112,27 +99,60 @@ namespace slskd /// Initializes the watchdog and makes the initial connection to the server. /// </summary> /// <remarks>This should be called at application startup.</remarks> - public void Start() + public virtual void Start() { WatchdogTimer.Enabled = true; - _ = AttemptReconnect(attempts: 0); + UpdateApplicationState(); + + // note: this is synchronized so only 1 can run at a time. we're only calling it here to avoid the + // initial timer tick duration + _ = AttemptConnection(source: nameof(Start)); } /// <summary> - /// Starts monitoring the server connection. + /// Stops the watchdog and aborts any active reconnection loop, then starts again. /// </summary> - /// <remarks>This should be called when the connection is disconnected.</remarks> - public void Restart() + /// <remarks> + /// This should be called when we have a reason to restart an in-process retry loop, such as a setting change, + /// user request, etc. + /// </remarks> + public virtual void Restart() { - WatchdogTimer.Enabled = true; - _ = AttemptReconnect(attempts: 1); + if (IsEnabled) + { + Log.Information("(Re)connection process restarted"); + Stop(abortReconnect: true); + Start(); + } } /// <summary> /// Stops monitoring the server connection. /// </summary> + /// <param name="abortReconnect">A value indicating whether to abort an ongoing reconnect attempt.</param> /// <remarks>This should be called when the application is reasonably certain that the connection is connected.</remarks> - public void Stop() => WatchdogTimer.Enabled = false; + public virtual void Stop(bool abortReconnect = false) + { + // stop the timer first to avoid having it start the connection process again when the cts is cancelled + WatchdogTimer.Enabled = false; + UpdateApplicationState(); + + // note: the connect event fires before the connection logic is complete, so there is a chain of events + // that leads to the application being connected and then immediately disconnected if this CTS is cancelled + // when the watchdog is stopped due to sucessful connection. pass the cancelReconnect flag only when the user + // wants to abort the reconnect logic. + if (abortReconnect) + { + try + { + CancellationTokenSource?.Cancel(throwOnFirstException: false); + } + catch (Exception) + { + // noop. Cancel() can throw if registered callbacks throw, but we don't have any right now and we don't care anyway + } + } + } /// <summary> /// Disposes this instance. @@ -140,30 +160,80 @@ namespace slskd /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { + Stop(abortReconnect: true); + if (!Disposed) { if (disposing) { WatchdogTimer?.Dispose(); + CancellationTokenSource?.Dispose(); } Disposed = true; } } - private async Task AttemptReconnect(int attempts = 1) + private void UpdateApplicationState(DateTime? nextAttemptAt = null) + { + lock (StateLock) + { + ApplicationState.SetValue(state => state with + { + ConnectionWatchdog = new ServerConnectionWatchdogState() + { + IsEnabled = IsEnabled, + IsAttemptingConnection = IsAttemptingConnection, + NextAttemptAt = IsAttemptingConnection ? nextAttemptAt ?? state.ConnectionWatchdog.NextAttemptAt : null, // only null this if we're not attempting, otherwise it sticks + }, + }); + } + } + + private void OptionsChanged(Options options) { + // it's possible that a user begins changing connection settings in an attempt to get the client to connect + // if they edit the options _at all_, assume this is the case and restart the retry loop from the beginning + if (IsEnabled) + { + Log.Information("Options changed, restarting (re)connection process..."); + Restart(); + } + } + + private async Task AttemptConnection(string source) + { + // semaphore is obtained here and released only when the reconnect attempt is complete, so + // one and only one invocation of this can run at a time if (await SyncRoot.WaitAsync(0)) { try { - if (State.CurrentValue.Server.IsConnected) - { - return; - } + Log.Debug("AttemptConnection initiated by {Source}", source); + + /* + go until we connect and break, or something stops the watchdog. why don't we just use the timer here? + good question. we could, but a loop gives us more control and precision. at the expensive of needing to + break out of it if a user wants to stop or reset. reevaluate later! - while (true) + things that can cause us to exit this loop: + - the timer being disabled, which will exit whenever the while expression is evaluated. any Task.Delay or connection attempt will keep going (this is more of a backstop) + - CancellationTokenSource being tripped, which will throw TaskCanceledException or OperationCanceledException somewhere + - a successful connection OR the server becomes connected another way + */ + int attempts = 0; + + while (IsEnabled) { + // bail out if we're already connected. it's possible that something else outside of the watchdog + // connects the client. highly unlikely but that might change if someone inadvertently adds some logic. + if (ApplicationState.CurrentValue.Server.IsConnected) + { + return; + } + + CancellationTokenSource = new CancellationTokenSource(); + if (attempts > 0) { var (delay, jitter) = Compute.ExponentialBackoffDelay( @@ -172,13 +242,17 @@ namespace slskd 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 to the Soulseek server (#{Attempts})...", attempts); + UpdateApplicationState(nextAttemptAt: DateTime.UtcNow.AddMilliseconds(delay + jitter)); + + await Task.Delay(delay + jitter, cancellationToken: CancellationTokenSource?.Token ?? CancellationToken.None); + + Log.Information("Attempting to connect to the Soulseek server (#{Attempts})...", attempts); } else { Log.Information("Attempting to connect to the Soulseek server..."); + UpdateApplicationState(nextAttemptAt: DateTime.UtcNow); } try @@ -186,16 +260,25 @@ namespace slskd // reconnect with the latest configuration values we have for username and password, instead of the // options that were captured at startup. if a user has updated these values prior to the disconnect, // the changes will take effect now. - var opt = Options.CurrentValue.Soulseek; + var opt = OptionsMonitor.CurrentValue.Soulseek; + // note: cancelling the CTS before the connect logic is fully complete (e.g. reacting to the connect event) + // will cause the client to connect, then disconnect immediately await Client.ConnectAsync( address: opt.Address, port: opt.Port, username: opt.Username, - password: opt.Password); + password: opt.Password, + cancellationToken: CancellationTokenSource?.Token ?? CancellationToken.None); break; } + catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException) + { + Log.Information("Reconnection attempt cancelled"); + Log.Debug(ex, "ConnectAsync() threw {Exception}", ex); + return; + } catch (Exception ex) { attempts++; @@ -203,9 +286,33 @@ namespace slskd } } } + catch (Exception ex) when (ex is TaskCanceledException || ex is OperationCanceledException) + { + Log.Information("Reconnection attempt cancelled"); + Log.Debug(ex, "Reconnect logic threw {Exception}", ex); + } + catch (Exception ex) + { + Log.Warning(ex, "Reconnect attempt failed: {Message}", ex.Message); + } finally { + var cts = CancellationTokenSource; + CancellationTokenSource = null; + + try + { + cts?.Dispose(); + } + catch (Exception) + { + // noop. i don't think this can throw, but if we fail to release the SyncRoot we'll be in trouble. + } + SyncRoot.Release(); + + // do this after the semaphore is released so that IsAttemptingConnection is false and the next attempt is nulled + UpdateApplicationState(); } } } diff --git a/src/slskd/Core/State.cs b/src/slskd/Core/State.cs index 9ae6df6b..86b628b5 100644 --- a/src/slskd/Core/State.cs +++ b/src/slskd/Core/State.cs @@ -35,6 +35,7 @@ namespace slskd public bool PendingReconnect { get; init; } public bool PendingRestart { get; init; } public ServerState Server { get; init; } = new ServerState(); + public ServerConnectionWatchdogState ConnectionWatchdog { get; init; } = new ServerConnectionWatchdogState(); public RelayState Relay { get; init; } = new RelayState(); public UserState User { get; init; } = new UserState(); public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); @@ -61,10 +62,19 @@ namespace slskd public IPEndPoint IPEndPoint { get; init; } public SoulseekClientStates State { get; init; } public bool IsConnected => State.HasFlag(SoulseekClientStates.Connected); + public bool IsConnecting => State.HasFlag(SoulseekClientStates.Connecting); public bool IsLoggedIn => State.HasFlag(SoulseekClientStates.LoggedIn); + public bool IsLoggingIn => State.HasFlag(SoulseekClientStates.LoggingIn); public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); } + public record ServerConnectionWatchdogState + { + public bool IsEnabled { get; init; } = false; + public bool IsAttemptingConnection { get; init; } = false; + public DateTime? NextAttemptAt { get; init; } + } + public record RelayState { public RelayMode Mode { get; init; } |
