]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client-stats.go
Drop support for go 1.20
[btrtrc.git] / client-stats.go
index 3513f8f1c250c98ffff3c0a4b33b0af5be8f737a..bfa6994e9890af2e346ac2d354e4b4be9ec0d975 100644 (file)
@@ -1,5 +1,24 @@
 package torrent
 
+import (
+       "net/netip"
+
+       g "github.com/anacrolix/generics"
+)
+
+func setAdd[K comparable](m *map[K]struct{}, elem K) {
+       g.MakeMapIfNilAndSet(m, elem, struct{}{})
+}
+
+type clientHolepunchAddrSets struct {
+       undialableWithoutHolepunch                            map[netip.AddrPort]struct{}
+       undialableWithoutHolepunchDialedAfterHolepunchConnect map[netip.AddrPort]struct{}
+       dialableOnlyAfterHolepunch                            map[netip.AddrPort]struct{}
+       dialedSuccessfullyAfterHolepunchConnect               map[netip.AddrPort]struct{}
+       probablyOnlyConnectedDueToHolepunch                   map[netip.AddrPort]struct{}
+       accepted                                              map[netip.AddrPort]struct{}
+}
+
 type ClientStats struct {
        ConnStats
 
@@ -8,10 +27,26 @@ type ClientStats struct {
        // if a Torrent is dropped while there are outstanding dials.
        ActiveHalfOpenAttempts int
 
+       NumPeersUndialableWithoutHolepunch int
        // Number of unique peer addresses that were dialed after receiving a holepunch connect message,
        // that have previously been undialable without any hole-punching attempts.
        NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect int
        // Number of unique peer addresses that were successfully dialed and connected after a holepunch
        // connect message and previously failing to connect without holepunching.
-       NumPeersDialableOnlyAfterHolepunch int
+       NumPeersDialableOnlyAfterHolepunch              int
+       NumPeersDialedSuccessfullyAfterHolepunchConnect int
+       NumPeersProbablyOnlyConnectedDueToHolepunch     int
+}
+
+func (cl *Client) statsLocked() (stats ClientStats) {
+       stats.ConnStats = cl.connStats.Copy()
+       stats.ActiveHalfOpenAttempts = cl.numHalfOpen
+
+       stats.NumPeersUndialableWithoutHolepunch = len(cl.undialableWithoutHolepunch)
+       stats.NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect = len(cl.undialableWithoutHolepunchDialedAfterHolepunchConnect)
+       stats.NumPeersDialableOnlyAfterHolepunch = len(cl.dialableOnlyAfterHolepunch)
+       stats.NumPeersDialedSuccessfullyAfterHolepunchConnect = len(cl.dialedSuccessfullyAfterHolepunchConnect)
+       stats.NumPeersProbablyOnlyConnectedDueToHolepunch = len(cl.probablyOnlyConnectedDueToHolepunch)
+
+       return
 }