|
@ -73,30 +73,32 @@ public class ConnectionHealthCheckService : BackgroundService |
|
|
// 检查连接是否超时
|
|
|
// 检查连接是否超时
|
|
|
if (now - connection.LastActivityTime > _inactivityTimeout) |
|
|
if (now - connection.LastActivityTime > _inactivityTimeout) |
|
|
{ |
|
|
{ |
|
|
_logger.LogWarning("连接超时,连接ID:{ConnectionId},最后活动时间:{LastActivityTime}", |
|
|
_logger.LogWarning("连接超时,连接ID:{ConnectionId},最后活动时间:{LastActivityTime},超时时间:{Timeout}秒", |
|
|
connection.Id, connection.LastActivityTime); |
|
|
connection.Id, connection.LastActivityTime, _inactivityTimeout.TotalSeconds); |
|
|
inactiveConnections.Add(connection.Id); |
|
|
// inactiveConnections.Add(connection.Id); // 暂时注释掉,不清理超时连接
|
|
|
continue; |
|
|
continue; // 暂时注释掉,继续检查其他状态
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查连接是否正在处理
|
|
|
// 检查连接是否正在处理
|
|
|
if (_coordinator.IsConnectionProcessing(connection.Id)) |
|
|
if (_coordinator.IsConnectionProcessing(connection.Id)) |
|
|
{ |
|
|
{ |
|
|
_logger.LogDebug("连接正在处理中,跳过检查,连接ID:{ConnectionId}", connection.Id); |
|
|
_logger.LogDebug("连接正在处理中,跳过检查,连接ID:{ConnectionId},最后活动时间:{LastActivityTime}", |
|
|
|
|
|
connection.Id, connection.LastActivityTime); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 检查连接状态
|
|
|
// 检查连接状态
|
|
|
if (connection.Socket.State != System.Net.WebSockets.WebSocketState.Open) |
|
|
if (connection.Socket.State != System.Net.WebSockets.WebSocketState.Open) |
|
|
{ |
|
|
{ |
|
|
_logger.LogWarning("连接状态异常,连接ID:{ConnectionId},状态:{State}", |
|
|
_logger.LogWarning("连接状态异常,连接ID:{ConnectionId},状态:{State},最后活动时间:{LastActivityTime}", |
|
|
connection.Id, connection.Socket.State); |
|
|
connection.Id, connection.Socket.State, connection.LastActivityTime); |
|
|
inactiveConnections.Add(connection.Id); |
|
|
inactiveConnections.Add(connection.Id); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
_logger.LogError(ex, "检查连接时发生错误,连接ID:{ConnectionId}", connection.Id); |
|
|
_logger.LogError(ex, "检查连接时发生错误,连接ID:{ConnectionId},最后活动时间:{LastActivityTime},连接状态:{State}", |
|
|
|
|
|
connection.Id, connection.LastActivityTime, connection.Socket.State); |
|
|
inactiveConnections.Add(connection.Id); |
|
|
inactiveConnections.Add(connection.Id); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -107,16 +109,17 @@ public class ConnectionHealthCheckService : BackgroundService |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
await connectionManager.RemoveConnectionAsync(connectionId); |
|
|
await connectionManager.RemoveConnectionAsync(connectionId); |
|
|
_logger.LogInformation("已清理不活跃连接,连接ID:{ConnectionId}", connectionId); |
|
|
_logger.LogInformation("已清理不活跃连接,连接ID:{ConnectionId},清理原因:连接状态异常或检查出错", connectionId); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
_logger.LogError(ex, "清理连接时发生错误,连接ID:{ConnectionId}", connectionId); |
|
|
_logger.LogError(ex, "清理连接时发生错误,连接ID:{ConnectionId},错误详情:{ErrorMessage}", |
|
|
|
|
|
connectionId, ex.Message); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
_logger.LogInformation("连接健康检查完成,检查连接数:{TotalConnections},清理连接数:{CleanedConnections}", |
|
|
_logger.LogInformation("连接健康检查完成,检查连接数:{TotalConnections},清理连接数:{CleanedConnections},检查时间:{CheckTime}", |
|
|
connections.Count(), inactiveConnections.Count); |
|
|
connections.Count(), inactiveConnections.Count, now.ToString("yyyy-MM-dd HH:mm:ss")); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public override void Dispose() |
|
|
public override void Dispose() |
|
|