psserver: swallow io.EOF when getting full content in FileRanger (#1238)

This commit is contained in:
Egon Elbre 2019-02-06 07:41:41 +02:00 committed by GitHub
parent 331f0cc845
commit 218e95728c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

View File

@ -123,8 +123,6 @@ func TestPiece(t *testing.T) {
} }
func TestRetrieve(t *testing.T) { func TestRetrieve(t *testing.T) {
t.Skip("flaky")
ctx := testcontext.New(t) ctx := testcontext.New(t)
defer ctx.Cleanup() defer ctx.Cleanup()

View File

@ -8,7 +8,7 @@ import (
"io" "io"
"os" "os"
"storj.io/storj/pkg/utils" "github.com/zeebo/errs"
) )
type fileRanger struct { type fileRanger struct {
@ -46,14 +46,35 @@ func (rr *fileRanger) Range(ctx context.Context, offset, length int64) (io.ReadC
} }
_, err = fh.Seek(offset, io.SeekStart) _, err = fh.Seek(offset, io.SeekStart)
if err != nil { if err != nil {
err = utils.CombineErrors(err, fh.Close()) return nil, Error.Wrap(errs.Combine(err, fh.Close()))
return nil, Error.Wrap(err)
} }
return struct {
io.Reader return &FileReader{fh, length}, nil
io.Closer }
}{
Reader: io.LimitReader(fh, length), // FileReader implements limit reader with io.EOF only on last read.
Closer: fh, type FileReader struct {
}, nil file *os.File
remaining int64
}
// Read reads from the underlying file.
func (reader *FileReader) Read(data []byte) (n int, err error) {
if reader.remaining <= 0 {
return 0, io.EOF
}
if int64(len(data)) > reader.remaining {
data = data[0:reader.remaining]
}
n, err = reader.file.Read(data)
reader.remaining -= int64(n)
if err == io.EOF && reader.remaining == 0 {
err = nil
}
return
}
// Close closes the underlying file.
func (reader *FileReader) Close() error {
return reader.file.Close()
} }