From 53da7ae2f55ab265af658719eb626ad57ed0aa28 Mon Sep 17 00:00:00 2001 From: PobreGato <315121269+pobregat0@users.noreply.github.com> Date: Sun, 23 Aug 2026 02:32:29 +0300 Subject: [PATCH] give each udp server tunnel its own cleanup timer --- libi2pd_client/ClientContext.cpp | 34 -------------------------------- libi2pd_client/ClientContext.h | 3 --- libi2pd_client/UDPTunnel.cpp | 22 +++++++++++++++++++++ libi2pd_client/UDPTunnel.h | 6 ++++++ 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/libi2pd_client/ClientContext.cpp b/libi2pd_client/ClientContext.cpp index 565d6848..d5089e78 100644 --- a/libi2pd_client/ClientContext.cpp +++ b/libi2pd_client/ClientContext.cpp @@ -117,13 +117,6 @@ namespace client m_AddressBook.StartResolvers (); - // start UDP cleanup - if (!m_ServerForwards.empty ()) - { - m_CleanupUDPTimer.reset (new boost::asio::steady_timer(m_SharedLocalDestination->GetService ())); - ScheduleCleanupUDP(); - } - // start torrents RPC server for (auto& it: m_TorrentsRPCServers) it.second->Start (); @@ -220,13 +213,6 @@ namespace client m_ClientForwards.clear(); } - LogPrint(eLogInfo, "Clients: Stopping UDP Tunnels timers"); - if (m_CleanupUDPTimer) - { - m_CleanupUDPTimer->cancel (); - m_CleanupUDPTimer = nullptr; - } - { LogPrint(eLogInfo, "Clients: Stopping Destinations"); std::lock_guard lock(m_DestinationsMutex); @@ -1182,26 +1168,6 @@ namespace client } } - void ClientContext::ScheduleCleanupUDP() - { - if (m_CleanupUDPTimer) - { - // schedule cleanup in 17 seconds - m_CleanupUDPTimer->expires_after (std::chrono::seconds (17)); - m_CleanupUDPTimer->async_wait(std::bind(&ClientContext::CleanupUDP, this, std::placeholders::_1)); - } - } - - void ClientContext::CleanupUDP(const boost::system::error_code & ecode) - { - if(!ecode) - { - std::lock_guard lock(m_ForwardsMutex); - for (auto & s : m_ServerForwards ) s.second->ExpireStale(); - ScheduleCleanupUDP(); - } - } - void ClientContext::VisitTunnels (bool clean) { for (auto it = m_ClientTunnels.begin (); it != m_ClientTunnels.end ();) diff --git a/libi2pd_client/ClientContext.h b/libi2pd_client/ClientContext.h index a5943f45..ca2253f9 100644 --- a/libi2pd_client/ClientContext.h +++ b/libi2pd_client/ClientContext.h @@ -144,8 +144,6 @@ namespace client void ReadI2CPOptions (const Section& section, bool isServer, i2p::util::Mapping& options) const; // for tunnels void ReadI2CPOptionsFromConfig (const std::string& prefix, i2p::util::Mapping& options) const; // for HTTP and SOCKS proxy - void CleanupUDP(const boost::system::error_code & ecode); - void ScheduleCleanupUDP(); void VisitTunnels (bool clean); @@ -178,7 +176,6 @@ namespace client BOBCommandChannel * m_BOBCommandChannel; I2CPServer * m_I2CPServer; - std::unique_ptr m_CleanupUDPTimer; // i18n std::shared_ptr m_Language; diff --git a/libi2pd_client/UDPTunnel.cpp b/libi2pd_client/UDPTunnel.cpp index 86a550d3..300f74ac 100644 --- a/libi2pd_client/UDPTunnel.cpp +++ b/libi2pd_client/UDPTunnel.cpp @@ -488,6 +488,27 @@ namespace client ); m_StatsTimer.reset (new boost::asio::steady_timer (m_LocalDest->GetService ())); ScheduleStatsTimer (); + m_CleanupTimer.reset (new boost::asio::steady_timer (m_LocalDest->GetService ())); + ScheduleCleanupTimer (); + } + + void I2PUDPServerTunnel::ScheduleCleanupTimer () + { + if (m_CleanupTimer) + { + m_CleanupTimer->expires_after (std::chrono::seconds (I2P_UDP_SESSION_CLEANUP_INTERVAL)); + m_CleanupTimer->async_wait (std::bind (&I2PUDPServerTunnel::HandleCleanupTimer, + this, std::placeholders::_1)); + } + } + + void I2PUDPServerTunnel::HandleCleanupTimer (const boost::system::error_code& ecode) + { + if (ecode != boost::asio::error::operation_aborted) + { + ExpireStale (); + ScheduleCleanupTimer (); + } } void I2PUDPServerTunnel::ScheduleStatsTimer () @@ -524,6 +545,7 @@ namespace client void I2PUDPServerTunnel::Stop () { if (m_StatsTimer) m_StatsTimer->cancel (); + if (m_CleanupTimer) m_CleanupTimer->cancel (); auto dgram = m_LocalDest->GetDatagramDestination (); if (dgram) { dgram->ResetReceiver (m_inPort); diff --git a/libi2pd_client/UDPTunnel.h b/libi2pd_client/UDPTunnel.h index 5e5a1430..debe1a67 100644 --- a/libi2pd_client/UDPTunnel.h +++ b/libi2pd_client/UDPTunnel.h @@ -30,6 +30,7 @@ namespace client { /** 2 minute timeout for udp sessions */ const uint64_t I2P_UDP_SESSION_TIMEOUT = 1000 * 60 * 2; + const int I2P_UDP_SESSION_CLEANUP_INTERVAL = 17; // in seconds const uint64_t I2P_UDP_REPLIABLE_DATAGRAM_INTERVAL = 100; // in milliseconds const uint64_t I2P_UDP_MAX_UNACKED_DATAGRAM_TIME = 8000; // in milliseconds const uint64_t I2P_UDP_MIN_ACK_TIMEOUT = 500; // in milliseconds @@ -176,6 +177,8 @@ namespace client void ScheduleStatsTimer (); void HandleStatsTimer (const boost::system::error_code& ecode); + void ScheduleCleanupTimer (); + void HandleCleanupTimer (const boost::system::error_code& ecode); private: @@ -192,6 +195,7 @@ namespace client uint32_t m_NumRawNoSession = 0; size_t m_MaxWindow = I2P_UDP_MIN_MAX_NUM_UNACKED_DATAGRAMS; std::unique_ptr m_StatsTimer; + std::unique_ptr m_CleanupTimer; public: @@ -240,6 +244,8 @@ namespace client void ScheduleStatsTimer (); void HandleStatsTimer (const boost::system::error_code& ecode); + void ScheduleCleanupTimer (); + void HandleCleanupTimer (const boost::system::error_code& ecode); private: