persist local RouterInfo in separate thread using seperate buffer

This commit is contained in:
orignal 2025-01-29 13:22:15 -05:00
parent 93ec5ac5c4
commit e4ba07a540
2 changed files with 38 additions and 1 deletions

View file

@ -78,6 +78,12 @@ namespace i2p
m_Service->Stop ();
CleanUp (); // GarlicDestination
}
if (m_SavingRouterInfo.valid ())
{
m_SaveBuffer = nullptr;
m_SavingRouterInfo.get ();
}
}
std::shared_ptr<i2p::data::RouterInfo::Buffer> RouterContext::CopyRouterInfoBuffer () const
@ -254,11 +260,38 @@ namespace i2p
void RouterContext::UpdateRouterInfo ()
{
std::shared_ptr<i2p::data::RouterInfo::Buffer> buffer;
{
std::lock_guard<std::mutex> l(m_RouterInfoMutex);
m_RouterInfo.CreateBuffer (m_Keys);
buffer = m_RouterInfo.CopyBuffer ();
}
m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
{
// update save buffer to latest
std::lock_guard<std::mutex> l(m_SaveBufferMutex);
m_SaveBuffer = buffer;
}
bool isSaving = m_SavingRouterInfo.valid ();
if (isSaving && m_SavingRouterInfo.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
m_SavingRouterInfo.get ();
isSaving = false;
}
if (!isSaving) // try to save only if not being saved
m_SavingRouterInfo = std::async (std::launch::async, [this]()
{
std::shared_ptr<i2p::data::RouterInfo::Buffer> buffer;
while (m_SaveBuffer)
{
{
std::lock_guard<std::mutex> l(m_SaveBufferMutex);
buffer = m_SaveBuffer;
m_SaveBuffer = nullptr;
}
if (buffer)
i2p::data::RouterInfo::SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO), buffer);
}
});
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
}