]> Sergey Matveev's repositories - btrtrc.git/blobdiff - requesting.go
Only steal an odd request if the stealer more recently received a chunk
[btrtrc.git] / requesting.go
index fdf638fef80b2806f26ba97f742a4cb955b4cfa4..aaaa4f9cfc7d09b04656d5cb4882dcab96e9add2 100644 (file)
@@ -231,7 +231,14 @@ func (p *Peer) applyRequestState(next desiredRequestState) bool {
                        continue
                }
                existing := t.requestingPeer(req)
-               if existing != nil && existing != p && existing.uncancelledRequests() > current.Requests.GetCardinality() {
+               if existing != nil && existing != p {
+                       // 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)) {
+                               continue
+                       }
                        t.cancelRequest(req)
                }
                more = p.mustRequest(req)