]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Make the readahead on ResponsiveDownloadStrategy customizable
authorMatt Joiner <anacrolix@gmail.com>
Wed, 28 May 2014 15:30:59 +0000 (01:30 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 28 May 2014 15:30:59 +0000 (01:30 +1000)
client.go
cmd/torrentfs/main.go
fs/torrentfs_test.go

index 31ecc9f15682b3990d158bda5371fc6e05c7374e..e079881ab2b66d06cde800fc70cd49e21715dcee 100644 (file)
--- a/client.go
+++ b/client.go
@@ -789,19 +789,24 @@ func (s *DefaultDownloadStrategy) DeleteRequest(t *torrent, r request) {
        m[r]--
 }
 
-type ResponsiveDownloadStrategy struct{}
+type ResponsiveDownloadStrategy struct {
+       // How many bytes to preemptively download starting at the beginning of
+       // the last piece read for a given torrent.
+       Readahead int
+}
 
 func (ResponsiveDownloadStrategy) TorrentStarted(*torrent)         {}
 func (ResponsiveDownloadStrategy) TorrentStopped(*torrent)         {}
 func (ResponsiveDownloadStrategy) DeleteRequest(*torrent, request) {}
 
-func (ResponsiveDownloadStrategy) FillRequests(t *torrent, c *connection) {
+func (me *ResponsiveDownloadStrategy) FillRequests(t *torrent, c *connection) {
        for e := t.Priorities.Front(); e != nil; e = e.Next() {
                if !c.Request(e.Value.(request)) {
                        return
                }
        }
-       for i := t.lastReadPiece; i < t.lastReadPiece+5 && i < t.NumPieces(); i++ {
+       readaheadPieces := (me.Readahead + t.UsualPieceSize() - 1) / t.UsualPieceSize()
+       for i := t.lastReadPiece; i < t.lastReadPiece+readaheadPieces && i < t.NumPieces(); i++ {
                for cs := range t.Pieces[i].PendingChunkSpecs {
                        if !c.Request(request{pp.Integer(i), cs}) {
                                return
@@ -810,6 +815,7 @@ func (ResponsiveDownloadStrategy) FillRequests(t *torrent, c *connection) {
        }
        // Then finish off incomplete pieces in order of bytes remaining.
        for _, index := range t.piecesByPendingBytes() {
+               // Stop when we're onto untouched pieces.
                if t.PieceNumPendingBytes(index) == t.PieceLength(index) {
                        break
                }
index 8ffeee73719b94ba5a4d1b748b26ab56c41649bf..95ff717732bb79f5bda49bb03865b256df3e8d26 100644 (file)
@@ -133,7 +133,7 @@ func main() {
        client := &torrent.Client{
                DataDir:          downloadDir,
                DisableTrackers:  *disableTrackers,
-               DownloadStrategy: torrent.ResponsiveDownloadStrategy{},
+               DownloadStrategy: &torrent.ResponsiveDownloadStrategy{2 * 1000 * 1024},
        }
        client.Start()
        addTorrentDir(client, torrentPath)
index 47662c2502144f4f829e94714489bad476a93b29..be85e473c2160548295ab28ed32cdc4f960b63c4 100644 (file)
@@ -139,7 +139,7 @@ func TestDownloadOnDemand(t *testing.T) {
        seeder.AddTorrent(layout.Metainfo)
        leecher := torrent.Client{
                DataDir:          filepath.Join(layout.BaseDir, "download"),
-               DownloadStrategy: torrent.ResponsiveDownloadStrategy{},
+               DownloadStrategy: &torrent.ResponsiveDownloadStrategy{},
        }
        leecher.Start()
        defer leecher.Stop()