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.
This commit is contained in:
Matt Joiner 2021-05-14 10:24:50 +10:00
parent 9dd85e5bd2
commit 8e9cb9f2be
1 changed files with 11 additions and 4 deletions

View File

@ -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)