clean up pieces of seeding torrent. Update torrent status in random intervals

This commit is contained in:
orignal
2026-09-12 08:17:17 -04:00
parent c740ea579c
commit 8edd903a79
4 changed files with 21 additions and 12 deletions
+2 -2
View File
@@ -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;
+5 -1
View File
@@ -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<PieceFileFragment> GetPieceFileFragments (int index) const;
std::vector<size_t> 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<PeerConnection> conn);
std::list<std::shared_ptr<PeerConnection> > GetConnections ();
@@ -270,6 +273,7 @@ namespace torrents
bool m_IsComplete, m_IsStopped;
std::list<std::pair<std::filesystem::path, size_t> > 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
+11 -8
View File
@@ -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 ();
}
+3 -1
View File
@@ -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