client-stats.go 2.3 KB

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