void housekeepSessions() { lock (lockObject) { List <Session> sessToDel = new List <Session>(); foreach (var sess in sessions) { // Requested too long ago, and not claimed yet if (sess.RequestedUtc != DateTime.MinValue && DateTime.UtcNow.Subtract(sess.RequestedUtc).TotalSeconds > options.SessionRequestExpirySeconds) { sessToDel.Add(sess); } // Inactive for too long else if (DateTime.UtcNow.Subtract(sess.LastActiveUtc).TotalSeconds > options.SessionIdleEndSeconds) { sessToDel.Add(sess); } } List <string> sessionKeysToTerminate = new List <string>(); foreach (var sess in sessToDel) { sessions.Remove(sess); if (sess.RequestedUtc == DateTime.MinValue) { sessionKeysToTerminate.Add(sess.SessionKey); } } Broadcaster.TerminateSessions(sessionKeysToTerminate); } }