From: Matt Joiner <anacrolix@gmail.com>
Date: Fri, 3 May 2024 03:46:35 +0000 (+1000)
Subject: Add type updateRequestReason and comments
X-Git-Tag: v1.56.0~7
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a9a5af51b7c087fbdf364ddf29147891518faebd;p=btrtrc.git

Add type updateRequestReason and comments
---

diff --git a/peer.go b/peer.go
index 41749710..55e8f19f 100644
--- 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
 	}
diff --git a/requesting.go b/requesting.go
index c5c797e7..8b57b21d 100644
--- a/requesting.go
+++ b/requesting.go
@@ -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 836ebc00..ba73d518 100644
--- 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 {
diff --git a/torrent.go b/torrent.go
index dc586770..dfb980ba 100644
--- a/torrent.go
+++ b/torrent.go
@@ -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)