From 8e9cb9f2be69498a95cd784df5f0a1f6f6664c09 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 14 May 2021 10:24:50 +1000 Subject: [PATCH] Do checks for preallocated requests too Otherwise we reserve requests with the assumption that they can be assigned later, and they actually might not be. --- request-strategy/order.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/request-strategy/order.go b/request-strategy/order.go index c16cb3ea..56002594 100644 --- a/request-strategy/order.go +++ b/request-strategy/order.go @@ -187,11 +187,18 @@ func allocatePendingChunks(p pieceRequestOrderPiece, peers []*requestsPeer) { preallocated := make(map[ChunkSpec]*peersForPieceRequests, p.NumPendingChunks) p.iterPendingChunksWrapper(func(spec ChunkSpec) { req := Request{pp.Integer(p.index), spec} - for _, p := range peersForPiece { - if h := p.HasExistingRequest; h != nil && h(req) { - preallocated[spec] = p - p.addNextRequest(req) + for _, peer := range peersForPiece { + if h := peer.HasExistingRequest; h == nil || !h(req) { + continue } + if !peer.canFitRequest() { + continue + } + if !peer.canRequestPiece(p.index) { + continue + } + preallocated[spec] = peer + peer.addNextRequest(req) } }) pendingChunksRemaining := int(p.NumPendingChunks)