]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Do checks for preallocated requests too
authorMatt Joiner <anacrolix@gmail.com>
Fri, 14 May 2021 00:24:50 +0000 (10:24 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 7 Jun 2021 03:01:39 +0000 (13:01 +1000)
Otherwise we reserve requests with the assumption that they can be assigned later, and they actually might not be.

request-strategy/order.go

index c16cb3ea9c533244f8f5080ec6761187c83432cf..56002594f9a03c9a6069b1b7bb813a1436356d8a 100644 (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)