peer.go | 21 ++++++++++++++------- requesting.go | 2 +- t.go | 2 +- torrent.go | 10 +++++----- diff --git a/peer.go b/peer.go index 41749710772c6f751868c242e730c8a85be50b10..55e8f19fa7cd3e241e7d0976d4e6422d741e21d2 100644 --- a/peer.go +++ b/peer.go @@ -57,7 +57,7 @@ lastUsefulChunkReceived time.Time lastChunkSent time.Time // Stuff controlled by the local peer. - needRequestUpdate string + needRequestUpdate updateRequestReason requestState request_strategy.PeerRequestState updateRequestsTimer *time.Timer lastRequestUpdate time.Time @@ -111,6 +111,8 @@ String() string } peerRequests = orderedBitmap[RequestIndex] + + updateRequestReason string ) const ( @@ -124,6 +126,15 @@ // The peer was given directly, such as through a magnet link. PeerSourceDirect = "M" ) +// These are grouped because we might vary update request behaviour depending on the reason. I'm not +// sure about the fact that multiple reasons can be triggered before an update runs, and only the +// first will count. Possibly we should be signalling what behaviours are appropriate in the next +// update instead. +const ( + peerUpdateRequestsPeerCancelReason updateRequestReason = "Peer.cancel" + peerUpdateRequestsRemoteRejectReason updateRequestReason = "Peer.remoteRejectedRequest" +) + // Returns the Torrent a Peer belongs to. Shouldn't change for the lifetime of the Peer. May be nil // if we are the receiving end of a connection and the handshake hasn't been received or accepted // yet. @@ -468,8 +479,6 @@ } return cn.peerImpl._request(ppReq), nil } -var peerUpdateRequestsPeerCancelReason = "Peer.cancel" - func (me *Peer) cancel(r RequestIndex) { if !me.deleteRequest(r) { panic("request not existing should have been guarded") @@ -487,7 +496,7 @@ } } // Sets a reason to update requests, and if there wasn't already one, handle it. -func (cn *Peer) updateRequests(reason string) { +func (cn *Peer) updateRequests(reason updateRequestReason) { if cn.needRequestUpdate != "" { return } @@ -567,8 +576,6 @@ } else { f() } } - -var peerUpdateRequestsRemoteRejectReason = "Peer.remoteRejectedRequest" // Returns true if it was valid to reject the request. func (c *Peer) remoteRejectedRequest(r RequestIndex) bool { @@ -790,7 +797,7 @@ // }) return true } -func (c *Peer) deleteAllRequests(reason string) { +func (c *Peer) deleteAllRequests(reason updateRequestReason) { if c.requestState.Requests.IsEmpty() { return } diff --git a/requesting.go b/requesting.go index c5c797e7ebf52529d05e4d426f4226273fa52ee7..8b57b21d9dd0733a35420dbdfd762042ce3c5908 100644 --- a/requesting.go +++ b/requesting.go @@ -244,7 +244,7 @@ } } pprof.Do( context.Background(), - pprof.Labels("update request", p.needRequestUpdate), + pprof.Labels("update request", string(p.needRequestUpdate)), func(_ context.Context) { next := p.getDesiredRequestState() p.applyRequestState(next) diff --git a/t.go b/t.go index 836ebc00749a296f69fb1128bddbe8303b7547ff..ba73d51851ff3411baf1885cd222b67bab1f7100 100644 --- a/t.go +++ b/t.go @@ -199,7 +199,7 @@ t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces") t.cl.unlock() } -func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) { +func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason updateRequestReason) { for i := begin; i < end; i++ { p := &t.pieces[i] if p.priority == PiecePriorityNone { diff --git a/torrent.go b/torrent.go index dc586770dcd6b212b70d1b612f2ccc225f764b19..dfb980baefc74174bce92dca6d5ac65a5331d4c9 100644 --- a/torrent.go +++ b/torrent.go @@ -1417,7 +1417,7 @@ t.cl.event.Broadcast() t.openNewConns() } -func (t *Torrent) onPiecePendingTriggers(piece pieceIndex, reason string) { +func (t *Torrent) onPiecePendingTriggers(piece pieceIndex, reason updateRequestReason) { if t._pendingPieces.Contains(uint32(piece)) { t.iterPeers(func(c *Peer) { // if c.requestState.Interested { @@ -1455,20 +1455,20 @@ return t._pendingPieces.CheckedAdd(uint32(piece)) } } -func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) { +func (t *Torrent) updatePiecePriority(piece pieceIndex, reason updateRequestReason) { if t.updatePiecePriorityNoTriggers(piece) && !t.disableTriggers { t.onPiecePendingTriggers(piece, reason) } t.updatePieceRequestOrderPiece(piece) } -func (t *Torrent) updateAllPiecePriorities(reason string) { +func (t *Torrent) updateAllPiecePriorities(reason updateRequestReason) { t.updatePiecePriorities(0, t.numPieces(), reason) } // Update all piece priorities in one hit. This function should have the same // output as updatePiecePriority, but across all pieces. -func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) { +func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason updateRequestReason) { for i := begin; i < end; i++ { t.updatePiecePriority(i, reason) } @@ -1514,7 +1514,7 @@ func (t *Torrent) pendRequest(req RequestIndex) { t.piece(t.pieceIndexOfRequestIndex(req)).pendChunkIndex(req % t.chunksPerRegularPiece()) } -func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) { +func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason updateRequestReason) { t.cl.event.Broadcast() if t.pieceComplete(piece) { t.onPieceCompleted(piece)