Merge 0625231cb29ed4a293f99913e2df4fab46ff0322 into 4d38424e6cbb32d07c74d3b4d760af7afb35742d

This commit is contained in:
Shitiz Garg 2025-03-20 06:02:15 -04:00 committed by GitHub
commit 0c42d096da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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 {