Skip to content

Commit

Permalink
save updated local RouterInfo in separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 29, 2025
1 parent 5d7a062 commit daeb177
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ namespace i2p
m_CongestionUpdateTimer->cancel ();
m_Service->Stop ();
CleanUp (); // GarlicDestination
}
}
if (m_SavingRouterInfo.valid ())
m_SavingRouterInfo.wait ();
}

std::shared_ptr<i2p::data::RouterInfo::Buffer> RouterContext::CopyRouterInfoBuffer () const
Expand Down Expand Up @@ -254,12 +256,25 @@ 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));
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
// defer saving buffer to disk
if (m_SavingRouterInfo.valid ())
{
// wait until previous update complete
m_SavingRouterInfo.wait ();
m_SavingRouterInfo.get ();
}
m_SavingRouterInfo = std::async (std::launch::async,
[buffer = std::move(buffer)]()
{
i2p::data::RouterInfo::SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO), buffer);
});
}

void RouterContext::NewNTCP2Keys ()
Expand Down
2 changes: 2 additions & 0 deletions libi2pd/RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <memory>
#include <random>
#include <future>
#include <unordered_set>
#include <boost/asio.hpp>
#include "Identity.h"
Expand Down Expand Up @@ -266,6 +267,7 @@ namespace garlic
bool m_IsHiddenMode; // not publish
mutable std::mutex m_RouterInfoMutex;
std::mt19937 m_Rng;
std::future<void> m_SavingRouterInfo;
};

extern RouterContext context;
Expand Down

0 comments on commit daeb177

Please sign in to comment.