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