Rename and improve downloaded chunk counters and status per connection

This commit is contained in:
Matt Joiner 2014-09-11 20:30:13 +10:00
parent 5d35e10706
commit c0d7b2fbf2
2 changed files with 11 additions and 4 deletions

View File

@ -1332,7 +1332,6 @@ func (me *Client) replenishConnRequests(t *torrent, c *connection) {
// Handle a received chunk from a peer. // Handle a received chunk from a peer.
func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error { func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error {
chunksDownloadedCount.Add(1) chunksDownloadedCount.Add(1)
c.ChunksReceived++
req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece))) req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
@ -1344,6 +1343,7 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
// Do we actually want this chunk? // Do we actually want this chunk?
if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok { if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok {
unusedDownloadedChunksCount.Add(1) unusedDownloadedChunksCount.Add(1)
c.UnwantedChunksReceived++
return nil return nil
} }

View File

@ -33,8 +33,8 @@ type connection struct {
post chan pp.Message post chan pp.Message
writeCh chan []byte writeCh chan []byte
ChunksReceived int UnwantedChunksReceived int
UsefulChunksReceived int UsefulChunksReceived int
lastMessageReceived time.Time lastMessageReceived time.Time
completedHandshake time.Time completedHandshake time.Time
@ -127,8 +127,15 @@ func (cn *connection) setNumPieces(num int) error {
return nil return nil
} }
func eventAgeString(t time.Time) string {
if t.IsZero() {
return "never"
}
return fmt.Sprintf("%.2fs ago", time.Now().Sub(t).Seconds())
}
func (cn *connection) WriteStatus(w io.Writer) { func (cn *connection) WriteStatus(w io.Writer) {
fmt.Fprintf(w, "%-90s: %s completed, good chunks: %d/%d reqs: %d-%d, last msg: %.0fs ago age: %.1fmin last useful chunk: %s ago flags: ", fmt.Sprintf("%q: %s-%s", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr()), cn.completedString(), cn.UsefulChunksReceived, cn.ChunksReceived, len(cn.Requests), len(cn.PeerRequests), time.Now().Sub(cn.lastMessageReceived).Seconds(), time.Now().Sub(cn.completedHandshake).Minutes(), time.Now().Sub(cn.lastUsefulChunkReceived)) fmt.Fprintf(w, "%-90s: %s completed, good chunks: %d/%d reqs: %d-%d, last msg: %s, connected: %s, last useful chunk: %s, flags: ", fmt.Sprintf("%q: %s-%s", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr()), cn.completedString(), cn.UsefulChunksReceived, cn.UnwantedChunksReceived+cn.UsefulChunksReceived, len(cn.Requests), len(cn.PeerRequests), eventAgeString(cn.lastMessageReceived), eventAgeString(cn.completedHandshake), eventAgeString(cn.lastUsefulChunkReceived))
c := func(b byte) { c := func(b byte) {
fmt.Fprintf(w, "%c", b) fmt.Fprintf(w, "%c", b)
} }