From: Matt Joiner Date: Wed, 11 May 2022 10:40:58 +0000 (+1000) Subject: Add fallback piece ordering for non-readahead priorities X-Git-Tag: v1.43.0~1 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=0235dd38013df9e8deaf77a2f5656a6cb96ebbc0;p=btrtrc.git Add fallback piece ordering for non-readahead priorities --- diff --git a/requesting.go b/requesting.go index f1831346..b781316d 100644 --- a/requesting.go +++ b/requesting.go @@ -156,9 +156,7 @@ func (p *desiredPeerRequests) lessByValue(leftRequest, rightRequest RequestIndex // torrent. This would probably require reconsideration of how readahead priority works. ml = ml.Int(leftPieceIndex, rightPieceIndex) } else { - // TODO: To prevent unnecessarily requesting from disparate pieces, and to ensure pieces are - // selected randomly when availability is even, there should be some fixed ordering of - // pieces. + ml = ml.Int(t.pieceRequestOrder[leftPieceIndex], t.pieceRequestOrder[rightPieceIndex]) } return ml.Less() } diff --git a/torrent.go b/torrent.go index dbfbea18..55ba2230 100644 --- a/torrent.go +++ b/torrent.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "math/rand" "net/netip" "net/url" "sort" @@ -62,6 +63,9 @@ type Torrent struct { closed chansync.SetOnce infoHash metainfo.Hash pieces []Piece + + // The order pieces are requested if there's no stronger reason like availability or priority. + pieceRequestOrder []int // Values are the piece indices that changed. pieceStateChanges pubsub.PubSub[PieceStateChange] // The size of chunks to request from peers over the wire. This is @@ -459,6 +463,7 @@ func (t *Torrent) pieceRequestOrderKey(i int) request_strategy.PieceRequestOrder // This seems to be all the follow-up tasks after info is set, that can't fail. func (t *Torrent) onSetInfo() { + t.pieceRequestOrder = rand.Perm(t.numPieces()) t.initPieceRequestOrder() MakeSliceWithLength(&t.requestState, t.numChunks()) MakeSliceWithLength(&t.requestPieceStates, t.numPieces()) @@ -849,7 +854,7 @@ func (t *Torrent) usualPieceSize() int { } func (t *Torrent) numPieces() pieceIndex { - return pieceIndex(t.info.NumPieces()) + return t.info.NumPieces() } func (t *Torrent) numPiecesCompleted() (num pieceIndex) {