From 128a8f3b486c8ff803b7ce51b144694ffe343b7c Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 11 Apr 2015 15:39:23 -0400 Subject: [PATCH] delete obsolete profiles --- NetDb.cpp | 29 +++++++++++++++++------------ Profiling.cpp | 31 +++++++++++++++++++++++++++++-- Profiling.h | 3 +++ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/NetDb.cpp b/NetDb.cpp index 293905f9..39a5b4ff 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -65,19 +65,24 @@ namespace data void NetDb::Stop () { - for (auto it: m_RouterInfos) - it.second->SaveProfile (); - m_RouterInfos.clear (); - if (m_Thread) + if (m_IsRunning) { - m_IsRunning = false; - m_Queue.WakeUp (); - m_Thread->join (); - delete m_Thread; - m_Thread = 0; - } - m_LeaseSets.clear(); - m_Requests.Stop (); + for (auto it: m_RouterInfos) + it.second->SaveProfile (); + DeleteObsoleteProfiles (); + m_RouterInfos.clear (); + m_Floodfills.clear (); + if (m_Thread) + { + m_IsRunning = false; + m_Queue.WakeUp (); + m_Thread->join (); + delete m_Thread; + m_Thread = 0; + } + m_LeaseSets.clear(); + m_Requests.Stop (); + } } void NetDb::Run () diff --git a/Profiling.cpp b/Profiling.cpp index b572f3bb..027635ae 100644 --- a/Profiling.cpp +++ b/Profiling.cpp @@ -95,7 +95,7 @@ namespace data auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, ""); if (t.length () > 0) m_LastUpdateTime = boost::posix_time::time_from_string (t); - if ((GetTime () - m_LastUpdateTime).hours () < 72) // profile becomes obsolete after 3 days of inactivity + if ((GetTime () - m_LastUpdateTime).hours () < PEER_PROFILE_EXPIRATION_TIMEOUT) { // read participations auto participations = pt.get_child (PEER_PROFILE_SECTION_PARTICIPATION); @@ -133,7 +133,7 @@ namespace data if ((GetTime () - m_LastUpdateTime).total_seconds () < 900) // if less than 15 minutes return m_NumTunnelsAgreed < m_NumTunnelsDeclined; // 50% rate else - return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // 20% rate + return 3*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // 25% rate } bool RouterProfile::IsBad () const @@ -147,5 +147,32 @@ namespace data profile->Load (); // if possible return profile; } + + void DeleteObsoleteProfiles () + { + int num = 0; + auto ts = boost::posix_time::second_clock::local_time(); + boost::filesystem::path p (i2p::util::filesystem::GetDataDir()/PEER_PROFILES_DIRECTORY); + if (boost::filesystem::exists (p)) + { + boost::filesystem::directory_iterator end; + for (boost::filesystem::directory_iterator it (p); it != end; ++it) + { + if (boost::filesystem::is_directory (it->status())) + { + for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1) + { + auto lastModified = boost::posix_time::from_time_t (boost::filesystem::last_write_time (it1->path ())); + if ((ts - lastModified).hours () >= PEER_PROFILE_EXPIRATION_TIMEOUT) + { + boost::filesystem::remove (it1->path ()); + num++; + } + } + } + } + } + LogPrint (eLogInfo, num, " obsolete profiles deleted"); + } } } \ No newline at end of file diff --git a/Profiling.h b/Profiling.h index 9ff03f4b..d16f8954 100644 --- a/Profiling.h +++ b/Profiling.h @@ -18,6 +18,8 @@ namespace data const char PEER_PROFILE_PARTICIPATION_AGREED[] = "agreed"; const char PEER_PROFILE_PARTICIPATION_DECLINED[] = "declined"; const char PEER_PROFILE_PARTICIPATION_NON_REPLIED[] = "nonreplied"; + + const int PEER_PROFILE_EXPIRATION_TIMEOUT = 72; // in hours (3 days) class RouterProfile { @@ -54,6 +56,7 @@ namespace data }; std::shared_ptr GetRouterProfile (const IdentHash& identHash); + void DeleteObsoleteProfiles (); } }