Add fallback piece ordering for non-readahead priorities

This commit is contained in:
Matt Joiner 2022-05-11 20:40:58 +10:00
parent 9f9b02e3dd
commit 0235dd3801
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 7 additions and 4 deletions

View File

@ -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()
}

View File

@ -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) {