Retry Reader reads when data is unexpectedly missing

This commit is contained in:
Matt Joiner 2015-06-03 13:36:27 +10:00
parent ca27d431f4
commit 0ecd03db32
1 changed files with 13 additions and 6 deletions

View File

@ -65,6 +65,10 @@ func (r *Reader) available(off, max int64) (ret int64) {
ret += len1
off += len1
}
// Ensure that ret hasn't exceeded our original max.
if max < 0 {
ret += max
}
return
}
@ -79,9 +83,6 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
func (r *Reader) Read(b []byte) (n int, err error) {
n, err = r.readAt(b, r.pos)
r.pos += int64(n)
if n != 0 && err == io.ErrUnexpectedEOF {
err = nil
}
return
}
@ -100,6 +101,7 @@ func (r *Reader) readAt(b []byte, pos int64) (n int, err error) {
if int64(len(b)) > maxLen {
b = b[:maxLen]
}
again:
r.raisePriorities(pos, len(b))
for !r.readable(pos) {
r.raisePriorities(pos, len(b))
@ -107,10 +109,15 @@ func (r *Reader) readAt(b []byte, pos int64) (n int, err error) {
}
avail := r.available(pos, int64(len(b)))
// log.Println("available", avail)
if int64(len(b)) > avail {
b = b[:avail]
b1 := b[:avail]
n, err = dataReadAt(r.t.data, b1, pos)
if n != 0 {
err = nil
return
}
if err == io.ErrUnexpectedEOF {
goto again
}
n, err = dataReadAt(r.t.data, b, pos)
return
}