From a4762fe65cf6e7b760ef7c3fdb90bb4d0f29004b Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 8 Sep 2016 10:46:13 -0400 Subject: [PATCH] remove expired session through one pass --- Datagram.cpp | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index fa05f5a9..53b13d50 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -117,26 +117,21 @@ namespace datagram } void DatagramDestination::CleanUp () - { - std::vector expiredSessions; + { + auto now = i2p::util::GetMillisecondsSinceEpoch(); + LogPrint(eLogDebug, "DatagramDestination: clean up sessions"); + std::lock_guard lock(m_SessionsMutex); + // for each session ... + for (auto it = m_Sessions.begin (); it != m_Sessions.end (); ) { - std::lock_guard lock(m_SessionsMutex); - auto now = i2p::util::GetMillisecondsSinceEpoch(); - LogPrint(eLogDebug, "DatagramDestination: clean up sessions"); - // for each session ... - for (auto & e : m_Sessions) + // check if expired + if (now - it->second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE) { - // check if expired - if(now - e.second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE) - expiredSessions.push_back(e.first); // we are expired + LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", it->first.ToBase32()); + it = m_Sessions.erase (it); // we are expired } - } - // for each expired session ... - for (auto & ident : expiredSessions) - { - // remove the expired session - LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", ident.ToBase32()); - m_Sessions.erase(ident); + else + it++; } }