// 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()
}
"errors"
"fmt"
"io"
+ "math/rand"
"net/netip"
"net/url"
"sort"
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
// 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())
}
func (t *Torrent) numPieces() pieceIndex {
- return pieceIndex(t.info.NumPieces())
+ return t.info.NumPieces()
}
func (t *Torrent) numPiecesCompleted() (num pieceIndex) {