From 4ce06bef00c88a356416a40e4dce7988d1db0f2d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 18 Oct 2021 21:52:09 +1100 Subject: [PATCH] Randomize request order --- requesting.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/requesting.go b/requesting.go index b551a48f..820cafaa 100644 --- a/requesting.go +++ b/requesting.go @@ -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 = "" -- 2.48.1