vfs: Fix multiple oversized downloaders when mounting with --vfs-read-ahead

When mounting with --vfs-read-ahead option, if a file is accessed at multiple offsets (for example, by a video player like Plex to read audio and video tracks) then each corresponding downloader would have the ReadAhead added to its offset, killing performance.

This fix would shrink old downloaders and only keep the latest one with enhanced offset.
This commit is contained in:
ShitizZip 2024-04-03 20:19:14 +11:00
parent 76f3eb3ed2
commit 0625231cb2

View File

@ -363,6 +363,32 @@ func (dls *Downloaders) _ensureDownloader(r ranges.Range) (err error) {
if r.Size == 0 {
return nil
}
// Shrink existing downloaders to prevent doubling up of ReadAhead in case of multiple file threads / accesses
if dls.opt.ReadAhead > 0 {
for _, dl = range dls.dls {
if dl._closed {
continue
}
if dl.maxOffset-dl.start <= window {
continue
}
// Adjust this range to shrink without ReadAhead value
dl.mu.Lock()
dl.maxOffset -= int64(dls.opt.ReadAhead)
if dl.maxOffset < dl.start {
dl.maxOffset = dl.start
}
dl.mu.Unlock()
select {
case dl.kick <- struct{}{}:
default:
}
}
}
// Downloader not found so start a new one
_, err = dls._newDownloader(r)
if err != nil {