private void HousekeepConnections(object notUsed) { // calls are accumulative, not discrete if (Interlocked.Decrement(ref _housekeepCallsPart1) > 0) { return; } try { DateTimeOffset dtCommenced = DateTimeOffset.Now; int activeCount = 0; var expiredConns = new List <IConnection>(); foreach (IConnection connection in _connectionIndex.GetValues()) { bool removeConnection = false; if (connection.HasFaulted) { removeConnection = true; Logger.LogDebug("Connection: '{0}' faulted ({1})", connection.ClientId, connection.ReplyAddress); } if (connection.HasExpired) { removeConnection = true; Logger.LogDebug("Connection: '{0}' expired ({1})", connection.ClientId, connection.ReplyAddress); } if (removeConnection) { // client not found or expired expiredConns.Add(connection); } else { activeCount++; } } foreach (IConnection connection in expiredConns) { _connectionIndex.Remove(connection.ClientId); _cacheEngine.DeleteConnectionState(connection.ClientId); } DateTimeOffset dtCompleted = DateTimeOffset.Now; TimeSpan duration = dtCompleted - dtCommenced; Logger.LogDebug("---------- Housekeep Connections ----------"); Logger.LogDebug("Connections"); Logger.LogDebug(" Active : {0}", activeCount); Logger.LogDebug(" Expired : {0}", expiredConns.Count); Logger.LogDebug("Duration : {0}s", duration.TotalSeconds); Logger.LogDebug("---------- Housekeep Connections ----------"); } catch (Exception e) { Logger.LogError("HousekeepConnections failed: {0}", e); } // ---------------------------------------- next part ---------------------------------------- //Interlocked.Increment(ref _HousekeepCallsPart2); //_MainCommsDispatcher.Dispatch<object>(null, HousekeeperPart2); }