torrent.go | 21 +++++++++++++++++++++ torrent_stats.go | 5 +++++ diff --git a/torrent.go b/torrent.go index 397c13ac572e78418d3831ab3bb2ac726df612e7..4d36209c5a825440b317a06d16bd335452716b46 100644 --- a/torrent.go +++ b/torrent.go @@ -1271,7 +1271,28 @@ func (t *Torrent) Stats() TorrentStats { t.cl.mu.Lock() defer t.cl.mu.Unlock() + + t.stats.ActivePeers = len(t.conns) + t.stats.HalfOpenPeers = len(t.halfOpen) + t.stats.PendingPeers = len(t.peers) + t.stats.TotalPeers = t.numTotalPeers() + return t.stats +} + +// The total number of peers in the torrent. +func (t *Torrent) numTotalPeers() int { + peers := make(map[string]struct{}) + for conn := range t.conns { + peers[conn.conn.RemoteAddr().String()] = struct{}{} + } + for addr := range t.halfOpen { + peers[addr] = struct{}{} + } + for _, peer := range t.peers { + peers[fmt.Sprintf("%s:%d", peer.IP, peer.Port)] = struct{}{} + } + return len(peers) } // Returns true if the connection is added. diff --git a/torrent_stats.go b/torrent_stats.go index 3f34434af26a619cd078c63a9a1cb76d5c6cff15..3dd2716a916db24aa5be75eac817922c68d89290 100644 --- a/torrent_stats.go +++ b/torrent_stats.go @@ -2,4 +2,9 @@ package torrent type TorrentStats struct { ConnStats // Aggregates stats over all connections past and present. + + ActivePeers int + HalfOpenPeers int + PendingPeers int + TotalPeers int }