Fix up client reconnect code

Simplified the reconnect code so that redis is made aware that we have a new connection.

Made the reconnect fire immediately on error rather than waiting for subsequent errors.
This commit is contained in:
2025-09-04 18:53:57 -04:00
parent 18da07763c
commit d8160effd0

View File

@@ -183,6 +183,8 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM
_connectionDto = await GetConnectionDto().ConfigureAwait(false); _connectionDto = await GetConnectionDto().ConfigureAwait(false);
await CheckClientHealth().ConfigureAwait(false);
ServerState = ServerState.Connected; ServerState = ServerState.Connected;
var currentClientVer = Assembly.GetExecutingAssembly().GetName().Version!; var currentClientVer = Assembly.GetExecutingAssembly().GetName().Version!;
@@ -315,13 +317,12 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM
{ {
await Task.Delay(TimeSpan.FromSeconds(30), ct).ConfigureAwait(false); await Task.Delay(TimeSpan.FromSeconds(30), ct).ConfigureAwait(false);
var healthy = await CheckClientHealth().ConfigureAwait(false); var healthy = await CheckClientHealth().ConfigureAwait(false);
Logger.LogDebug("Checking Client Health State returned {0} and hub connection {1}", healthy, _mareHub.State == HubConnectionState.Connected);
if (!healthy || _mareHub.State != HubConnectionState.Connected) if (!healthy || _mareHub.State != HubConnectionState.Connected)
{ {
_unhealthy++; _unhealthy++;
if (_unhealthy > 1) if (_unhealthy > 0)
{ {
Logger.LogWarning("Health check failed more than once, forcing reconnect."); Logger.LogWarning("Health check failed, forcing reconnect. ClientHealth: {0} HubConnected: {1}", healthy, _mareHub.State != HubConnectionState.Connected);
await ForceResetConnection().ConfigureAwait(false); await ForceResetConnection().ConfigureAwait(false);
_unhealthy = 0; _unhealthy = 0;
} }
@@ -402,7 +403,7 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM
{ {
var users = await GroupsGetUsersInGroup(group).ConfigureAwait(false); var users = await GroupsGetUsersInGroup(group).ConfigureAwait(false);
foreach (var user in users) foreach (var user in users)
{ {
Logger.LogDebug("Group Pair: {user}", user); Logger.LogDebug("Group Pair: {user}", user);
_pairManager.AddGroupPair(user); _pairManager.AddGroupPair(user);
} }
@@ -492,42 +493,27 @@ public sealed partial class ApiController : DisposableMediatorSubscriberBase, IM
//Because this plugin really likes to bug out with connections, lets "fix" it.... //Because this plugin really likes to bug out with connections, lets "fix" it....
public async Task ForceResetConnection() public async Task ForceResetConnection()
{ {
if (_mareHub == null || !_initialized) return; if (!_initialized) return;
Logger.LogInformation("ForceReconnect called"); Logger.LogInformation("ForceReconnect called");
ServerState = ServerState.Reconnecting;
try try
{ {
// Cancel previous health checks to avoid overlaps await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
// Cancel any ongoing health checks to prevent conflicts
_healthCheckTokenSource?.Cancel(); _healthCheckTokenSource?.Cancel();
_healthCheckTokenSource?.Dispose(); _healthCheckTokenSource?.Dispose();
_healthCheckTokenSource = null; _healthCheckTokenSource = null;
// Re-initialize hooks and refresh connection state await CreateConnections().ConfigureAwait(false);
InitializeApiHooks();
_connectionDto = await GetConnectionDto(publishConnected: false).ConfigureAwait(false);
if (_connectionDto.ServerVersion != IMareHub.ApiVersion)
{
await StopConnection(ServerState.VersionMisMatch).ConfigureAwait(false);
return;
}
ServerState = ServerState.Connected;
// Reload all pairs
await LoadIninitialPairs().ConfigureAwait(false);
await LoadOnlinePairs().ConfigureAwait(false);
Mediator.Publish(new ConnectedMessage(_connectionDto));
Logger.LogInformation("ForceReconnect completed successfully"); Logger.LogInformation("ForceReconnect completed successfully");
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogCritical(ex, "Failure during ForceReconnect, disconnecting"); Logger.LogError(ex, "Failure during ForceReconnect, disconnecting");
await StopConnection(ServerState.Disconnected).ConfigureAwait(false); await StopConnection(ServerState.Disconnected).ConfigureAwait(false);
} }
} }
} }
#pragma warning restore MA0040 #pragma warning restore MA0040