diff --git a/libi2pd/Profiling.cpp b/libi2pd/Profiling.cpp index 4f032326..311d1c86 100644 --- a/libi2pd/Profiling.cpp +++ b/libi2pd/Profiling.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -237,28 +238,37 @@ namespace data void PersistProfiles () { auto ts = GetTime (); - std::unique_lock l(g_ProfilesMutex); - for (auto it = g_Profiles.begin (); it != g_Profiles.end ();) - { - if ((ts - it->second->GetLastUpdateTime ()).total_seconds () > PEER_PROFILE_PERSIST_INTERVAL) - { - if (it->second->IsUpdated ()) - it->second->Save (it->first); - it = g_Profiles.erase (it); - } - else - it++; - } + std::list > > tmp; + { + std::unique_lock l(g_ProfilesMutex); + for (auto it = g_Profiles.begin (); it != g_Profiles.end ();) + { + if ((ts - it->second->GetLastUpdateTime ()).total_seconds () > PEER_PROFILE_PERSIST_INTERVAL) + { + if (it->second->IsUpdated ()) + tmp.push_back (std::make_pair (it->first, it->second)); + it = g_Profiles.erase (it); + } + else + it++; + } + } + for (auto& it: tmp) + if (it.second) it.second->Save (it.first); } void SaveProfiles () { + std::unordered_map > tmp; + { + std::unique_lock l(g_ProfilesMutex); + tmp = g_Profiles; + g_Profiles.clear (); + } auto ts = GetTime (); - std::unique_lock l(g_ProfilesMutex); - for (auto it: g_Profiles) + for (auto& it: tmp) if (it.second->IsUpdated () && (ts - it.second->GetLastUpdateTime ()).total_seconds () < PEER_PROFILE_EXPIRATION_TIMEOUT*3600) it.second->Save (it.first); - g_Profiles.clear (); } void DeleteObsoleteProfiles ()