]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add type updateRequestReason and comments
authorMatt Joiner <anacrolix@gmail.com>
Fri, 3 May 2024 03:46:35 +0000 (13:46 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 3 May 2024 03:46:35 +0000 (13:46 +1000)
peer.go
requesting.go
t.go
torrent.go

diff --git a/peer.go b/peer.go
index 41749710772c6f751868c242e730c8a85be50b10..55e8f19fa7cd3e241e7d0976d4e6422d741e21d2 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -57,7 +57,7 @@ type (
                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 @@ type (
        }
 
        peerRequests = orderedBitmap[RequestIndex]
+
+       updateRequestReason string
 )
 
 const (
@@ -124,6 +126,15 @@ const (
        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 @@ func (cn *Peer) request(r RequestIndex) (more bool, err error) {
        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 @@ func (me *Peer) cancel(r RequestIndex) {
 }
 
 // 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
        }
@@ -568,8 +577,6 @@ func runSafeExtraneous(f func()) {
        }
 }
 
-var peerUpdateRequestsRemoteRejectReason = "Peer.remoteRejectedRequest"
-
 // Returns true if it was valid to reject the request.
 func (c *Peer) remoteRejectedRequest(r RequestIndex) bool {
        if c.deleteRequest(r) {
@@ -790,7 +797,7 @@ func (c *Peer) deleteRequest(r RequestIndex) bool {
        return true
 }
 
-func (c *Peer) deleteAllRequests(reason string) {
+func (c *Peer) deleteAllRequests(reason updateRequestReason) {
        if c.requestState.Requests.IsEmpty() {
                return
        }
index c5c797e7ebf52529d05e4d426f4226273fa52ee7..8b57b21d9dd0733a35420dbdfd762042ce3c5908 100644 (file)
@@ -244,7 +244,7 @@ func (p *Peer) maybeUpdateActualRequestState() {
        }
        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 (file)
--- a/t.go
+++ b/t.go
@@ -199,7 +199,7 @@ func (t *Torrent) CancelPieces(begin, end pieceIndex) {
        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 {
index dc586770dcd6b212b70d1b612f2ccc225f764b19..dfb980baefc74174bce92dca6d5ac65a5331d4c9 100644 (file)
@@ -1417,7 +1417,7 @@ func (t *Torrent) maybeNewConns() {
        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 @@ func (t *Torrent) updatePiecePriorityNoTriggers(piece pieceIndex) (pendingChange
        }
 }
 
-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)