]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Added peer count stats to TorrentStats struct
authorJoe Lanford <joe.lanford@gmail.com>
Thu, 20 Apr 2017 20:19:58 +0000 (16:19 -0400)
committerJoe Lanford <joe.lanford@gmail.com>
Sun, 23 Apr 2017 03:29:10 +0000 (23:29 -0400)
torrent.go
torrent_stats.go

index 397c13ac572e78418d3831ab3bb2ac726df612e7..4d36209c5a825440b317a06d16bd335452716b46 100644 (file)
@@ -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() {
index 3f34434af26a619cd078c63a9a1cb76d5c6cff15..3dd2716a916db24aa5be75eac817922c68d89290 100644 (file)
@@ -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
 }