1

go net/http transport 内存泄漏

 1 year ago
source link: https://www.v2ex.com/t/929315
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  问与答

go net/http transport 内存泄漏

  aladdinding · 13 小时 10 分钟前 · 926 次点击
  • 使用的是全局的 transport
		transport: &http.Transport{
			DialContext: proxy.DailContext,
			Proxy: func(req *http.Request) (*url.URL, error) {
				if proxyURL, ok := req.Context().Value(proxy.KeyProxyURL).(*url.URL); ok {
					return proxyURL, nil
				}
				return nil, nil
			},
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
			IdleConnTimeout:       15 * time.Second,
			ResponseHeaderTimeout: 10 * time.Second,
			TLSHandshakeTimeout:   10 * time.Second,
			ExpectContinueTimeout: 10 * time.Second,
			MaxIdleConnsPerHost:   3,
		},}
  • 每次 resp.Body 也 close 了

附上 pprof 图,

http://img.aladdinding.cn/202304030947153.png

集中在 bufio NewReader/Writer 是什么原因呢?

  • goroutine 数量也没有暴增,数量稳定在 3000-4000 ,下面是 transport 代码

	pconn.br = bufio.NewReaderSize(pconn, t.readBufferSize())
	pconn.bw = bufio.NewWriterSize(persistConnWriter{pconn}, t.writeBufferSize())

	go pconn.readLoop()
	go pconn.writeLoop()
	return pconn, nil

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK