From 8edd903a799804fe9163731e0cb6464eedcedf7e Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 12 Sep 2026 08:17:17 -0400 Subject: [PATCH] clean up pieces of seeding torrent. Update torrent status in random intervals --- libi2pd_client/Torrents.cpp | 4 ++-- libi2pd_client/Torrents.h | 6 +++++- libi2pd_client/TorrentsTunnel.cpp | 19 +++++++++++-------- libi2pd_client/TorrentsTunnel.h | 4 +++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index fb1a38d4..e92f1275 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_Uploaded (0), m_Downloaded (0), m_NextUpdateStatusTime (0) { ResetStats (); } @@ -743,7 +743,7 @@ namespace torrents for (auto& it: m_Pieces) { if (!it.IsComplete ()) complete = false; - if (ts > it.GetLastActivityTimestamp () + PIECE_INACTIVITY_TIMEOUT) // piece was inactive recently + if (m_IsStopped || (ts > it.GetLastActivityTimestamp () + PIECE_INACTIVITY_TIMEOUT)) // piece was inactive recently it.Reset (); } return complete; diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index fd5a4690..b6f7f5a6 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -44,7 +44,7 @@ namespace torrents constexpr size_t MAX_NUM_REQUESTS = 24; constexpr size_t MAX_NUM_PIECES = 6; constexpr size_t MAX_INCOMING_REQUESTS_QUEUE_SIZE = 32; - constexpr int PIECE_INACTIVITY_TIMEOUT = 35; // in seconds + constexpr int PIECE_INACTIVITY_TIMEOUT = 22; // in seconds constexpr int HANDSHAKE_RECEIVE_TIMEOUT = 20; // in seconds constexpr int BANDWIDTH_RATE_SAMPLING_INTERVAL = 20; // in milliseconds @@ -188,6 +188,7 @@ namespace torrents void SetComplete (); bool IsStopped () const { return m_IsStopped; } void SetStopped (bool stopped) { m_IsStopped = stopped; } + bool IsActive () const { return !m_Connections.empty (); } TorrentStatus GetStatus () const; std::string_view GetAnnounce () const { return m_Announce; } @@ -215,6 +216,8 @@ namespace torrents std::vector GetPieceFileFragments (int index) const; std::vector GetFilesCompleted () const; // completed size per file bool UpdateStatus (uint64_t ts); // return true if complete + uint64_t GetNextUpdateStatusTime () { return m_NextUpdateStatusTime; } + void SetNextUpdateStatusTime (uint64_t nextUpdateStatusTime) { m_NextUpdateStatusTime = nextUpdateStatusTime; } void AddConnection (std::shared_ptr conn); std::list > GetConnections (); @@ -270,6 +273,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 // 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 9ef503bc..1de0ff8c 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -489,6 +489,7 @@ namespace torrents { if (!torrent) return; torrent->SetStopped (true); + torrent->UpdateStatus (i2p::util::GetMonotonicSeconds ()); // inform trackers that we stopped RequestTorrentTrackers (torrent, eTrackerAnnounceEventStopped); // close connections @@ -797,7 +798,7 @@ namespace torrents void TorrentsTunnel::ScheduleStatusUpdate () { m_TorrentsStatusUpdateTimer.cancel (); - m_TorrentsStatusUpdateTimer.expires_after (std::chrono::seconds(TORRENTS_STATUS_UPDATE_INTERVAL)); + m_TorrentsStatusUpdateTimer.expires_after (std::chrono::seconds(TORRENTS_STATUS_UPDATE_CHECK_INTERVAL)); m_TorrentsStatusUpdateTimer.async_wait (std::bind (&TorrentsTunnel::HandleTorrentsStatusUpdateTimer, this, std::placeholders::_1)); } @@ -808,15 +809,17 @@ namespace torrents { auto ts = i2p::util::GetMonotonicSeconds (); for (auto it: m_Torrents) - { - if (!it.second->IsComplete () &&!it.second->IsStopped ()) + if (ts > it.second->GetNextUpdateStatusTime ()) { - if (it.second->UpdateStatus (ts)) - CompleteTorrent (it.second); - else - UpdatePeersPerPiece (it.second); + if (!it.second->IsStopped () && (it.second->IsActive () || !it.second->IsComplete ())) + { + if (it.second->UpdateStatus (ts)) + CompleteTorrent (it.second); + else + UpdatePeersPerPiece (it.second); + } + it.second->SetNextUpdateStatusTime (ts + TORRENTS_STATUS_UPDATE_INTERVAL + GetLocalDestination ()->GetRng ()() % TORRENTS_STATUS_UPDATE_INTERVAL_VARIANCE); } - } UpdateStats (); ScheduleStatusUpdate (); } diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 09db9687..a45c7df7 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -41,7 +41,9 @@ namespace torrents 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 - constexpr int TORRENTS_STATUS_UPDATE_INTERVAL = 25; // in seconds + constexpr int TORRENTS_STATUS_UPDATE_INTERVAL = 9; // in seconds + constexpr int TORRENTS_STATUS_UPDATE_INTERVAL_VARIANCE = 7; // in seconds + constexpr int TORRENTS_STATUS_UPDATE_CHECK_INTERVAL = 8; // in seconds constexpr int TRACKER_MAX_NUM_WANT = 25; enum DatagramTrackerAction