github.com/anacrolix/torrent@v1.61.0/client-stats.go (about)

     1  package torrent
     2  
     3  import (
     4  	"net/netip"
     5  
     6  	g "github.com/anacrolix/generics"
     7  )
     8  
     9  func setAdd[K comparable](m *map[K]struct{}, elem K) {
    10  	g.MakeMapIfNilAndSet(m, elem, struct{}{})
    11  }
    12  
    13  type clientHolepunchAddrSets struct {
    14  	undialableWithoutHolepunch                            map[netip.AddrPort]struct{}
    15  	undialableWithoutHolepunchDialedAfterHolepunchConnect map[netip.AddrPort]struct{}
    16  	dialableOnlyAfterHolepunch                            map[netip.AddrPort]struct{}
    17  	dialedSuccessfullyAfterHolepunchConnect               map[netip.AddrPort]struct{}
    18  	probablyOnlyConnectedDueToHolepunch                   map[netip.AddrPort]struct{}
    19  	accepted                                              map[netip.AddrPort]struct{}
    20  }
    21  
    22  type ClientStats struct {
    23  	TorrentStats
    24  
    25  	// Ongoing outgoing dial attempts. There may be more than one dial going on per peer address due
    26  	// to hole-punch connect requests. The total may not match the sum of attempts for all Torrents
    27  	// if a Torrent is dropped while there are outstanding dials.
    28  	ActiveHalfOpenAttempts int
    29  
    30  	NumPeersUndialableWithoutHolepunch int
    31  	// Number of unique peer addresses that were dialed after receiving a holepunch connect message,
    32  	// that have previously been undialable without any hole-punching attempts.
    33  	NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect int
    34  	// Number of unique peer addresses that were successfully dialed and connected after a holepunch
    35  	// connect message and previously failing to connect without holepunching.
    36  	NumPeersDialableOnlyAfterHolepunch              int
    37  	NumPeersDialedSuccessfullyAfterHolepunchConnect int
    38  	NumPeersProbablyOnlyConnectedDueToHolepunch     int
    39  }
    40  
    41  func (cl *Client) statsLocked() (stats ClientStats) {
    42  	stats.AllConnStats = cl.connStats.Copy()
    43  	stats.TorrentStatCounters = copyCountFields(&cl.counters)
    44  	for t := range cl.torrents {
    45  		stats.TorrentGauges.Add(t.gauges())
    46  	}
    47  
    48  	stats.ActiveHalfOpenAttempts = cl.numHalfOpen
    49  
    50  	stats.NumPeersUndialableWithoutHolepunch = len(cl.undialableWithoutHolepunch)
    51  	stats.NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect = len(cl.undialableWithoutHolepunchDialedAfterHolepunchConnect)
    52  	stats.NumPeersDialableOnlyAfterHolepunch = len(cl.dialableOnlyAfterHolepunch)
    53  	stats.NumPeersDialedSuccessfullyAfterHolepunchConnect = len(cl.dialedSuccessfullyAfterHolepunchConnect)
    54  	stats.NumPeersProbablyOnlyConnectedDueToHolepunch = len(cl.probablyOnlyConnectedDueToHolepunch)
    55  
    56  	return
    57  }