golang 標準庫 http 的 client 爲什麼必須手動關閉 resp.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 爲啥這麼做?

很簡單:鏈接複用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章