fix reconnection issues maybe

This commit is contained in:
rootdarkarchon
2024-01-27 23:08:15 +01:00
parent 22c2cad83e
commit 4632a256bd

View File

@@ -14,12 +14,14 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using StackExchange.Redis.Extensions.Core.Abstractions; using StackExchange.Redis.Extensions.Core.Abstractions;
using System.Collections.Concurrent;
namespace MareSynchronosServer.Hubs; namespace MareSynchronosServer.Hubs;
[Authorize(Policy = "Authenticated")] [Authorize(Policy = "Authenticated")]
public partial class MareHub : Hub<IMareHub>, IMareHub public partial class MareHub : Hub<IMareHub>, IMareHub
{ {
private static readonly ConcurrentDictionary<string, string> _userConnections = new(StringComparer.Ordinal);
private readonly MareMetrics _mareMetrics; private readonly MareMetrics _mareMetrics;
private readonly SystemInfoService _systemInfoService; private readonly SystemInfoService _systemInfoService;
private readonly IHttpContextAccessor _contextAccessor; private readonly IHttpContextAccessor _contextAccessor;
@@ -132,15 +134,27 @@ public partial class MareHub : Hub<IMareHub>, IMareHub
[Authorize(Policy = "Authenticated")] [Authorize(Policy = "Authenticated")]
public override async Task OnConnectedAsync() public override async Task OnConnectedAsync()
{ {
_mareMetrics.IncGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent); if (_userConnections.TryGetValue(UserUID, out var oldId))
try
{ {
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent)); _logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), "UpdatingId", oldId, Context.ConnectionId));
await _onlineSyncedPairCacheService.InitPlayer(UserUID).ConfigureAwait(false); _userConnections[UserUID] = Context.ConnectionId;
await UpdateUserOnRedis().ConfigureAwait(false); }
else
{
_mareMetrics.IncGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent);
try
{
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, UserCharaIdent));
await _onlineSyncedPairCacheService.InitPlayer(UserUID).ConfigureAwait(false);
await UpdateUserOnRedis().ConfigureAwait(false);
_userConnections[UserUID] = Context.ConnectionId;
}
catch
{
_userConnections.Remove(UserUID, out _);
}
} }
catch { }
await base.OnConnectedAsync().ConfigureAwait(false); await base.OnConnectedAsync().ConfigureAwait(false);
} }
@@ -148,27 +162,39 @@ 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)
{ {
_mareMetrics.DecGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent); if (_userConnections.TryGetValue(UserUID, out var connectionId)
&& string.Equals(connectionId, Context.ConnectionId, StringComparison.Ordinal))
try
{ {
await _onlineSyncedPairCacheService.DisposePlayer(UserUID).ConfigureAwait(false); _mareMetrics.DecGaugeWithLabels(MetricsAPI.GaugeConnections, labels: Continent);
_logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), UserCharaIdent)); try
if (exception != null) {
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), exception.Message, exception.StackTrace)); await _onlineSyncedPairCacheService.DisposePlayer(UserUID).ConfigureAwait(false);
await RemoveUserFromRedis().ConfigureAwait(false); _logger.LogCallInfo(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, UserCharaIdent));
if (exception != null)
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), Context.ConnectionId, exception.Message, exception.StackTrace));
_mareCensus.ClearStatistics(UserUID); await RemoveUserFromRedis().ConfigureAwait(false);
await SendOfflineToAllPairedUsers().ConfigureAwait(false); _mareCensus.ClearStatistics(UserUID);
DbContext.RemoveRange(DbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == UserUID)); await SendOfflineToAllPairedUsers().ConfigureAwait(false);
await DbContext.SaveChangesAsync().ConfigureAwait(false);
DbContext.RemoveRange(DbContext.Files.Where(f => !f.Uploaded && f.UploaderUID == UserUID));
await DbContext.SaveChangesAsync().ConfigureAwait(false);
}
catch { }
finally
{
_userConnections.Remove(UserUID, out _);
}
}
else
{
_logger.LogCallWarning(MareHubLogger.Args(_contextAccessor.GetIpAddress(), "ObsoleteId", UserUID, Context.ConnectionId));
} }
catch { }
await base.OnDisconnectedAsync(exception).ConfigureAwait(false); await base.OnDisconnectedAsync(exception).ConfigureAwait(false);
} }