]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Only use last requested when both requests are already pending
authorMatt Joiner <anacrolix@gmail.com>
Tue, 7 Dec 2021 03:17:43 +0000 (14:17 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 12 Dec 2021 07:35:01 +0000 (18:35 +1100)
requesting.go

index f3cd868091f22649c31f19f9819d43925b54f774..fdf638fef80b2806f26ba97f742a4cb955b4cfa4 100644 (file)
@@ -99,12 +99,22 @@ func (p *peerRequests) Less(i, j int) bool {
                return ml.MustLess()
        }
        if leftPeer != nil {
+               // The right peer should also be set, or we'd have resolved the computation by now.
                ml = ml.Uint64(
                        rightPeer.actualRequestState.Requests.GetCardinality(),
                        leftPeer.actualRequestState.Requests.GetCardinality(),
                )
+               // Could either of the lastRequested be Zero? That's what checking an existing peer is for.
+               leftLast := t.lastRequested[leftRequest]
+               rightLast := t.lastRequested[rightRequest]
+               if leftLast.IsZero() || rightLast.IsZero() {
+                       panic("expected non-zero last requested times")
+               }
+               // We want the most-recently requested on the left. Clients like Transmission serve requests
+               // in received order, so the most recently-requested is the one that has the longest until
+               // it will be served and therefore is the best candidate to cancel.
+               ml = ml.CmpInt64(rightLast.Sub(leftLast).Nanoseconds())
        }
-       ml = ml.CmpInt64(t.lastRequested[rightRequest].Sub(t.lastRequested[leftRequest]).Nanoseconds())
        leftPiece := t.piece(int(leftPieceIndex))
        rightPiece := t.piece(int(rightPieceIndex))
        ml = ml.Int(