From c73c99b372114204b3e62ab7fe7103b2477c7dee Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 8 Feb 2024 18:07:32 +1100 Subject: [PATCH] Stay interested when the peer has pieces we don't have --- requesting.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/requesting.go b/requesting.go index b70f2645..fdb0ad79 100644 --- a/requesting.go +++ b/requesting.go @@ -9,6 +9,7 @@ import ( "time" "unsafe" + "github.com/RoaringBitmap/roaring" "github.com/anacrolix/generics/heap" "github.com/anacrolix/log" "github.com/anacrolix/multiless" @@ -248,9 +249,31 @@ func (p *Peer) maybeUpdateActualRequestState() { ) } +// Whether we should allow sending not interested ("losing interest") to the peer. I noticed +// qBitTorrent seems to punish us for sending not interested when we're streaming and don't +// currently need anything. +func (p *Peer) allowSendNotInterested() bool { + // Except for caching, we're not likely to lose pieces very soon. + if p.t.haveAllPieces() { + return true + } + all, known := p.peerHasAllPieces() + if all || !known { + return false + } + // Allow losing interest if we have all the pieces the peer has. + return roaring.AndNot(p.peerPieces(), &p.t._completedPieces).IsEmpty() +} + // Transmit/action the request state to the peer. func (p *Peer) applyRequestState(next desiredRequestState) { current := &p.requestState + // Make interest sticky + if !next.Interested && p.requestState.Interested { + if !p.allowSendNotInterested() { + next.Interested = true + } + } if !p.setInterested(next.Interested) { return } -- 2.48.1