![](/style/images/good.png)
1
![](/style/images/bad.png)
go net/http transport 内存泄漏
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.
- 使用的是全局的 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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK