This commit is contained in:
Shitiz Garg 2024-05-03 21:40:17 -05:00 committed by GitHub
commit dea972bd0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 0 deletions

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 {