From a0d8830242c349ca922946d234f06d8eaac84fb0 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Tue, 18 Dec 2018 03:16:17 +0800 Subject: [PATCH] =?UTF-8?q?golang=20open=20files=20=E6=98=AFkeepalives?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/session.go b/session.go index 598680e..d989492 100644 --- a/session.go +++ b/session.go @@ -178,6 +178,8 @@ func NewSession() *Session { client := &http.Client{} transport := &http.Transport{DisableCompression: true, DisableKeepAlives: true} + EnsureTransporterFinalized(transport) + client.Transport = transport cjar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}) if err != nil { @@ -361,16 +363,15 @@ func (ses *Session) Delete(url string) *Workflow { return wf } -// CloseIdleConnections closes the idle connections that a session client may make use of -// 从levigross/grequests 借鉴 -func (ses *Session) CloseIdleConnections() { - ses.client.Transport.(*http.Transport).CloseIdleConnections() -} +// // CloseIdleConnections closes the idle connections that a session client may make use of +// // 从levigross/grequests 借鉴 +// func (ses *Session) CloseIdleConnections() { +// ses.client.Transport.(*http.Transport).CloseIdleConnections() +// } // EnsureTransporterFinalized will ensure that when the HTTP client is GCed // the runtime will close the idle connections (so that they won't leak) // this function was adopted from Hashicorp's go-cleanhttp package -// 暂时不用, 标记到以后是否起作用 func EnsureTransporterFinalized(httpTransport *http.Transport) { runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) { (*transportInt).CloseIdleConnections()