diff --git a/libi2pd/NetDbRequests.cpp b/libi2pd/NetDbRequests.cpp index 2ea1be50..6cdc0d0a 100644 --- a/libi2pd/NetDbRequests.cpp +++ b/libi2pd/NetDbRequests.cpp @@ -97,7 +97,8 @@ namespace data NetDbRequests::NetDbRequests (): RunnableServiceWithWork ("NetDbReq"), - m_ManageRequestsTimer (GetIOService ()), m_ExploratoryTimer (GetIOService ()) + m_ManageRequestsTimer (GetIOService ()), m_ExploratoryTimer (GetIOService ()), + m_CleanupTimer (GetIOService ()) { } @@ -108,11 +109,11 @@ namespace data void NetDbRequests::Start () { - m_LastPoolCleanUpTime = i2p::util::GetSecondsSinceEpoch (); if (!IsRunning ()) { StartIOService (); ScheduleManageRequests (); + ScheduleCleanup (); if (!i2p::context.IsHidden ()) ScheduleExploratory (EXPLORATORY_REQUEST_INTERVAL); } @@ -124,6 +125,7 @@ namespace data { m_ManageRequestsTimer.cancel (); m_ExploratoryTimer.cancel (); + m_CleanupTimer.cancel (); StopIOService (); m_RequestedDestinations.clear (); @@ -131,7 +133,22 @@ namespace data } } - + void NetDbRequests::ScheduleCleanup () + { + m_CleanupTimer.expires_from_now (boost::posix_time::seconds(REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL)); + m_CleanupTimer.async_wait (std::bind (&NetDbRequests::HandleCleanupTimer, + this, std::placeholders::_1)); + } + + void NetDbRequests::HandleCleanupTimer (const boost::system::error_code& ecode) + { + if (ecode != boost::asio::error::operation_aborted) + { + m_RequestedDestinationsPool.CleanUpMt (); + ScheduleCleanup (); + } + } + std::shared_ptr NetDbRequests::CreateRequest (const IdentHash& destination, bool isExploratory, bool direct, RequestedDestination::RequestComplete requestComplete) { @@ -202,11 +219,6 @@ namespace data void NetDbRequests::ManageRequests () { uint64_t ts = i2p::util::GetSecondsSinceEpoch (); - if (ts > m_LastPoolCleanUpTime + REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL) - { - m_RequestedDestinationsPool.CleanUpMt (); - m_LastPoolCleanUpTime = ts; - } std::unique_lock l(m_RequestedDestinationsMutex); for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();) { diff --git a/libi2pd/NetDbRequests.h b/libi2pd/NetDbRequests.h index 5ceb7b4c..903bdf76 100644 --- a/libi2pd/NetDbRequests.h +++ b/libi2pd/NetDbRequests.h @@ -100,14 +100,15 @@ namespace data void HandleManageRequestsTimer (const boost::system::error_code& ecode); void ScheduleExploratory (uint64_t interval); void HandleExploratoryTimer (const boost::system::error_code& ecode); + void ScheduleCleanup (); + void HandleCleanupTimer (const boost::system::error_code& ecode); private: mutable std::mutex m_RequestedDestinationsMutex; std::unordered_map > m_RequestedDestinations; i2p::util::MemoryPoolMt m_RequestedDestinationsPool; - uint64_t m_LastPoolCleanUpTime = 0; // in seconds - boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer; + boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer, m_CleanupTimer; }; } }