]> Sergey Matveev's repositories - btrtrc.git/blob - client-stats.go
Drop support for go 1.20
[btrtrc.git] / client-stats.go
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         ConnStats
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.ConnStats = cl.connStats.Copy()
43         stats.ActiveHalfOpenAttempts = cl.numHalfOpen
44
45         stats.NumPeersUndialableWithoutHolepunch = len(cl.undialableWithoutHolepunch)
46         stats.NumPeersUndialableWithoutHolepunchDialedAfterHolepunchConnect = len(cl.undialableWithoutHolepunchDialedAfterHolepunchConnect)
47         stats.NumPeersDialableOnlyAfterHolepunch = len(cl.dialableOnlyAfterHolepunch)
48         stats.NumPeersDialedSuccessfullyAfterHolepunchConnect = len(cl.dialedSuccessfullyAfterHolepunchConnect)
49         stats.NumPeersProbablyOnlyConnectedDueToHolepunch = len(cl.probablyOnlyConnectedDueToHolepunch)
50
51         return
52 }