From ca0754067577ad80e8171b6fe6b9faf74fe39c90 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Wed, 8 Jun 2022 08:51:48 +0300 Subject: [PATCH] Fix rate calculation when interval is too low PR #17155. --- src/base/bittorrent/session.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 25145101a..89544084e 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -5137,7 +5137,15 @@ void Session::handleExternalIPAlert(const lt::external_ip_alert *p) void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) { - const qreal interval = lt::total_milliseconds(p->timestamp() - m_statsLastTimestamp) / 1000.; + if (m_refreshEnqueued) + m_refreshEnqueued = false; + else + enqueueRefresh(); + + const int64_t interval = lt::total_microseconds(p->timestamp() - m_statsLastTimestamp); + if (interval <= 0) + return; + m_statsLastTimestamp = p->timestamp(); const auto stats = p->counters(); @@ -5158,7 +5166,9 @@ void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) const auto calcRate = [interval](const qint64 previous, const qint64 current) -> qint64 { Q_ASSERT(current >= previous); - return ((current - previous) / interval); + Q_ASSERT(interval >= 0); + using namespace std::chrono_literals; + return (((current - previous) * lt::microseconds(1s).count()) / interval); }; m_status.payloadDownloadRate = calcRate(m_status.totalPayloadDownload, totalPayloadDownload); @@ -5204,11 +5214,6 @@ void Session::handleSessionStatsAlert(const lt::session_stats_alert *p) ? (stats[m_metricIndices.disk.diskJobTime] / totalJobs) : 0; emit statsUpdated(); - - if (m_refreshEnqueued) - m_refreshEnqueued = false; - else - enqueueRefresh(); } void Session::handleAlertsDroppedAlert(const lt::alerts_dropped_alert *p) const