From daeb177579b6759530a107f693c2559290a546c9 Mon Sep 17 00:00:00 2001 From: orignal Date: Tue, 28 Jan 2025 20:49:36 -0500 Subject: [PATCH] save updated local RouterInfo in separate thread --- libi2pd/RouterContext.cpp | 19 +++++++++++++++++-- libi2pd/RouterContext.h | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 030dc162a62..0c22ba45821 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -77,7 +77,9 @@ namespace i2p m_CongestionUpdateTimer->cancel (); m_Service->Stop (); CleanUp (); // GarlicDestination - } + } + if (m_SavingRouterInfo.valid ()) + m_SavingRouterInfo.wait (); } std::shared_ptr RouterContext::CopyRouterInfoBuffer () const @@ -254,12 +256,25 @@ namespace i2p void RouterContext::UpdateRouterInfo () { + std::shared_ptr buffer; { std::lock_guard 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 () diff --git a/libi2pd/RouterContext.h b/libi2pd/RouterContext.h index ae62ebf339c..29af6daceba 100644 --- a/libi2pd/RouterContext.h +++ b/libi2pd/RouterContext.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "Identity.h" @@ -266,6 +267,7 @@ namespace garlic bool m_IsHiddenMode; // not publish mutable std::mutex m_RouterInfoMutex; std::mt19937 m_Rng; + std::future m_SavingRouterInfo; }; extern RouterContext context;