Browse Source

use pointer to RouterInfo in SaveUpdated

pull/2108/head
orignal 1 month ago
parent
commit
0ccf0a6339
  1. 67
      libi2pd/NetDb.cpp
  2. 2
      libi2pd/RouterInfo.cpp

67
libi2pd/NetDb.cpp

@ -643,69 +643,68 @@ namespace data
std::list<std::string> removeFromDisk; std::list<std::string> removeFromDisk;
auto own = i2p::context.GetSharedRouterInfo (); auto own = i2p::context.GetSharedRouterInfo ();
for (auto& it: m_RouterInfos) for (auto [ident, r]: m_RouterInfos)
{ {
if (!it.second || it.second == own) continue; // skip own if (!r || r == own) continue; // skip own
if (it.second->IsBufferScheduledToDelete ()) // from previous SaveUpdated, we assume m_PersistingRouters complete if (r->IsBufferScheduledToDelete ()) // from previous SaveUpdated, we assume m_PersistingRouters complete
{ {
std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update
it.second->DeleteBuffer (); r->DeleteBuffer ();
} }
std::string ident = it.second->GetIdentHashBase64(); if (r->IsUpdated ())
if (it.second->IsUpdated ())
{ {
if (it.second->GetBuffer () && !it.second->IsUnreachable ()) if (r->GetBuffer () && !r->IsUnreachable ())
{ {
// we have something to save // we have something to save
std::shared_ptr<RouterInfo::Buffer> buffer; std::shared_ptr<RouterInfo::Buffer> buffer;
{ {
std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update std::lock_guard<std::mutex> l(m_RouterInfosMutex); // possible collision between DeleteBuffer and Update
buffer = it.second->CopyBuffer (); buffer = r->CopyBuffer ();
it.second->ScheduleBufferToDelete (); r->ScheduleBufferToDelete ();
} }
if (buffer) if (buffer)
saveToDisk.push_back(std::make_pair(ident, buffer)); saveToDisk.push_back(std::make_pair(ident.ToBase64 (), buffer));
} }
it.second->SetUpdated (false); r->SetUpdated (false);
updatedCount++; updatedCount++;
continue; continue;
} }
if (it.second->GetProfile ()->IsUnreachable ()) if (r->GetProfile ()->IsUnreachable ())
it.second->SetUnreachable (true); r->SetUnreachable (true);
// make router reachable back if too few routers or floodfills // make router reachable back if too few routers or floodfills
if (it.second->IsUnreachable () && (total - deletedCount < NETDB_MIN_ROUTERS || isLowRate || if (r->IsUnreachable () && (total - deletedCount < NETDB_MIN_ROUTERS || isLowRate ||
(it.second->IsFloodfill () && totalFloodfills - deletedFloodfillsCount < NETDB_MIN_FLOODFILLS))) (r->IsFloodfill () && totalFloodfills - deletedFloodfillsCount < NETDB_MIN_FLOODFILLS)))
it.second->SetUnreachable (false); r->SetUnreachable (false);
if (!it.second->IsUnreachable ()) if (!r->IsUnreachable ())
{ {
// find & mark expired routers // find & mark expired routers
if (!it.second->GetCompatibleTransports (true)) // non reachable by any transport if (!r->GetCompatibleTransports (true)) // non reachable by any transport
it.second->SetUnreachable (true); r->SetUnreachable (true);
else if (ts + NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < it.second->GetTimestamp ()) else if (ts + NETDB_EXPIRATION_TIMEOUT_THRESHOLD*1000LL < r->GetTimestamp ())
{ {
LogPrint (eLogWarning, "NetDb: RouterInfo is from future for ", (it.second->GetTimestamp () - ts)/1000LL, " seconds"); LogPrint (eLogWarning, "NetDb: RouterInfo is from future for ", (r->GetTimestamp () - ts)/1000LL, " seconds");
it.second->SetUnreachable (true); r->SetUnreachable (true);
} }
else if (checkForExpiration) else if (checkForExpiration)
{ {
if (ts > it.second->GetTimestamp () + expirationTimeout) if (ts > r->GetTimestamp () + expirationTimeout)
it.second->SetUnreachable (true); r->SetUnreachable (true);
else if ((ts > it.second->GetTimestamp () + expirationTimeout/2) && // more than half of expiration else if ((ts > r->GetTimestamp () + expirationTimeout/2) && // more than half of expiration
total > NETDB_NUM_ROUTERS_THRESHOLD && !it.second->IsHighBandwidth() && // low bandwidth total > NETDB_NUM_ROUTERS_THRESHOLD && !r->IsHighBandwidth() && // low bandwidth
!it.second->IsFloodfill() && (!i2p::context.IsFloodfill () || // non floodfill !r->IsFloodfill() && (!i2p::context.IsFloodfill () || // non floodfill
(CreateRoutingKey (it.second->GetIdentHash ()) ^ i2p::context.GetIdentHash ()).metric[0] >= 0x02)) // different first 7 bits (CreateRoutingKey (ident) ^ i2p::context.GetIdentHash ()).metric[0] >= 0x02)) // different first 7 bits
it.second->SetUnreachable (true); r->SetUnreachable (true);
} }
} }
// make router reachable back if connected now // make router reachable back if connected now
if (it.second->IsUnreachable () && i2p::transport::transports.IsConnected (it.second->GetIdentHash ())) if (r->IsUnreachable () && i2p::transport::transports.IsConnected (ident))
it.second->SetUnreachable (false); r->SetUnreachable (false);
if (it.second->IsUnreachable ()) if (r->IsUnreachable ())
{ {
if (it.second->IsFloodfill ()) deletedFloodfillsCount++; if (r->IsFloodfill ()) deletedFloodfillsCount++;
// delete RI file // delete RI file
removeFromDisk.push_back (ident); removeFromDisk.push_back (ident.ToBase64());
deletedCount++; deletedCount++;
if (total - deletedCount < NETDB_MIN_ROUTERS) checkForExpiration = false; if (total - deletedCount < NETDB_MIN_ROUTERS) checkForExpiration = false;
} }

2
libi2pd/RouterInfo.cpp

@ -56,7 +56,7 @@ namespace data
RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len): RouterInfo::RouterInfo (std::shared_ptr<Buffer>&& buf, size_t len):
m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false), m_IsFloodfill (false), m_FamilyID (0), m_IsUpdated (true), m_IsUnreachable (false), m_IsFloodfill (false),
m_SupportedTransports (0), m_ReachableTransports (0), m_PublishedTransports (0), m_IsBufferScheduledToDelete (false), m_SupportedTransports (0), m_ReachableTransports (0), m_PublishedTransports (0),
m_Caps (0), m_Version (0), m_Congestion (eLowCongestion) m_Caps (0), m_Version (0), m_Congestion (eLowCongestion)
{ {
if (len <= MAX_RI_BUFFER_SIZE) if (len <= MAX_RI_BUFFER_SIZE)

Loading…
Cancel
Save