Merge pull request #5 from h31p/dynamic_readahead

dynamically adjust readahead, for nearly sequential download
This commit is contained in:
YouROK
2019-11-13 19:09:47 +03:00
committed by GitHub
2 changed files with 20 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import (
"sort" "sort"
"sync" "sync"
"time" "time"
"fmt"
"server/settings" "server/settings"
"server/utils" "server/utils"
@@ -184,6 +185,17 @@ func (t *Torrent) progressEvent() {
} }
t.muTorrent.Unlock() t.muTorrent.Unlock()
t.lastTimeSpeed = time.Now() t.lastTimeSpeed = time.Now()
if (t.BytesReadUsefulData > settings.Get().PreloadBufferSize) {
adj := int64((int(t.cache.GetState().PiecesLength) * t.Torrent.Stats().ActivePeers) / (1 + t.cache.ReadersLen()))
switch {
case adj < t.cache.GetState().PiecesLength:
adj = t.cache.GetState().PiecesLength
case adj > t.cache.GetState().PiecesLength * 4:
adj = t.cache.GetState().PiecesLength * 4
}
t.cache.AdjustRA(adj)
log.Println("Status:", t.Name(), "S:", fmt.Sprintf("%8s", utils.Format(t.DownloadSpeed)), "P:", fmt.Sprintf("%2d", t.Torrent.Stats().ActivePeers), "/", fmt.Sprintf("%2d", t.Torrent.Stats().TotalPeers), "R:", t.cache.ReadersLen(), "RA:", utils.Format(float64(adj)))
}
} }
func (t *Torrent) expired() bool { func (t *Torrent) expired() bool {
@@ -223,7 +235,7 @@ func (t *Torrent) NewReader(file *torrent.File, readahead int64) *reader.Reader
defer t.muReader.Unlock() defer t.muReader.Unlock()
reader := reader.NewReader(file) reader := reader.NewReader(file)
if readahead <= 0 { if readahead <= 0 {
readahead = utils.GetReadahead() readahead = t.cache.GetState().PiecesLength
} }
reader.SetReadahead(readahead) reader.SetReadahead(readahead)
t.cache.AddReader(reader) t.cache.AddReader(reader)

View File

@@ -211,3 +211,10 @@ func (c *Cache) ReadersLen() int {
} }
return len(c.readers) return len(c.readers)
} }
func (c *Cache) AdjustRA(readahead int64) {
for r, _ := range c.readers {
r.SetReadahead(readahead)
}
}