From 0625231cb29ed4a293f99913e2df4fab46ff0322 Mon Sep 17 00:00:00 2001 From: ShitizZip <124110010+ShitizZip@users.noreply.github.com> Date: Wed, 3 Apr 2024 20:19:14 +1100 Subject: [PATCH] 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. --- vfs/vfscache/downloaders/downloaders.go | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/vfs/vfscache/downloaders/downloaders.go b/vfs/vfscache/downloaders/downloaders.go index 2546afdd9..19fbac35f 100644 --- a/vfs/vfscache/downloaders/downloaders.go +++ b/vfs/vfscache/downloaders/downloaders.go @@ -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 {