don't build exploratory selection on each request

gha
orignal 1 month ago
parent 13a746162a
commit 8a20d3219b

@ -38,7 +38,9 @@ namespace data
{ {
NetDb netdb; NetDb netdb;
NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr), m_Storage("netDb", "r", "routerInfo-", "dat"), m_PersistProfiles (true) NetDb::NetDb (): m_IsRunning (false), m_Thread (nullptr), m_Reseeder (nullptr),
m_Storage("netDb", "r", "routerInfo-", "dat"), m_PersistProfiles (true),
m_LastExploratorySelectionUpdateTime (0)
{ {
} }
@ -1094,7 +1096,7 @@ namespace data
excludedRouters.insert (excluded_ident); excludedRouters.insert (excluded_ident);
excluded_ident += 32; excluded_ident += 32;
} }
replyMsg = CreateDatabaseSearchReply (ident, GetClosestNonFloodfill (ident, replyMsg = CreateDatabaseSearchReply (ident, GetExploratoryNonFloodfill (ident,
NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES, excludedRouters)); NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES, excludedRouters));
} }
else else
@ -1451,32 +1453,38 @@ namespace data
}); });
} }
std::vector<IdentHash> NetDb::GetClosestNonFloodfill (const IdentHash& destination, std::vector<IdentHash> NetDb::GetExploratoryNonFloodfill (const IdentHash& destination,
size_t num, const std::set<IdentHash>& excluded) const size_t num, const std::set<IdentHash>& excluded)
{ {
std::vector<IdentHash> ret; std::vector<IdentHash> ret;
if (!num) return ret; // empty list if (!num) return ret; // empty list
// collect eligible auto ts = i2p::util::GetMonotonicSeconds ();
std::vector<std::shared_ptr<const RouterInfo> > eligible; if (ts > m_LastExploratorySelectionUpdateTime + NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL)
eligible.reserve (NETDB_MAX_EXPLORATORY_SELECTION_SIZE);
{ {
bool checkIsReal = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < NETDB_TUNNEL_CREATION_RATE_THRESHOLD; // too low rate // update selection
std::lock_guard<std::mutex> l(m_RouterInfosMutex); m_ExploratorySelection.clear ();
for (const auto& it: m_RouterInfos) {
if (!it.second->IsDeclaredFloodfill () && // collect eligible from current netdb
(!checkIsReal || (it.second->HasProfile () && it.second->GetProfile ()->IsReal ()))) bool checkIsReal = i2p::tunnel::tunnels.GetPreciseTunnelCreationSuccessRate () < NETDB_TUNNEL_CREATION_RATE_THRESHOLD; // too low rate
eligible.push_back (it.second); std::lock_guard<std::mutex> l(m_RouterInfosMutex);
} for (const auto& it: m_RouterInfos)
// reduce number of eligible routers if too many if (!it.second->IsDeclaredFloodfill () &&
if (eligible.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE) (!checkIsReal || (it.second->HasProfile () && it.second->GetProfile ()->IsReal ())))
{ m_ExploratorySelection.push_back (it.second);
std::shuffle (eligible.begin(), eligible.end(), std::mt19937(std::random_device()())); }
eligible.resize (NETDB_MAX_EXPLORATORY_SELECTION_SIZE); if (m_ExploratorySelection.size () > NETDB_MAX_EXPLORATORY_SELECTION_SIZE)
{
// reduce number of eligible to max selection size
std::shuffle (m_ExploratorySelection.begin(), m_ExploratorySelection.end(), std::mt19937(std::random_device()()));
m_ExploratorySelection.resize (NETDB_MAX_EXPLORATORY_SELECTION_SIZE);
}
m_LastExploratorySelectionUpdateTime = ts;
} }
// sort by distance // sort by distance
IdentHash destKey = CreateRoutingKey (destination); IdentHash destKey = CreateRoutingKey (destination);
std::map<XORMetric, std::shared_ptr<const RouterInfo> > sorted; std::map<XORMetric, std::shared_ptr<const RouterInfo> > sorted;
for (const auto& it: eligible) for (const auto& it: m_ExploratorySelection)
if (!excluded.count (it->GetIdentHash ())) if (!excluded.count (it->GetIdentHash ()))
sorted.emplace (destKey ^ it->GetIdentHash (), it); sorted.emplace (destKey ^ it->GetIdentHash (), it);
// return first num closest routers // return first num closest routers

@ -55,6 +55,7 @@ namespace data
const int NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51 const int NETDB_MIN_SHORT_TUNNEL_BUILD_VERSION = MAKE_VERSION_NUMBER(0, 9, 51); // 0.9.51
const size_t NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES = 16; const size_t NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES = 16;
const size_t NETDB_MAX_EXPLORATORY_SELECTION_SIZE = 500; const size_t NETDB_MAX_EXPLORATORY_SELECTION_SIZE = 500;
const int NETDB_EXPLORATORY_SELECTION_UPDATE_INTERVAL = 82; // in seconds
/** function for visiting a leaseset stored in a floodfill */ /** function for visiting a leaseset stored in a floodfill */
typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor; typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor;
@ -99,7 +100,7 @@ namespace data
std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const; std::shared_ptr<const RouterInfo> GetClosestFloodfill (const IdentHash& destination, const std::set<IdentHash>& excluded) const;
std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num, std::vector<IdentHash> GetClosestFloodfills (const IdentHash& destination, size_t num,
std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const; std::set<IdentHash>& excluded, bool closeThanUsOnly = false) const;
std::vector<IdentHash> GetClosestNonFloodfill (const IdentHash& destination, size_t num, const std::set<IdentHash>& excluded) const; std::vector<IdentHash> GetExploratoryNonFloodfill (const IdentHash& destination, size_t num, const std::set<IdentHash>& excluded);
std::shared_ptr<const RouterInfo> GetRandomRouterInFamily (FamilyID fam) const; std::shared_ptr<const RouterInfo> GetRandomRouterInFamily (FamilyID fam) const;
void SetUnreachable (const IdentHash& ident, bool unreachable); void SetUnreachable (const IdentHash& ident, bool unreachable);
void ExcludeReachableTransports (const IdentHash& ident, RouterInfo::CompatibleTransports transports); void ExcludeReachableTransports (const IdentHash& ident, RouterInfo::CompatibleTransports transports);
@ -190,6 +191,9 @@ namespace data
std::set<IdentHash> m_PublishExcluded; std::set<IdentHash> m_PublishExcluded;
uint32_t m_PublishReplyToken = 0; uint32_t m_PublishReplyToken = 0;
std::vector<std::shared_ptr<const RouterInfo> > m_ExploratorySelection;
uint64_t m_LastExploratorySelectionUpdateTime; // in monotonic seconds
i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool; i2p::util::MemoryPoolMt<RouterInfo::Buffer> m_RouterInfoBuffersPool;
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool; i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
i2p::util::MemoryPoolMt<RouterInfo::Addresses> m_RouterInfoAddressVectorsPool; i2p::util::MemoryPoolMt<RouterInfo::Addresses> m_RouterInfoAddressVectorsPool;

Loading…
Cancel
Save