Browse Source

request newly discovered routers with random intervals after exploratory

pull/2072/head
orignal 6 months ago
parent
commit
bb531a878d
  1. 61
      libi2pd/NetDbRequests.cpp
  2. 9
      libi2pd/NetDbRequests.h

61
libi2pd/NetDbRequests.cpp

@ -7,7 +7,6 @@
*/ */
#include "Log.h" #include "Log.h"
#include "Base.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "Transports.h" #include "Transports.h"
#include "NetDb.hpp" #include "NetDb.hpp"
@ -100,7 +99,7 @@ namespace data
NetDbRequests::NetDbRequests (): NetDbRequests::NetDbRequests ():
RunnableServiceWithWork ("NetDbReq"), RunnableServiceWithWork ("NetDbReq"),
m_ManageRequestsTimer (GetIOService ()), m_ExploratoryTimer (GetIOService ()), m_ManageRequestsTimer (GetIOService ()), m_ExploratoryTimer (GetIOService ()),
m_CleanupTimer (GetIOService ()) m_CleanupTimer (GetIOService ()), m_DiscoveredRoutersTimer (GetIOService ())
{ {
} }
@ -366,10 +365,12 @@ namespace data
size_t num = buf[32]; // num size_t num = buf[32]; // num
LogPrint (eLogDebug, "NetDbReq: DatabaseSearchReply for ", key, " num=", num); LogPrint (eLogDebug, "NetDbReq: DatabaseSearchReply for ", key, " num=", num);
IdentHash ident (buf); IdentHash ident (buf);
bool isExploratory = false;
auto dest = FindRequest (ident); auto dest = FindRequest (ident);
if (dest && dest->IsActive ()) if (dest && dest->IsActive ())
{ {
if (!dest->IsExploratory () && (num > 0 || dest->GetNumAttempts () < 3)) // before 3-rd attempt might be just bad luck isExploratory = dest->IsExploratory ();
if (!isExploratory && (num > 0 || dest->GetNumAttempts () < 3)) // before 3-rd attempt might be just bad luck
{ {
// try to send next requests // try to send next requests
if (!SendNextRequest (dest)) if (!SendNextRequest (dest))
@ -391,14 +392,33 @@ namespace data
LogPrint (eLogWarning, "NetDbReq: Too many peer hashes ", num, " in database search reply, Reduced to ", NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES); LogPrint (eLogWarning, "NetDbReq: Too many peer hashes ", num, " in database search reply, Reduced to ", NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES);
num = NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES; num = NETDB_MAX_NUM_SEARCH_REPLY_PEER_HASHES;
} }
if (isExploratory && !m_DiscoveredRouterHashes.empty ())
{
// request outstanding routers
for (auto it: m_DiscoveredRouterHashes)
RequestRouter (it);
m_DiscoveredRouterHashes.clear ();
m_DiscoveredRoutersTimer.cancel ();
}
for (size_t i = 0; i < num; i++) for (size_t i = 0; i < num; i++)
{ {
const uint8_t * router = buf + 33 + i*32; IdentHash router (buf + 33 + i*32);
char peerHash[48]; if (CheckLogLevel (eLogDebug))
int l1 = i2p::data::ByteStreamToBase64 (router, 32, peerHash, 48); LogPrint (eLogDebug, "NetDbReq: ", i, ": ", router.ToBase64 ());
peerHash[l1] = 0;
LogPrint (eLogDebug, "NetDbReq: ", i, ": ", peerHash);
if (isExploratory)
// postpone request
m_DiscoveredRouterHashes.push_back (router);
else
// send request right a way
RequestRouter (router);
}
if (isExploratory && !m_DiscoveredRouterHashes.empty ())
ScheduleDiscoveredRoutersRequest ();
}
void NetDbRequests::RequestRouter (const IdentHash& router)
{
auto r = netdb.FindRouter (router); auto r = netdb.FindRouter (router);
if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL) if (!r || i2p::util::GetMillisecondsSinceEpoch () > r->GetTimestamp () + 3600*1000LL)
{ {
@ -409,12 +429,11 @@ namespace data
else */if (!IsRouterBanned (router)) else */if (!IsRouterBanned (router))
RequestDestination (router, nullptr, true); RequestDestination (router, nullptr, true);
else else
LogPrint (eLogDebug, "NetDbReq: Router ", peerHash, " is banned. Skipped"); LogPrint (eLogDebug, "NetDbReq: Router ", router.ToBase64 (), " is banned. Skipped");
} }
else else
LogPrint (eLogDebug, "NetDbReq: [:|||:]"); LogPrint (eLogDebug, "NetDbReq: [:|||:]");
} }
}
void NetDbRequests::PostRequestDestination (const IdentHash& destination, void NetDbRequests::PostRequestDestination (const IdentHash& destination,
const RequestedDestination::RequestComplete& requestComplete, bool direct) const RequestedDestination::RequestComplete& requestComplete, bool direct)
@ -517,5 +536,27 @@ namespace data
ScheduleExploratory (nextExploratoryInterval); ScheduleExploratory (nextExploratoryInterval);
} }
} }
void NetDbRequests::ScheduleDiscoveredRoutersRequest ()
{
m_DiscoveredRoutersTimer.expires_from_now (boost::posix_time::milliseconds(
DISCOVERED_REQUEST_INTERVAL + rand () % DISCOVERED_REQUEST_INTERVAL_VARIANCE));
m_DiscoveredRoutersTimer.async_wait (std::bind (&NetDbRequests::HandleDiscoveredRoutersTimer,
this, std::placeholders::_1));
}
void NetDbRequests::HandleDiscoveredRoutersTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
if (!m_DiscoveredRouterHashes.empty ())
{
RequestRouter (m_DiscoveredRouterHashes.front ());
m_DiscoveredRouterHashes.pop_front ();
if (!m_DiscoveredRouterHashes.empty ()) // more hashes to request
ScheduleDiscoveredRoutersRequest ();
}
}
}
} }
} }

9
libi2pd/NetDbRequests.h

@ -28,6 +28,8 @@ namespace data
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL); const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL);
const uint64_t EXPLORATORY_REQUEST_INTERVAL = 55; // in seconds const uint64_t EXPLORATORY_REQUEST_INTERVAL = 55; // in seconds
const uint64_t EXPLORATORY_REQUEST_INTERVAL_VARIANCE = 170; // in seconds const uint64_t EXPLORATORY_REQUEST_INTERVAL_VARIANCE = 170; // in seconds
const uint64_t DISCOVERED_REQUEST_INTERVAL = 360; // in milliseconds
const uint64_t DISCOVERED_REQUEST_INTERVAL_VARIANCE = 540; // in milliseconds
const uint64_t MAX_EXPLORATORY_REQUEST_TIME = 30; // in seconds const uint64_t MAX_EXPLORATORY_REQUEST_TIME = 30; // in seconds
const uint64_t REQUEST_CACHE_TIME = MAX_REQUEST_TIME + 40; // in seconds const uint64_t REQUEST_CACHE_TIME = MAX_REQUEST_TIME + 40; // in seconds
const uint64_t REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL = 191; // in seconds const uint64_t REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL = 191; // in seconds
@ -96,6 +98,7 @@ namespace data
bool SendNextRequest (std::shared_ptr<RequestedDestination> dest); bool SendNextRequest (std::shared_ptr<RequestedDestination> dest);
void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg); void HandleDatabaseSearchReplyMsg (std::shared_ptr<const I2NPMessage> msg);
void RequestRouter (const IdentHash& router);
void RequestDestination (const IdentHash& destination, const RequestedDestination::RequestComplete& requestComplete, bool direct); void RequestDestination (const IdentHash& destination, const RequestedDestination::RequestComplete& requestComplete, bool direct);
void Explore (int numDestinations); void Explore (int numDestinations);
void ManageRequests (); void ManageRequests ();
@ -106,13 +109,17 @@ namespace data
void HandleExploratoryTimer (const boost::system::error_code& ecode); void HandleExploratoryTimer (const boost::system::error_code& ecode);
void ScheduleCleanup (); void ScheduleCleanup ();
void HandleCleanupTimer (const boost::system::error_code& ecode); void HandleCleanupTimer (const boost::system::error_code& ecode);
void ScheduleDiscoveredRoutersRequest ();
void HandleDiscoveredRoutersTimer (const boost::system::error_code& ecode);
private: private:
mutable std::mutex m_RequestedDestinationsMutex; mutable std::mutex m_RequestedDestinationsMutex;
std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations; std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations;
std::list<IdentHash> m_DiscoveredRouterHashes;
i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool; i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool;
boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer, m_CleanupTimer; boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer,
m_CleanupTimer, m_DiscoveredRoutersTimer;
}; };
} }
} }

Loading…
Cancel
Save