From: Joe Lanford Date: Thu, 20 Apr 2017 20:19:58 +0000 (-0400) Subject: Added peer count stats to TorrentStats struct X-Git-Tag: v1.0.0~473^2 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=0d19c72ea5f96163357600427c59cdcb0b9c1294;p=btrtrc.git Added peer count stats to TorrentStats struct --- diff --git a/torrent.go b/torrent.go index 397c13ac..4d36209c 100644 --- a/torrent.go +++ b/torrent.go @@ -1271,9 +1271,30 @@ func (t *Torrent) addPeers(peers []Peer) { 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. func (t *Torrent) addConnection(c *connection) bool { if t.cl.closed.IsSet() { diff --git a/torrent_stats.go b/torrent_stats.go index 3f34434a..3dd2716a 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 }