remove expired session through one pass

This commit is contained in:
orignal 2016-09-08 10:46:13 -04:00
parent bee407ea34
commit a4762fe65c

View file

@ -118,25 +118,20 @@ namespace datagram
void DatagramDestination::CleanUp () void DatagramDestination::CleanUp ()
{ {
std::vector<i2p::data::IdentHash> expiredSessions;
{
std::lock_guard<std::mutex> lock(m_SessionsMutex);
auto now = i2p::util::GetMillisecondsSinceEpoch(); auto now = i2p::util::GetMillisecondsSinceEpoch();
LogPrint(eLogDebug, "DatagramDestination: clean up sessions"); LogPrint(eLogDebug, "DatagramDestination: clean up sessions");
std::lock_guard<std::mutex> lock(m_SessionsMutex);
// for each session ... // for each session ...
for (auto & e : m_Sessions) for (auto it = m_Sessions.begin (); it != m_Sessions.end (); )
{ {
// check if expired // check if expired
if(now - e.second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE) if (now - it->second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE)
expiredSessions.push_back(e.first); // we are expired
}
}
// for each expired session ...
for (auto & ident : expiredSessions)
{ {
// remove the expired session LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", it->first.ToBase32());
LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", ident.ToBase32()); it = m_Sessions.erase (it); // we are expired
m_Sessions.erase(ident); }
else
it++;
} }
} }