private async Task PruneZombieConnectionsAsync(IRealm realm) { var clientIds = realm.GetClientIds(); var now = DateTime.UtcNow; var aliveTimeout = TimeSpan.FromSeconds(60); var count = 0; foreach (var clientId in clientIds) { var client = realm.GetClient(clientId); var timeSinceLastHeartbeat = now - client.GetLastHeartbeat(); if (timeSinceLastHeartbeat < aliveTimeout) { continue; } var socket = client.GetSocket(); try { await socket?.CloseAsync($"Zombie connection, time since last heartbeat: {timeSinceLastHeartbeat.TotalSeconds}s"); } finally { realm.ClearMessageQueue(clientId); realm.RemoveClientById(clientId); socket?.Dispose(); } count++; } _logger.LogInformation($"Pruned zombie connections for {count} peers."); }