Fix health checks and add check for reconnects

Allows the client to auto reconnect when they encountered a connection issue.
This commit is contained in:
2025-09-04 17:15:54 -04:00
parent ef39da41c4
commit ef4c463471

View File

@@ -102,11 +102,17 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
[Authorize(Policy = "Authenticated")] [Authorize(Policy = "Authenticated")]
public async Task<bool> CheckClientHealth() public async Task<bool> CheckClientHealth()
{
try
{ {
await UpdateUserOnRedis().ConfigureAwait(false); await UpdateUserOnRedis().ConfigureAwait(false);
return true;
}
catch
{
return false; return false;
} }
}
[Authorize(Policy = "Authenticated")] [Authorize(Policy = "Authenticated")]
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
@@ -127,9 +133,14 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
[Authorize(Policy = "Authenticated")] [Authorize(Policy = "Authenticated")]
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
DateTime _lastdc = DateTime.UtcNow;
_mareMetrics.DecGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent); _mareMetrics.DecGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent);
try try
{
// Check DB to see if the user reconnected after this disconnect
await Task.Delay(TimeSpan.FromSeconds(7)).ConfigureAwait(false);
var dbUser = await DbContext.Users.AsNoTracking().SingleOrDefaultAsync(u => u.UID == UserUID).ConfigureAwait(false);
if (dbUser != null && dbUser.LastLoggedIn < _lastdc)
{ {
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent)); _logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent));
if (exception != null) if (exception != null)
@@ -140,6 +151,7 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
await SendOfflineToAllPairedUsers().ConfigureAwait(false); await SendOfflineToAllPairedUsers().ConfigureAwait(false);
} }
}
catch { } catch { }
await base.OnDisconnectedAsync(exception).ConfigureAwait(false); await base.OnDisconnectedAsync(exception).ConfigureAwait(false);