From bee407ea34edc1fe7f0939396267d7dac5131402 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 8 Sep 2016 10:16:42 -0400 Subject: [PATCH] clean-up datagram session toghters with leasesets and tags --- Datagram.cpp | 40 +++++++++++++++------------------------- Datagram.h | 14 ++++---------- Destination.cpp | 7 +++++++ Destination.h | 2 ++ 4 files changed, 28 insertions(+), 35 deletions(-) diff --git a/Datagram.cpp b/Datagram.cpp index cecbcc40..fa05f5a9 100644 --- a/Datagram.cpp +++ b/Datagram.cpp @@ -12,16 +12,12 @@ namespace i2p namespace datagram { DatagramDestination::DatagramDestination (std::shared_ptr owner): - m_Owner (owner.get()), - m_CleanupTimer(owner->GetService()), - m_Receiver (nullptr) + m_Owner (owner.get()), m_Receiver (nullptr) { - ScheduleCleanup(); } DatagramDestination::~DatagramDestination () { - m_CleanupTimer.cancel(); m_Sessions.clear(); } @@ -120,34 +116,28 @@ namespace datagram return msg; } - void DatagramDestination::ScheduleCleanup() + void DatagramDestination::CleanUp () { - m_CleanupTimer.expires_from_now(boost::posix_time::seconds(DATAGRAM_SESSION_CLEANUP_INTERVAL)); - m_CleanupTimer.async_wait(std::bind(&DatagramDestination::HandleCleanUp, this, std::placeholders::_1)); - } - - void DatagramDestination::HandleCleanUp(const boost::system::error_code & ecode) - { - if(ecode) - return; - std::lock_guard lock(m_SessionsMutex); - auto now = i2p::util::GetMillisecondsSinceEpoch(); - LogPrint(eLogDebug, "DatagramDestination: clean up sessions"); std::vector expiredSessions; - // for each session ... - for (auto & e : m_Sessions) { - // check if expired - if(now - e.second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE) - expiredSessions.push_back(e.first); // we are expired + { + 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 - e.second->LastActivity() >= DATAGRAM_SESSION_MAX_IDLE) + expiredSessions.push_back(e.first); // we are expired + } } // for each expired session ... - for (auto & ident : expiredSessions) { + for (auto & ident : expiredSessions) + { // remove the expired session LogPrint(eLogInfo, "DatagramDestination: expiring idle session with ", ident.ToBase32()); m_Sessions.erase(ident); } - m_Owner->CleanupExpiredTags(); - ScheduleCleanup(); } std::shared_ptr DatagramDestination::ObtainSession(const i2p::data::IdentHash & ident) diff --git a/Datagram.h b/Datagram.h index 06b3048e..f3aae6f9 100644 --- a/Datagram.h +++ b/Datagram.h @@ -19,9 +19,6 @@ namespace client } namespace datagram { - - // seconds interval for cleanup timer - const int DATAGRAM_SESSION_CLEANUP_INTERVAL = 3; // milliseconds for max session idle time const uint64_t DATAGRAM_SESSION_MAX_IDLE = 10 * 60 * 1000; // milliseconds for how long we try sticking to a dead routing path before trying to switch @@ -127,13 +124,11 @@ namespace datagram std::shared_ptr GetInfoForRemote(const i2p::data::IdentHash & remote); + // clean up stale sessions + void CleanUp (); + private: - // clean up after next tick - void ScheduleCleanup(); - - // clean up stale sessions and expire tags - void HandleCleanUp(const boost::system::error_code & ecode); - + std::shared_ptr ObtainSession(const i2p::data::IdentHash & ident); std::shared_ptr CreateDataMessage (const uint8_t * payload, size_t len, uint16_t fromPort, uint16_t toPort); @@ -145,7 +140,6 @@ namespace datagram private: i2p::client::ClientDestination * m_Owner; - boost::asio::deadline_timer m_CleanupTimer; Receiver m_Receiver; // default std::mutex m_SessionsMutex; std::map > m_Sessions; diff --git a/Destination.cpp b/Destination.cpp index 03d0dd69..48717f5f 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -641,6 +641,7 @@ namespace client { CleanupExpiredTags (); CleanupRemoteLeaseSets (); + CleanupDestination (); m_CleanupTimer.expires_from_now (boost::posix_time::minutes (DESTINATION_CLEANUP_TIMEOUT)); m_CleanupTimer.async_wait (std::bind (&LeaseSetDestination::HandleCleanupTimer, shared_from_this (), std::placeholders::_1)); @@ -892,5 +893,11 @@ namespace client Sign (leaseSet->GetBuffer (), leaseSet->GetBufferLen () - leaseSet->GetSignatureLen (), leaseSet->GetSignature ()); // TODO SetLeaseSet (leaseSet); } + + void ClientDestination::CleanupDestination () + { + if (m_DatagramDestination) m_DatagramDestination->CleanUp (); + } + } } diff --git a/Destination.h b/Destination.h index 8150c72d..22ffa603 100644 --- a/Destination.h +++ b/Destination.h @@ -96,6 +96,7 @@ namespace client protected: void SetLeaseSet (i2p::data::LocalLeaseSet * newLeaseSet); + virtual void CleanupDestination () {}; // additional clean up in derived classes // I2CP virtual void HandleDataMessage (const uint8_t * buf, size_t len) = 0; virtual void CreateNewLeaseSet (std::vector > tunnels) = 0; @@ -180,6 +181,7 @@ namespace client protected: + void CleanupDestination (); // I2CP void HandleDataMessage (const uint8_t * buf, size_t len); void CreateNewLeaseSet (std::vector > tunnels);