panic(fmt.Sprintf("can't read from %T", d))
}
+func readaheadPieces(readahead, pieceLength int64) int {
+ return int((readahead+pieceLength-1)/pieceLength - 1)
+}
+
func (cl *Client) readRaisePiecePriorities(t *torrent, off int64) {
index := int(off / int64(t.usualPieceSize()))
cl.raisePiecePriority(t, index, piecePriorityNow)
return
}
cl.raisePiecePriority(t, index, piecePriorityNext)
- for i := 0; i < t.numConnsUnchoked()/2; i++ {
+ for i := 0; i < readaheadPieces(5*1024*1024, t.Info.PieceLength); i++ {
index++
if index >= t.numPieces() {
break
t.Fatal(":(")
}
}
+
+func TestReadaheadPieces(t *testing.T) {
+ for _, case_ := range []struct {
+ readaheadBytes, pieceLength int64
+ readaheadPieces int
+ }{
+ {5 * 1024 * 1024, 256 * 1024, 19},
+ {5 * 1024 * 1024, 5 * 1024 * 1024, 0},
+ {5*1024*1024 - 1, 5 * 1024 * 1024, 0},
+ {5 * 1024 * 1024, 5*1024*1024 - 1, 1},
+ {0, 5 * 1024 * 1024, -1},
+ {5 * 1024 * 1024, 1048576, 4},
+ } {
+ if readaheadPieces(case_.readaheadBytes, case_.pieceLength) != case_.readaheadPieces {
+ t.Fatalf("case failed: %s", case_)
+ }
+ }
+}