]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add Client.Stats with ActiveHalfOpenAttempts
authorMatt Joiner <anacrolix@gmail.com>
Tue, 2 May 2023 07:09:43 +0000 (17:09 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 9 May 2023 05:46:52 +0000 (15:46 +1000)
client-stats.go [new file with mode: 0644]
client.go
peer.go
stats.go [new file with mode: 0644]
torrent-stats.go [moved from torrent_stats.go with 100% similarity]
torrent.go

diff --git a/client-stats.go b/client-stats.go
new file mode 100644 (file)
index 0000000..d799bae
--- /dev/null
@@ -0,0 +1,10 @@
+package torrent
+
+type ClientStats struct {
+       ConnStats
+
+       // Ongoing outgoing dial attempts. There may be more than one dial going on per peer address due
+       // to hole-punch connect requests. The total may not match the sum of attempts for all Torrents
+       // if a Torrent is dropped while there are outstanding dials.
+       ActiveHalfOpenAttempts int
+}
index 709bc4c07ca6033a20ecbb33e4b0697b371b939f..65545973b8733db2c46d81a8127196fdec57f89a 100644 (file)
--- a/client.go
+++ b/client.go
@@ -57,7 +57,7 @@ import (
 type Client struct {
        // An aggregate of stats over all connections. First in struct to ensure 64-bit alignment of
        // fields. See #262.
-       stats ConnStats
+       connStats ConnStats
 
        _mu    lockWithDeferreds
        event  sync.Cond
@@ -149,7 +149,7 @@ func (cl *Client) WriteStatus(_w io.Writer) {
                fmt.Fprintf(w, "%s DHT server at %s:\n", s.Addr().Network(), s.Addr().String())
                writeDhtServerStatus(w, s)
        })
-       spew.Fdump(w, &cl.stats)
+       dumpStats(w, cl.statsLocked())
        torrentsSlice := cl.torrentsAsSlice()
        fmt.Fprintf(w, "# Torrents: %d\n", len(torrentsSlice))
        fmt.Fprintln(w)
@@ -694,9 +694,7 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string, attemptKey outgoingC
 
 func (cl *Client) countHalfOpenFromTorrents() (count int) {
        for _, t := range cl.torrents {
-               for _, attempts := range t.halfOpen {
-                       count += len(attempts)
-               }
+               count += t.numHalfOpenAttempts()
        }
        return
 }
@@ -1783,8 +1781,20 @@ func (cl *Client) String() string {
        return fmt.Sprintf("<%[1]T %[1]p>", cl)
 }
 
-// Returns connection-level aggregate stats at the Client level. See the comment on
+// Returns connection-level aggregate connStats at the Client level. See the comment on
 // TorrentStats.ConnStats.
 func (cl *Client) ConnStats() ConnStats {
-       return cl.stats.Copy()
+       return cl.connStats.Copy()
+}
+
+func (cl *Client) Stats() ClientStats {
+       cl.rLock()
+       defer cl.rUnlock()
+       return cl.statsLocked()
+}
+
+func (cl *Client) statsLocked() (stats ClientStats) {
+       stats.ConnStats = cl.connStats.Copy()
+       stats.ActiveHalfOpenAttempts = cl.numHalfOpen
+       return
 }
diff --git a/peer.go b/peer.go
index 1d8ead1002b49758b0564ac47516d9f6fc420c80..4f7831373e2f07c852411db796c014d2523af92b 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -519,7 +519,7 @@ func (cn *Peer) peerPiecesChanged() {
 func (cn *Peer) postHandshakeStats(f func(*ConnStats)) {
        t := cn.t
        f(&t.stats)
-       f(&t.cl.stats)
+       f(&t.cl.connStats)
 }
 
 // All ConnStats that include this connection. Some objects are not known
diff --git a/stats.go b/stats.go
new file mode 100644 (file)
index 0000000..90144bf
--- /dev/null
+++ b/stats.go
@@ -0,0 +1,12 @@
+package torrent
+
+import (
+       "io"
+
+       "github.com/davecgh/go-spew/spew"
+)
+
+func dumpStats[T any](w io.Writer, stats T) {
+       spew.NewDefaultConfig()
+       spew.Fdump(w, stats)
+}
similarity index 100%
rename from torrent_stats.go
rename to torrent-stats.go
index 41848e4a2a39662cd619e0387182d392ce85410f..7ad67b2b4f92025a9aaf04136421213f62fe629f 100644 (file)
@@ -31,7 +31,6 @@ import (
        "github.com/anacrolix/missinggo/v2/pubsub"
        "github.com/anacrolix/multiless"
        "github.com/anacrolix/sync"
-       "github.com/davecgh/go-spew/spew"
        "github.com/pion/datachannel"
        "golang.org/x/exp/maps"
 
@@ -767,8 +766,7 @@ func (t *Torrent) writeStatus(w io.Writer) {
 
        fmt.Fprintf(w, "DHT Announces: %d\n", t.numDHTAnnounces)
 
-       spew.NewDefaultConfig()
-       spew.Fdump(w, t.statsLocked())
+       dumpStats(w, t.statsLocked())
 
        fmt.Fprintf(w, "webseeds:\n")
        t.writePeerStatuses(w, maps.Values(t.webSeeds))
@@ -2455,7 +2453,7 @@ func (t *Torrent) AddClientPeer(cl *Client) int {
 // connection.
 func (t *Torrent) allStats(f func(*ConnStats)) {
        f(&t.stats)
-       f(&t.cl.stats)
+       f(&t.cl.connStats)
 }
 
 func (t *Torrent) hashingPiece(i pieceIndex) bool {
@@ -2859,3 +2857,10 @@ func (t *Torrent) startHolepunchRendezvous(addrPort netip.AddrPort) (rz *utHolep
        }
        return
 }
+
+func (t *Torrent) numHalfOpenAttempts() (num int) {
+       for _, attempts := range t.halfOpen {
+               num += len(attempts)
+       }
+       return
+}