From fe8eeec5b519aa9e688998e17cac2fe3220d9c4e Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 19 Feb 2018 17:52:33 +0000 Subject: [PATCH] cache: improve efficiency with RangeOption and RangeSeek #1825 * All remotes now support RangeOption so remove SeekOption * Correct off by one error as RangeOption arguments are inclusive. * Use RangeSeek in preference to Seek if available --- backend/cache/handle.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/cache/handle.go b/backend/cache/handle.go index e0a106913..ef25ab313 100644 --- a/backend/cache/handle.go +++ b/backend/cache/handle.go @@ -389,7 +389,7 @@ func (w *worker) reader(offset, end int64, closeOpen bool) (io.ReadCloser, error r := w.rc if w.rc == nil { r, err = w.r.cacheFs().openRateLimited(func() (io.ReadCloser, error) { - return w.r.cachedObject.Object.Open(&fs.SeekOption{Offset: offset}, &fs.RangeOption{Start: offset, End: end}) + return w.r.cachedObject.Object.Open(&fs.RangeOption{Start: offset, End: end - 1}) }) if err != nil { return nil, err @@ -398,16 +398,18 @@ func (w *worker) reader(offset, end int64, closeOpen bool) (io.ReadCloser, error } if !closeOpen { - seekerObj, ok := r.(io.Seeker) - if ok { - _, err = seekerObj.Seek(offset, os.SEEK_SET) + if do, ok := r.(fs.RangeSeeker); ok { + _, err = do.RangeSeek(offset, os.SEEK_SET, end-offset) + return r, err + } else if do, ok := r.(io.Seeker); ok { + _, err = do.Seek(offset, os.SEEK_SET) return r, err } } _ = w.rc.Close() return w.r.cacheFs().openRateLimited(func() (io.ReadCloser, error) { - r, err = w.r.cachedObject.Object.Open(&fs.SeekOption{Offset: offset}, &fs.RangeOption{Start: offset, End: end}) + r, err = w.r.cachedObject.Object.Open(&fs.RangeOption{Start: offset, End: end - 1}) if err != nil { return nil, err }