IPFS http 响应偶尔会出现正确正文的 EOF 错误
IPFS http response got EOF error with correct body occasionally
当我在下面code中调试时,有时它可以正确地从正文中读取数据,但出现EOF错误。
func (r *trailerReader) Read(b []byte) (int, error) {
n, err := r.resp.Body.Read(b)
if err != nil {
if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
err = errors.New(e)
}
}
return n, err
}
我在代码中调用了 this method:
// FilesRead read a file in a given MFS
func (s *Shell) FilesRead(ctx context.Context, path string, options ...FilesOpt) (io.ReadCloser, error) {
rb := s.Request("files/read", path)
for _, opt := range options {
if err := opt(rb); err != nil {
return nil, err
}
}
resp, err := rb.Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
return resp.Output, nil
}
有什么想法吗?
正如 Stebalien 在此 Github issue 中所说,这是 Reader
的预期行为。
参考本文第三段documentation
When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.
当我在下面code中调试时,有时它可以正确地从正文中读取数据,但出现EOF错误。
func (r *trailerReader) Read(b []byte) (int, error) {
n, err := r.resp.Body.Read(b)
if err != nil {
if e := r.resp.Trailer.Get("X-Stream-Error"); e != "" {
err = errors.New(e)
}
}
return n, err
}
我在代码中调用了 this method:
// FilesRead read a file in a given MFS
func (s *Shell) FilesRead(ctx context.Context, path string, options ...FilesOpt) (io.ReadCloser, error) {
rb := s.Request("files/read", path)
for _, opt := range options {
if err := opt(rb); err != nil {
return nil, err
}
}
resp, err := rb.Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
return resp.Output, nil
}
有什么想法吗?
正如 Stebalien 在此 Github issue 中所说,这是 Reader
的预期行为。
参考本文第三段documentation
When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.