From: Matt Joiner Date: Thu, 8 Feb 2024 07:07:32 +0000 (+1100) Subject: Stay interested when the peer has pieces we don't have X-Git-Tag: v1.54.0~4 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c73c99b372114204b3e62ab7fe7103b2477c7dee;p=btrtrc.git Stay interested when the peer has pieces we don't have --- 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 }