From 6c79ccd1dc6a4879c6d634851356b4b5f26d9be9 Mon Sep 17 00:00:00 2001 From: hakunamtu Date: Sun, 20 Jan 2019 21:49:26 +0300 Subject: [PATCH] Periodic obsolete profiles cleanup --- libi2pd/NetDb.cpp | 22 +++++++++++++++++++++- libi2pd/NetDb.hpp | 1 + libi2pd/Profiling.h | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index 7a54e606..59964377 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -82,7 +82,7 @@ namespace data void NetDb::Run () { - uint32_t lastSave = 0, lastLeasesetsManage = 0, lastPublish = 0, lastExploratory = 0, lastManageRequest = 0, lastDestinationCleanup = 0; + uint32_t lastSave = 0, lastLeasesetsManage = 0, lastPublish = 0, lastExploratory = 0, lastManageRequest = 0, lastDestinationCleanup = 0, lastProfilesCleanup = 0; while (m_IsRunning) { try @@ -171,6 +171,11 @@ namespace data lastExploratory = ts; } } + if (ts - lastProfilesCleanup >= PEER_PROFILE_EXPIRATION_TIMEOUT * 3600) + { + RemoveObsoleteProfiles (); + lastProfilesCleanup = ts; + } } catch (std::exception& ex) { @@ -607,6 +612,21 @@ namespace data } } + void NetDb::RemoveObsoleteProfiles () + { + DeleteObsoleteProfiles (); + auto now = boost::posix_time::second_clock::local_time (); + { + std::unique_lock l(m_UnsavedProfilesMutex); + for (auto it = m_UnsavedProfiles.begin (); it != m_UnsavedProfiles.end ();) + if ((now - it->second->GetLastUpdateTime ()).hours () >= PEER_PROFILE_EXPIRATION_TIMEOUT) + it = m_UnsavedProfiles.erase (it); + else + ++it; + } + LogPrint (eLogInfo, "NetDb: obsolete profiles were removed"); + } + void NetDb::RequestDestination (const IdentHash& destination, RequestedDestination::RequestComplete requestComplete) { auto dest = m_Requests.CreateRequest (destination, false, requestComplete); // non-exploratory diff --git a/libi2pd/NetDb.hpp b/libi2pd/NetDb.hpp index 3e814746..568899c1 100644 --- a/libi2pd/NetDb.hpp +++ b/libi2pd/NetDb.hpp @@ -110,6 +110,7 @@ namespace data bool LoadRouterInfo (const std::string & path); void SaveUpdated (); void RemoveExpired (); + void RemoveObsoleteProfiles (); void Run (); // exploratory thread void Explore (int numDestinations); void Publish (); diff --git a/libi2pd/Profiling.h b/libi2pd/Profiling.h index 4ba6702f..708cae45 100644 --- a/libi2pd/Profiling.h +++ b/libi2pd/Profiling.h @@ -29,6 +29,8 @@ namespace data RouterProfile (); RouterProfile& operator= (const RouterProfile& ) = default; + boost::posix_time::ptime GetLastUpdateTime () const { return m_LastUpdateTime; }; + void Save (const IdentHash& identHash); void Load (const IdentHash& identHash);