From f3cbc0328772786c85f47ba0c7b25f2e91b1b257 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 8 Jun 2025 17:43:44 -0500 Subject: use the connection watchdog to connect via API; leverage auto retries --- src/slskd/Core/API/Controllers/ServerController.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index 9b8e2764..cfc695a0 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -19,7 +19,6 @@ using Microsoft.Extensions.Options; namespace slskd.Core.API { - using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -37,15 +36,18 @@ namespace slskd.Core.API { public ServerController( ISoulseekClient soulseekClient, + IConnectionWatchdog connectionWatchdog, IOptionsSnapshot optionsSnapshot, IStateSnapshot stateSnapshot) { Client = soulseekClient; + ConnectionWatchdog = connectionWatchdog; OptionsSnapshot = optionsSnapshot; StateSnapshot = stateSnapshot; } private ISoulseekClient Client { get; } + private IConnectionWatchdog ConnectionWatchdog { get; } private IOptionsSnapshot OptionsSnapshot { get; } private IStateSnapshot StateSnapshot { get; } @@ -58,22 +60,17 @@ namespace slskd.Core.API [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] [ProducesResponseType(403)] - public async Task Connect() + public IActionResult Connect() { if (Program.IsRelayAgent) { return Forbid(); } - if (!Client.State.HasFlag(SoulseekClientStates.Connected)) + // we use the watchdog here so that we get the correct auto-reconnect behavior + if (!ConnectionWatchdog.IsEnabled) { - var opt = OptionsSnapshot.Value.Soulseek; - - await Client.ConnectAsync( - address: opt.Address, - port: opt.Port, - username: opt.Username, - password: opt.Password); + ConnectionWatchdog.Start(); } return Ok(); @@ -98,6 +95,8 @@ namespace slskd.Core.API 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)); } -- cgit v1.2.3 From 5623053911f10b68d73fc38c9d0cc89f0ce81488 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 19 Jun 2025 12:27:07 -0500 Subject: abort reconnect when stopping via API --- src/slskd/Core/API/Controllers/ServerController.cs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index cfc695a0..aceaeec9 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -93,6 +93,9 @@ 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 -- cgit v1.2.3 From 7d302ce2a65bac34f0795ce5eafb3e06b746872d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 11 Jul 2025 17:52:57 -0500 Subject: return 205/ResetContent if retry loop is restarted by API call --- src/slskd/Core/API/Controllers/ServerController.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/ServerController.cs b/src/slskd/Core/API/Controllers/ServerController.cs index aceaeec9..da777b02 100644 --- a/src/slskd/Core/API/Controllers/ServerController.cs +++ b/src/slskd/Core/API/Controllers/ServerController.cs @@ -21,6 +21,7 @@ namespace slskd.Core.API { using Asp.Versioning; using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Soulseek; @@ -36,7 +37,7 @@ namespace slskd.Core.API { public ServerController( ISoulseekClient soulseekClient, - IConnectionWatchdog connectionWatchdog, + ConnectionWatchdog connectionWatchdog, IOptionsSnapshot optionsSnapshot, IStateSnapshot stateSnapshot) { @@ -47,7 +48,7 @@ namespace slskd.Core.API } private ISoulseekClient Client { get; } - private IConnectionWatchdog ConnectionWatchdog { get; } + private ConnectionWatchdog ConnectionWatchdog { get; } private IOptionsSnapshot OptionsSnapshot { get; } private IStateSnapshot StateSnapshot { get; } @@ -59,6 +60,7 @@ namespace slskd.Core.API [Route("")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(200)] + [ProducesResponseType(StatusCodes.Status205ResetContent)] [ProducesResponseType(403)] public IActionResult Connect() { @@ -67,10 +69,19 @@ namespace slskd.Core.API return Forbid(); } - // we use the watchdog here so that we get the correct auto-reconnect behavior + // 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) + { + ConnectionWatchdog.Restart(); + return StatusCode(StatusCodes.Status205ResetContent); + } + if (!ConnectionWatchdog.IsEnabled) { ConnectionWatchdog.Start(); + return Ok(); } return Ok(); -- cgit v1.2.3