From 4b42c6b76a397571c28bc79eba8b16e2e2b5142b Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 13 Sep 2026 15:19:28 -0400 Subject: [PATCH] recoonect peers from different torrents after random intervals --- libi2pd_client/Torrents.cpp | 2 +- libi2pd_client/Torrents.h | 4 +++- libi2pd_client/TorrentsTunnel.cpp | 16 ++++++++++------ libi2pd_client/TorrentsTunnel.h | 4 +++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index d843bac2..d72d2823 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -344,7 +344,7 @@ namespace torrents Torrent::Torrent (): m_Length (0), m_PieceLength (0), m_IsComplete (false), m_IsStopped (false), - m_Uploaded (0), m_Downloaded (0), m_NextUpdateStatusTime (0) + m_Uploaded (0), m_Downloaded (0), m_NextUpdateStatusTime (0), m_NextReconnectTime (0) { ResetStats (); } diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 347d36e1..4bf2e360 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -220,6 +220,8 @@ namespace torrents bool UpdateStatus (uint64_t ts); // return true if complete uint64_t GetNextUpdateStatusTime () { return m_NextUpdateStatusTime; } void SetNextUpdateStatusTime (uint64_t nextUpdateStatusTime) { m_NextUpdateStatusTime = nextUpdateStatusTime; } + uint64_t GetNextReconnectTime () { return m_NextReconnectTime; } + void SetNextReconnectTime (uint64_t nextReconnectTime) { m_NextReconnectTime = nextReconnectTime; } bool AddConnection (std::shared_ptr conn); void RemoveConnection (std::shared_ptr conn); std::list > GetConnections (); @@ -277,7 +279,7 @@ namespace torrents bool m_IsComplete, m_IsStopped; std::list > m_Files; // list of (path, length) size_t m_Uploaded, m_Downloaded; - uint64_t m_NextUpdateStatusTime; // in monotonic seconds + uint64_t m_NextUpdateStatusTime, m_NextReconnectTime; // in monotonic seconds // stats uint64_t m_DownloadRate, m_UploadRate; // B/sec int m_NumDownloadingFromPeers, m_NumUploadingToPeers; // by us diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index fd3042ec..c48f1507 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -801,15 +801,19 @@ namespace torrents { if (ecode != boost::asio::error::operation_aborted) { + auto ts = i2p::util::GetMonotonicSeconds (); for (auto it: m_Torrents) - { - if (!it.second->IsComplete () && !it.second->IsStopped ()) + if (ts > it.second->GetNextReconnectTime ()) { - auto numPeers = ConnectToPeers (it.second); - if (numPeers) - LogPrint (eLogDebug, "TorrentsTunnel: Reconnecting to ", numPeers, " peers"); + if (!it.second->IsComplete () && !it.second->IsStopped ()) + { + auto numPeers = ConnectToPeers (it.second); + if (numPeers) + LogPrint (eLogDebug, "TorrentsTunnel: Reconnecting to ", numPeers, " peers"); + } + it.second->SetNextReconnectTime (ts + RECONNECT_INTERVAL + GetLocalDestination ()->GetRng ()() % RECONNECT_INTERVAL_VARIANCE); } - } + ScheduleReconnectCheck (); } } diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 69e93821..15340fee 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -37,7 +37,9 @@ namespace torrents constexpr int DATAGRAM_TRACKER_TRANSACTION_TIMEOUT = 10000; // in milliseconds constexpr int DATAGRAM_TRACKER_CONNECTION_EXPIRATION = 60000; // in milliseconds constexpr int TRACKER_REQUESTS_CHECK_TIMEOUT = 1900; // in milliseconds - constexpr int RECONNECT_CHECK_INTERVAL = 70; // in seconds + constexpr int RECONNECT_INTERVAL = 45; // in seconds + constexpr int RECONNECT_INTERVAL_VARIANCE = 30; // in seconds + constexpr int RECONNECT_CHECK_INTERVAL = 30; // in seconds constexpr int TRACKER_REQUESTS_INTERVAL_VARIANCE = 3000; // in milliseconds constexpr int TRACKER_INITIAL_REQUEST_INTERVAL_VARIANCE = 31000; // in milliseconds constexpr int PEER_KEEP_ALIVE_CHECK_INTERVAL = 15; // in seconds