diff --git a/libktcore/torrent/queuemanager.h b/libktcore/torrent/queuemanager.h --- a/libktcore/torrent/queuemanager.h +++ b/libktcore/torrent/queuemanager.h @@ -195,6 +195,16 @@ */ void mergeAnnounceList(const bt::SHA1Hash& ih, const bt::TrackerTier* trk) override; + /** + * Requested by each TorrentControl during its update to + * get permission on saving Stats file to disk. May be + * overriden to balance I/O operations. + * @param tc Pointer to TorrentControl instance + * @return true if file save is permitted, false otherwise + */ + + bool permitStatsSync(bt::TorrentControl* tc) override; + /** * Set the maximum number of downloads * @param m Max downloads @@ -300,6 +310,7 @@ bool exiting; bool ordering; QDateTime network_down_time; + bt::TimeStamp last_stats_sync_permitted; }; } #endif diff --git a/libktcore/torrent/queuemanager.cpp b/libktcore/torrent/queuemanager.cpp --- a/libktcore/torrent/queuemanager.cpp +++ b/libktcore/torrent/queuemanager.cpp @@ -58,6 +58,8 @@ exiting = false; ordering = false; + last_stats_sync_permitted = 0; + QNetworkConfigurationManager* networkConfigurationManager = new QNetworkConfigurationManager(this); connect(networkConfigurationManager, &QNetworkConfigurationManager::onlineStateChanged, this, &QueueManager::onOnlineStateChanged); } @@ -545,6 +547,26 @@ } } + bool QueueManager::permitStatsSync(TorrentControl *tc) + { + // we want to assure that minimum time interval delay is happen + // before next TorrentControl dumps its State to the file + + // if you have more than 500 running torrents it's feasible to + // increase the period to avoid too small interval value + const TimeStamp max_period = (50 * 60 * 1000) * (downloads.size() / 500 + 1); + + if (tc->getStatsSyncElapsedTime() >= max_period) { + const bt::TimeStamp now = Now(); + const bt::TimeStamp interval = max_period / downloads.size(); + if (now - last_stats_sync_permitted > interval) { + last_stats_sync_permitted = now; + return true; + } + } + return false; + } + void QueueManager::orderQueue() { if (ordering || !downloads.count() || exiting)