From 9d34389d6a42c9f2a8e4ce9943860a32a0fd0fd9 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 28 Nov 2022 11:21:33 +0300 Subject: [PATCH] Fixed lame speed calculation --- cmd/btrtrc/list.go | 18 +++++++++++------- cmd/btrtrc/status.go | 8 +++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cmd/btrtrc/list.go b/cmd/btrtrc/list.go index dcbb8b99..89d96146 100644 --- a/cmd/btrtrc/list.go +++ b/cmd/btrtrc/list.go @@ -26,8 +26,14 @@ const ( FilesDir = "files" ) +type TorrentStat struct { + stats torrent.ConnStats + rxSpeed int64 + txSpeed int64 +} + var ( - TorrentStats = map[metainfo.Hash]torrent.TorrentStats{} + TorrentStats = map[metainfo.Hash]TorrentStat{} TorrentStatsM sync.RWMutex ) @@ -75,11 +81,9 @@ func fifoList(c *torrent.Client) { TorrentStatsM.RLock() prev := TorrentStats[t.InfoHash()] TorrentStatsM.RUnlock() - rxSpeed := stats.BytesReadData.Int64() - prev.BytesReadData.Int64() - txSpeed := stats.BytesWrittenData.Int64() - prev.BytesWrittenData.Int64() var eta string - if done < 100 && rxSpeed > 0 { - etaRaw := time.Duration((t.Length() - t.BytesCompleted()) / rxSpeed) + if done < 100 && prev.rxSpeed > 0 { + etaRaw := time.Duration((t.Length() - t.BytesCompleted()) / prev.rxSpeed) etaRaw *= time.Second eta = etaRaw.String() } @@ -90,8 +94,8 @@ func fifoList(c *torrent.Client) { humanize.IBytes(uint64(t.Length())), percColour, done, Reset, ratio, - Green, rxSpeed/1024, Reset, - Magenta, txSpeed/1024, Reset, + Green, prev.rxSpeed/1024, Reset, + Magenta, prev.txSpeed/1024, Reset, stats.TotalPeers, stats.PendingPeers, stats.ActivePeers, diff --git a/cmd/btrtrc/status.go b/cmd/btrtrc/status.go index f040d554..3d937efa 100644 --- a/cmd/btrtrc/status.go +++ b/cmd/btrtrc/status.go @@ -25,8 +25,14 @@ func overallStatus(c *torrent.Client, cancel chan struct{}) { continue } tStats := t.Stats() + cur := tStats.Copy() TorrentStatsM.Lock() - TorrentStats[t.InfoHash()] = tStats + prev := TorrentStats[t.InfoHash()].stats + TorrentStats[t.InfoHash()] = TorrentStat{ + stats: cur, + rxSpeed: cur.BytesReadData.Int64() - prev.BytesReadData.Int64(), + txSpeed: cur.BytesWrittenData.Int64() - prev.BytesWrittenData.Int64(), + } TorrentStatsM.Unlock() peers += tStats.ActivePeers } -- 2.48.1