Skip to content

Commit

Permalink
Track chunks read for webseed peers too
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Jun 7, 2021
1 parent 33d3a75 commit c895a21
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
11 changes: 3 additions & 8 deletions conn_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,9 @@ func (cs *ConnStats) wroteMsg(msg *pp.Message) {
}
}

func (cs *ConnStats) readMsg(msg *pp.Message) {
// We want to also handle extended metadata pieces here, but we wouldn't
// have decoded the extended payload yet.
switch msg.Type {
case pp.Piece:
cs.ChunksRead.Add(1)
cs.BytesReadData.Add(int64(len(msg.Piece)))
}
func (cs *ConnStats) receivedChunk(size int64) {
cs.ChunksRead.Add(1)
cs.BytesReadData.Add(size)
}

func (cs *ConnStats) incrementPiecesDirtiedGood() {
Expand Down
15 changes: 8 additions & 7 deletions peerconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,6 @@ func (cn *PeerConn) wroteMsg(msg *pp.Message) {
cn.allStats(func(cs *ConnStats) { cs.wroteMsg(msg) })
}

func (cn *PeerConn) readMsg(msg *pp.Message) {
cn.allStats(func(cs *ConnStats) { cs.readMsg(msg) })
}

// After handshake, we know what Torrent and Client stats to include for a
// connection.
func (cn *Peer) postHandshakeStats(f func(*ConnStats)) {
Expand Down Expand Up @@ -1065,7 +1061,6 @@ func (c *PeerConn) mainReadLoop() (err error) {
if err != nil {
return err
}
c.readMsg(&msg)
c.lastMessageReceived = time.Now()
if msg.Keepalive {
receivedKeepalives.Add(1)
Expand Down Expand Up @@ -1105,6 +1100,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
r := newRequestFromMessage(&msg)
err = c.onReadRequest(r)
case pp.Piece:
c.doChunkReadStats(int64(len(msg.Piece)))
err = c.receiveChunk(&msg)
if len(msg.Piece) == int(t.chunkSize) {
t.chunkPool.Put(&msg.Piece)
Expand Down Expand Up @@ -1253,10 +1249,12 @@ func (cn *PeerConn) rw() io.ReadWriter {
}{cn.r, cn.w}
}

func (c *Peer) doChunkReadStats(size int64) {
c.allStats(func(cs *ConnStats) { cs.receivedChunk(size) })
}

// Handle a received chunk from a peer.
func (c *Peer) receiveChunk(msg *pp.Message) error {
t := c.t
cl := t.cl
chunksReceived.Add("total", 1)

req := newRequestFromMessage(msg)
Expand Down Expand Up @@ -1296,6 +1294,9 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
}
}

t := c.t
cl := t.cl

// Do we actually want this chunk?
if t.haveChunk(req) {
chunksReceived.Add("wasted", 1)
Expand Down
4 changes: 4 additions & 0 deletions webseed-peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (ws *webseedPeer) onClose() {

func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
result := <-webseedRequest.Result
// We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
// sure if we can divine which errors indicate cancellation on our end without hitting the
// network though.
ws.peer.doChunkReadStats(int64(len(result.Bytes)))
ws.peer.t.cl.lock()
defer ws.peer.t.cl.unlock()
if result.Err != nil {
Expand Down

0 comments on commit c895a21

Please sign in to comment.