This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize retrieval to do minimal fetcher from singularity
Read HTTP range headers and do fetch for entire range instead of allowing the io.CopyN, used by http.ServeContent, to do multiple small fetches. Fixes #143
- Loading branch information
Showing
2 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package singularity | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
// rangeReader reads data from one individually retrievable file range. | ||
type rangeReader struct { | ||
// offset is the absolute offset within file where the next read will get | ||
// data from. | ||
offset int64 | ||
reader io.ReadCloser | ||
remaining int64 | ||
} | ||
|
||
func (rr *rangeReader) writeToN(w io.Writer, readLen int64) (int64, error) { | ||
var read int64 | ||
for readLen > 0 { | ||
if rr.remaining == 0 { | ||
return read, io.EOF | ||
} | ||
var n int64 | ||
var err error | ||
|
||
if readLen >= rr.remaining { | ||
// Copy all remaining bytes. | ||
n, err = io.Copy(w, rr.reader) | ||
} else { | ||
// Copy requested number of bytes. | ||
n, err = io.CopyN(w, rr.reader, readLen) | ||
} | ||
if err != nil && !errors.Is(err, io.EOF) { | ||
return 0, err | ||
} | ||
if n == 0 { | ||
// Must have been EOF. | ||
rr.remaining = 0 | ||
return read, io.EOF | ||
} | ||
rr.offset += n | ||
rr.remaining -= n | ||
readLen -= n | ||
read += n | ||
} | ||
return read, nil | ||
} | ||
|
||
func (rr *rangeReader) close() error { | ||
var err error | ||
if rr.reader != nil { | ||
rr.remaining = 0 | ||
err = rr.reader.Close() | ||
rr.reader = nil | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters