separate timer for netdb requests cleanup

This commit is contained in:
orignal 2024-05-23 14:27:39 -04:00
parent f4ea6138e8
commit 9a724b2af9
2 changed files with 23 additions and 10 deletions

View File

@ -97,7 +97,8 @@ 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 ())
{ {
} }
@ -108,11 +109,11 @@ namespace data
void NetDbRequests::Start () void NetDbRequests::Start ()
{ {
m_LastPoolCleanUpTime = i2p::util::GetSecondsSinceEpoch ();
if (!IsRunning ()) if (!IsRunning ())
{ {
StartIOService (); StartIOService ();
ScheduleManageRequests (); ScheduleManageRequests ();
ScheduleCleanup ();
if (!i2p::context.IsHidden ()) if (!i2p::context.IsHidden ())
ScheduleExploratory (EXPLORATORY_REQUEST_INTERVAL); ScheduleExploratory (EXPLORATORY_REQUEST_INTERVAL);
} }
@ -124,6 +125,7 @@ namespace data
{ {
m_ManageRequestsTimer.cancel (); m_ManageRequestsTimer.cancel ();
m_ExploratoryTimer.cancel (); m_ExploratoryTimer.cancel ();
m_CleanupTimer.cancel ();
StopIOService (); StopIOService ();
m_RequestedDestinations.clear (); 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<RequestedDestination> NetDbRequests::CreateRequest (const IdentHash& destination, std::shared_ptr<RequestedDestination> NetDbRequests::CreateRequest (const IdentHash& destination,
bool isExploratory, bool direct, RequestedDestination::RequestComplete requestComplete) bool isExploratory, bool direct, RequestedDestination::RequestComplete requestComplete)
{ {
@ -202,11 +219,6 @@ namespace data
void NetDbRequests::ManageRequests () void NetDbRequests::ManageRequests ()
{ {
uint64_t ts = i2p::util::GetSecondsSinceEpoch (); uint64_t ts = i2p::util::GetSecondsSinceEpoch ();
if (ts > m_LastPoolCleanUpTime + REQUESTED_DESTINATIONS_POOL_CLEANUP_INTERVAL)
{
m_RequestedDestinationsPool.CleanUpMt ();
m_LastPoolCleanUpTime = ts;
}
std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex); std::unique_lock<std::mutex> l(m_RequestedDestinationsMutex);
for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();) for (auto it = m_RequestedDestinations.begin (); it != m_RequestedDestinations.end ();)
{ {

View File

@ -100,14 +100,15 @@ namespace data
void HandleManageRequestsTimer (const boost::system::error_code& ecode); void HandleManageRequestsTimer (const boost::system::error_code& ecode);
void ScheduleExploratory (uint64_t interval); void ScheduleExploratory (uint64_t interval);
void HandleExploratoryTimer (const boost::system::error_code& ecode); void HandleExploratoryTimer (const boost::system::error_code& ecode);
void ScheduleCleanup ();
void HandleCleanupTimer (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;
i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool; i2p::util::MemoryPoolMt<RequestedDestination> m_RequestedDestinationsPool;
uint64_t m_LastPoolCleanUpTime = 0; // in seconds boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer, m_CleanupTimer;
boost::asio::deadline_timer m_ManageRequestsTimer, m_ExploratoryTimer;
}; };
} }
} }