diff --git a/libi2pd/NetDb.cpp b/libi2pd/NetDb.cpp index ea687ffa..b9d32a70 100644 --- a/libi2pd/NetDb.cpp +++ b/libi2pd/NetDb.cpp @@ -118,7 +118,7 @@ namespace data { 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; 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 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 (lastManage) diff --git a/libi2pd/NetDbRequests.cpp b/libi2pd/NetDbRequests.cpp index baae4b1b..105e9a9f 100644 --- a/libi2pd/NetDbRequests.cpp +++ b/libi2pd/NetDbRequests.cpp @@ -104,15 +104,37 @@ namespace data } } + NetDbRequests::NetDbRequests (): + RunnableServiceWithWork ("NetDbReq"), + m_ManageRequestsTimer (GetIOService ()) + { + } + + NetDbRequests::~NetDbRequests () + { + Stop (); + } + void NetDbRequests::Start () { m_LastPoolCleanUpTime = i2p::util::GetSecondsSinceEpoch (); + if (!IsRunning ()) + { + StartIOService (); + ScheduleManageRequests (); + } } void NetDbRequests::Stop () { - m_RequestedDestinations.clear (); - m_RequestedDestinationsPool.CleanUpMt (); + if (IsRunning ()) + { + m_ManageRequestsTimer.cancel (); + StopIOService (); + + m_RequestedDestinations.clear (); + m_RequestedDestinationsPool.CleanUpMt (); + } } @@ -294,5 +316,22 @@ namespace data } 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 (); + } + } } } diff --git a/libi2pd/NetDbRequests.h b/libi2pd/NetDbRequests.h index 1f78fde6..e9dd99af 100644 --- a/libi2pd/NetDbRequests.h +++ b/libi2pd/NetDbRequests.h @@ -68,10 +68,14 @@ namespace data int m_NumAttempts; }; - class NetDbRequests: public std::enable_shared_from_this + class NetDbRequests: public std::enable_shared_from_this, + private i2p::util::RunnableServiceWithWork { public: + NetDbRequests (); + ~NetDbRequests (); + void Start (); void Stop (); @@ -79,8 +83,14 @@ namespace data bool direct = false, RequestedDestination::RequestComplete requestComplete = nullptr); void RequestComplete (const IdentHash& ident, std::shared_ptr r); std::shared_ptr FindRequest (const IdentHash& ident) const; - void ManageRequests (); bool SendNextRequest (std::shared_ptr dest); + + private: + + void ManageRequests (); + // timer + void ScheduleManageRequests (); + void HandleManageRequestsTimer (const boost::system::error_code& ecode); private: @@ -88,6 +98,7 @@ namespace data std::unordered_map > m_RequestedDestinations; i2p::util::MemoryPoolMt m_RequestedDestinationsPool; uint64_t m_LastPoolCleanUpTime = 0; // in seconds + boost::asio::deadline_timer m_ManageRequestsTimer; }; } }