Merge pull request #5921 from ktock/fix-failed-precondition

Fix pull fails on unexpected EOF
This commit is contained in:
Phil Estes 2021-08-30 14:01:19 -04:00 committed by GitHub
commit 44d5a7e26d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,12 +26,16 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const maxRetry = 3
type httpReadSeeker struct { type httpReadSeeker struct {
size int64 size int64
offset int64 offset int64
rc io.ReadCloser rc io.ReadCloser
open func(offset int64) (io.ReadCloser, error) open func(offset int64) (io.ReadCloser, error)
closed bool closed bool
errsWithNoProgress int
} }
func newHTTPReadSeeker(size int64, open func(offset int64) (io.ReadCloser, error)) (io.ReadCloser, error) { func newHTTPReadSeeker(size int64, open func(offset int64) (io.ReadCloser, error)) (io.ReadCloser, error) {
@ -53,6 +57,27 @@ func (hrs *httpReadSeeker) Read(p []byte) (n int, err error) {
n, err = rd.Read(p) n, err = rd.Read(p)
hrs.offset += int64(n) hrs.offset += int64(n)
if n > 0 || err == nil {
hrs.errsWithNoProgress = 0
}
if err == io.ErrUnexpectedEOF {
// connection closed unexpectedly. try reconnecting.
if n == 0 {
hrs.errsWithNoProgress++
if hrs.errsWithNoProgress > maxRetry {
return // too many retries for this offset with no progress
}
}
if hrs.rc != nil {
if clsErr := hrs.rc.Close(); clsErr != nil {
log.L.WithError(clsErr).Errorf("httpReadSeeker: failed to close ReadCloser")
}
hrs.rc = nil
}
if _, err2 := hrs.reader(); err2 == nil {
return n, nil
}
}
return return
} }