From 110aa733a4c646a1a801b1299e1bf495c428c4ab Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 29 May 2014 01:30:59 +1000 Subject: [PATCH] Make the readahead on ResponsiveDownloadStrategy customizable --- client.go | 12 +++++++++--- cmd/torrentfs/main.go | 2 +- fs/torrentfs_test.go | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index 31ecc9f1..e079881a 100644 --- 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 } diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index 8ffeee73..95ff7177 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -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) diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 47662c25..be85e473 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -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() -- 2.48.1