Browse Source

moved netdb requests to separate thread

pull/2072/head
orignal 7 months ago
parent
commit
e3be409945
  1. 11
      libi2pd/NetDb.cpp
  2. 43
      libi2pd/NetDbRequests.cpp
  3. 15
      libi2pd/NetDbRequests.h

11
libi2pd/NetDb.cpp

@ -118,7 +118,7 @@ namespace data
{ {
i2p::util::SetThreadName("NetDB"); i2p::util::SetThreadName("NetDB");
uint64_t lastManage = 0, lastExploratory = 0, lastManageRequest = 0; uint64_t lastManage = 0, lastExploratory = 0;
uint64_t lastProfilesCleanup = i2p::util::GetMonotonicMilliseconds (), lastObsoleteProfilesCleanup = lastProfilesCleanup; uint64_t lastProfilesCleanup = i2p::util::GetMonotonicMilliseconds (), lastObsoleteProfilesCleanup = lastProfilesCleanup;
int16_t profilesCleanupVariance = 0, obsoleteProfilesCleanVariance = 0, exploratoryIntervalVariance = 0; int16_t profilesCleanupVariance = 0, obsoleteProfilesCleanVariance = 0, exploratoryIntervalVariance = 0;
@ -162,15 +162,6 @@ namespace data
continue; // don't manage netdb when offline or transports are not running continue; // don't manage netdb when offline or transports are not running
uint64_t mts = i2p::util::GetMonotonicMilliseconds (); uint64_t mts = i2p::util::GetMonotonicMilliseconds ();
if (mts >= lastManageRequest + MANAGE_REQUESTS_INTERVAL*1000)
{
if (lastManageRequest || i2p::tunnel::tunnels.GetExploratoryPool ()) // expolratory pool is ready?
{
if (m_Requests) m_Requests->ManageRequests ();
lastManageRequest = mts;
}
}
if (mts >= lastManage + 60000) // manage routers and leasesets every minute if (mts >= lastManage + 60000) // manage routers and leasesets every minute
{ {
if (lastManage) if (lastManage)

43
libi2pd/NetDbRequests.cpp

@ -104,15 +104,37 @@ namespace data
} }
} }
NetDbRequests::NetDbRequests ():
RunnableServiceWithWork ("NetDbReq"),
m_ManageRequestsTimer (GetIOService ())
{
}
NetDbRequests::~NetDbRequests ()
{
Stop ();
}
void NetDbRequests::Start () void NetDbRequests::Start ()
{ {
m_LastPoolCleanUpTime = i2p::util::GetSecondsSinceEpoch (); m_LastPoolCleanUpTime = i2p::util::GetSecondsSinceEpoch ();
if (!IsRunning ())
{
StartIOService ();
ScheduleManageRequests ();
}
} }
void NetDbRequests::Stop () void NetDbRequests::Stop ()
{ {
m_RequestedDestinations.clear (); if (IsRunning ())
m_RequestedDestinationsPool.CleanUpMt (); {
m_ManageRequestsTimer.cancel ();
StopIOService ();
m_RequestedDestinations.clear ();
m_RequestedDestinationsPool.CleanUpMt ();
}
} }
@ -294,5 +316,22 @@ namespace data
} }
return ret; return ret;
} }
void NetDbRequests::ScheduleManageRequests ()
{
m_ManageRequestsTimer.expires_from_now (boost::posix_time::seconds(MANAGE_REQUESTS_INTERVAL));
m_ManageRequestsTimer.async_wait (std::bind (&NetDbRequests::HandleManageRequestsTimer,
this, std::placeholders::_1));
}
void NetDbRequests::HandleManageRequestsTimer (const boost::system::error_code& ecode)
{
if (ecode != boost::asio::error::operation_aborted)
{
if (i2p::tunnel::tunnels.GetExploratoryPool ()) // expolratory pool is ready?
ManageRequests ();
ScheduleManageRequests ();
}
}
} }
} }

15
libi2pd/NetDbRequests.h

@ -68,10 +68,14 @@ namespace data
int m_NumAttempts; int m_NumAttempts;
}; };
class NetDbRequests: public std::enable_shared_from_this<NetDbRequests> class NetDbRequests: public std::enable_shared_from_this<NetDbRequests>,
private i2p::util::RunnableServiceWithWork
{ {
public: public:
NetDbRequests ();
~NetDbRequests ();
void Start (); void Start ();
void Stop (); void Stop ();
@ -79,8 +83,14 @@ namespace data
bool direct = false, RequestedDestination::RequestComplete requestComplete = nullptr); bool direct = false, RequestedDestination::RequestComplete requestComplete = nullptr);
void RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r); void RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r);
std::shared_ptr<RequestedDestination> FindRequest (const IdentHash& ident) const; std::shared_ptr<RequestedDestination> FindRequest (const IdentHash& ident) const;
void ManageRequests ();
bool SendNextRequest (std::shared_ptr<RequestedDestination> dest); bool SendNextRequest (std::shared_ptr<RequestedDestination> dest);
private:
void ManageRequests ();
// timer
void ScheduleManageRequests ();
void HandleManageRequestsTimer (const boost::system::error_code& ecode);
private: private:
@ -88,6 +98,7 @@ namespace data
std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations; std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations;
i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool; i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool;
uint64_t m_LastPoolCleanUpTime = 0; // in seconds uint64_t m_LastPoolCleanUpTime = 0; // in seconds
boost::asio::deadline_timer m_ManageRequestsTimer;
}; };
} }
} }

Loading…
Cancel
Save