使用 golang http client 请求,我们通常是这么干的
resp, err := http.Get(...)
if err != nil {
..
}
defer resp.Body.Close()
为啥必须 resp.Body.Close()
呢?
回答这个问题其实需要回答两个问题:
resp.Body.Close()
做了啥?- 为啥这么做?
1 resp.Body.Close()
做了啥?
为了确定 Close 方法做了啥,需要先确定 resp.Body
具体是怎么实现的。http client 是否设置 timeout 决定了 resp.Body
的两种实现,
第一,设置timeout
cli := http.Client{Timeout: time.Second}
resp, _ := cli.Get(...)
这种情况下,resp.Body
是 cancelTimerBody
第二,不设置timeout,这种情况下,resp.Body
是 bodyEOFSignal
不管是那种情况,这两种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 为啥这么做?
很简单:链接复用