-
-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathconn-stats.go
More file actions
92 lines (75 loc) · 2.43 KB
/
conn-stats.go
File metadata and controls
92 lines (75 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package torrent
import (
"io"
pp "github.com/anacrolix/torrent/peer_protocol"
)
// Various connection-level metrics. At the Torrent level these are aggregates. Chunks are messages
// with data payloads. Data is actual torrent content without any overhead. Useful is something we
// needed locally. Intended is something we were expecting (I think such as when we cancel a request
// but it arrives anyway). Written is things sent to the peer, and Read is stuff received from them.
// Due to the implementation of Count, must be aligned on some platforms: See
// https://github.com/anacrolix/torrent/issues/262.
type ConnStats struct {
// Total bytes on the wire. Includes handshakes and encryption.
BytesWritten Count
BytesWrittenData Count
BytesRead Count
BytesReadData Count
BytesReadUsefulData Count
BytesReadUsefulIntendedData Count
ChunksWritten Count
ChunksRead Count
ChunksReadUseful Count
ChunksReadWasted Count
MetadataChunksRead Count
// Number of pieces data was written to, that subsequently passed verification.
PiecesDirtiedGood Count
// Number of pieces data was written to, that subsequently failed verification. Note that a
// connection may not have been the sole dirtier of a piece.
PiecesDirtiedBad Count
}
func (me *ConnStats) Copy() (ret ConnStats) {
return copyCountFields(me)
}
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
// TODO: Track messages and not just chunks.
switch msg.Type {
case pp.Piece:
cs.ChunksWritten.Add(1)
cs.BytesWrittenData.Add(int64(len(msg.Piece)))
}
}
func (cs *ConnStats) receivedChunk(size int64) {
cs.ChunksRead.Add(1)
cs.BytesReadData.Add(size)
}
func (cs *ConnStats) incrementPiecesDirtiedGood() bool {
cs.PiecesDirtiedGood.Add(1)
// This method is used as an iterator and should never return early.
return true
}
func (cs *ConnStats) incrementPiecesDirtiedBad() bool {
cs.PiecesDirtiedBad.Add(1)
// This method is used as an iterator and should never return early.
return true
}
func add(n int64, f func(*ConnStats) *Count) func(*ConnStats) {
return func(cs *ConnStats) {
p := f(cs)
p.Add(n)
}
}
type connStatsReadWriter struct {
rw io.ReadWriter
c *PeerConn
}
func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
n, err = me.rw.Write(b)
me.c.wroteBytes(int64(n))
return
}
func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
n, err = me.rw.Read(b)
me.c.readBytes(int64(n))
return
}