Skip to content

Commit

Permalink
use m_IsSaving flag for saving RouterInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Jan 29, 2025
1 parent e4ba07a commit adc230a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
22 changes: 7 additions & 15 deletions libi2pd/RouterContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace i2p
m_Error (eRouterErrorNone), m_ErrorV6 (eRouterErrorNone),
m_Testing (false), m_TestingV6 (false), m_NetID (I2PD_NET_ID),
m_PublishReplyToken (0), m_IsHiddenMode (false),
m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL)
m_Rng(i2p::util::GetMonotonicMicroseconds () % 1000000LL), m_IsSaving (false)
{
}

Expand Down Expand Up @@ -78,12 +78,6 @@ 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
Expand Down Expand Up @@ -271,14 +265,10 @@ namespace i2p
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]()
bool isSaving = false;
if (m_IsSaving.compare_exchange_strong (isSaving, true)) // try to save only if not being saved
{
auto savingRouterInfo = std::async (std::launch::async, [this]()
{
std::shared_ptr<i2p::data::RouterInfo::Buffer> buffer;
while (m_SaveBuffer)
Expand All @@ -291,7 +281,9 @@ namespace i2p
if (buffer)
i2p::data::RouterInfo::SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO), buffer);
}
m_IsSaving = false;
});
}
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
}

Expand Down
3 changes: 1 addition & 2 deletions libi2pd/RouterContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <string>
#include <memory>
#include <random>
#include <future>
#include <unordered_set>
#include <boost/asio.hpp>
#include "Identity.h"
Expand Down Expand Up @@ -267,9 +266,9 @@ namespace garlic
bool m_IsHiddenMode; // not publish
mutable std::mutex m_RouterInfoMutex;
std::mt19937 m_Rng;
std::future<void> m_SavingRouterInfo;
std::shared_ptr<i2p::data::RouterInfo::Buffer> m_SaveBuffer;
std::mutex m_SaveBufferMutex; // TODO: make m_SaveBuffer atomic
std::atomic<bool> m_IsSaving;
};

extern RouterContext context;
Expand Down

0 comments on commit adc230a

Please sign in to comment.