首页 文章 Go语言基础 golang 标准库 http 的 client 为什么必须手动关闭 resp.Body
0
0
0
59

golang 标准库 http 的 client 为什么必须手动关闭 resp.Body

标准库 手动 标准 golang client Body

使用 golang http client 请求,我们通常是这么干的

resp, err := http.Get(...)
if err != nil {
..
}
defer resp.Body.Close()

为啥必须 resp.Body.Close() 呢?
回答这个问题其实需要回答两个问题:

  1. resp.Body.Close() 做了啥?
  2. 为啥这么做?

1 resp.Body.Close() 做了啥?

为了确定 Close 方法做了啥,需要先确定 resp.Body 具体是怎么实现的。http client 是否设置 timeout 决定了 resp.Body 的两种实现,
第一,设置timeout

cli := http.Client{Timeout: time.Second}
resp, _ := cli.Get(...)

这种情况下,resp.BodycancelTimerBody
第二,不设置timeout,这种情况下,resp.BodybodyEOFSignal

不管是那种情况,这两种body最终都是封装的这个body:

// body turns a Reader into a ReadCloser.
// Close ensures that the body has been fully read
// and then reads the trailer if necessary.
type body struct {
	src          io.Reader
	hdr          interface{}   // non-nil (Response or Request) value means read trailer
	r            *bufio.Reader // underlying wire-format reader for the trailer
	closing      bool          // is the connection to be closed after reading body?
	doEarlyClose bool          // whether Close should stop early

	mu         sync.Mutex // guards following, and calls to Read and Close
	sawEOF     bool
	closed     bool
	earlyClose bool   // Close called and we didn't read to the end of src
	onHitEOF   func() // if non-nil, func to call when EOF is Read
}

所以,最终的 close 方法执行的是这个逻辑:

func (b *body) Close() error {
	b.mu.Lock()
	defer b.mu.Unlock()
	if b.closed {
		return nil
	}
	var err error
	switch {
	case b.sawEOF:
		// Already saw EOF, so no need going to look for it.
	case b.hdr == nil && b.closing:
		// no trailer and closing the connection next.
		// no point in reading to EOF.
	case b.doEarlyClose:
		// Read up to maxPostHandlerReadBytes bytes of the body, looking
		// for EOF (and trailers), so we can re-use this connection.
		if lr, ok := b.src.(*io.LimitedReader); ok && lr.N > maxPostHandlerReadBytes {
			// There was a declared Content-Length, and we have more bytes remaining
			// than our maxPostHandlerReadBytes tolerance. So, give up.
			b.earlyClose = true
		} else {
			var n int64
			// Consume the body, or, which will also lead to us reading
			// the trailer headers after the body, if present.
			n, err = io.CopyN(ioutil.Discard, bodyLocked{b}, maxPostHandlerReadBytes)
			if err == io.EOF {
				err = nil
			}
			if n == maxPostHandlerReadBytes {
				b.earlyClose = true
			}
		}
	default:
		// Fully consume the body, which will also lead to us reading
		// the trailer headers after the body, if present.
		_, err = io.Copy(ioutil.Discard, bodyLocked{b})
	}
	b.closed = true
	return err
}

正如上面的代码注释所说 Close ensures that the body has been fully read,是用来确保body读干净。

2 为啥这么做?

很简单:链接复用

到此这篇关于“golang 标准库 http 的 client 为什么必须手动关闭 resp.Body”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持Go语言编程网!

相关文章

创建博客

开始创作
写作能提升自己能力,也能为他人分享知识。

在线教程

查看更多
  • Go入门指南

    Go入门指南

  • Go语言高级编程

    Go语言高级编程

  • Go Web 编程

    Go Web 编程

  • GO专家编程

    GO专家编程

  • Go语言四十二章经

    Go语言四十二章经

  • 数据结构和算法(Golang实现)

    数据结构和算法(Golang实现)

Go语言编程网

微信扫码关注订阅号


博客 资讯 教程 我的