From d72c464bf59c4744ab17af0002b1ad70a8e96cbc Mon Sep 17 00:00:00 2001 From: Mark Holt Date: Sat, 20 Apr 2024 08:17:09 +0100 Subject: [PATCH] cherry-pick --- requesting.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/requesting.go b/requesting.go index 8b9db971..117d447b 100644 --- a/requesting.go +++ b/requesting.go @@ -266,13 +266,25 @@ func (p *Peer) applyRequestState(next desiredRequestState) { } for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()+current.Cancelled.GetCardinality()) < p.nominalMaxRequests() { req := heap.Pop(requestHeap) + if cap(next.Requests.requestIndexes) != cap(orig) { + panic("changed") + } + + if p.needRequestUpdate == "Peer.remoteRejectedRequest" { + continue + } + existing := t.requestingPeer(req) if existing != nil && existing != p { + if p.needRequestUpdate == "Peer.cancel" { + continue + } + // Don't steal from the poor. diff := int64(current.Requests.GetCardinality()) + 1 - (int64(existing.uncancelledRequests()) - 1) // Steal a request that leaves us with one more request than the existing peer // connection if the stealer more recently received a chunk. - if diff > 1 || (diff == 1 && p.lastUsefulChunkReceived.Before(existing.lastUsefulChunkReceived)) { + if diff > 1 || (diff == 1 && !p.lastUsefulChunkReceived.After(existing.lastUsefulChunkReceived)) { continue } t.cancelRequest(req) -- 2.48.1