private void HandleCleanupRegistry(CleanupRegistry message) { // Get instances of clients that have not been connected in 48 hours var oldClients = _clients.Values.Where(a => !a.Connected && a.LastHeartbeatReceivedAtUtc < DateTime.UtcNow.AddHours(48)).ToList(); foreach (var oldClient in oldClients) { _clients.Remove(oldClient.Id); } // Cleanup all clients heartbeat lists to only includ the last 50 foreach (var id in _clients.Keys) { UpdateClient(id, client => { client.Heartbeats = client.Heartbeats.Take(50).ToList(); }); } }