]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Randomize request order
authorMatt Joiner <anacrolix@gmail.com>
Mon, 18 Oct 2021 10:52:09 +0000 (21:52 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 19 Oct 2021 03:08:56 +0000 (14:08 +1100)
requesting.go

index b551a48f79bd375c1fc76139239d040ad5b1800a..820cafaaa8dfe63681d899bd0bd069eb3c7a2846 100644 (file)
@@ -4,6 +4,7 @@ import (
        "container/heap"
        "context"
        "encoding/gob"
+       "math/rand"
        "reflect"
        "runtime/pprof"
        "time"
@@ -266,11 +267,15 @@ func (p *Peer) applyRequestState(next requestState) bool {
        if !more {
                return false
        }
-       next.Requests.Iterate(func(req uint32) bool {
+       for _, x := range rand.Perm(int(next.Requests.GetCardinality())) {
+               req, err := next.Requests.Select(uint32(x))
+               if err != nil {
+                       panic(err)
+               }
                if p.cancelledRequests.Contains(req) {
                        // Waiting for a reject or piece message, which will suitably trigger us to update our
                        // requests, so we can skip this one with no additional consideration.
-                       return true
+                       continue
                }
                if maxRequests(current.Requests.GetCardinality()) >= p.nominalMaxRequests() {
                        //log.Printf("not assigning all requests [desired=%v, cancelled=%v, current=%v, max=%v]",
@@ -279,15 +284,16 @@ func (p *Peer) applyRequestState(next requestState) bool {
                        //      current.Requests.GetCardinality(),
                        //      p.nominalMaxRequests(),
                        //)
-                       return false
+                       break
                }
-               var err error
                more, err = p.request(req)
                if err != nil {
                        panic(err)
                }
-               return more
-       })
+               if !more {
+                       break
+               }
+       }
        p.updateRequestsTimer.Stop()
        if more {
                p.needRequestUpdate = ""