From 8383a3820fbcaa8dd7c5a4f27c42853e294653e2 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Tue, 23 Oct 2018 17:45:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E7=94=A8=E7=9A=84=E5=87=BD=E6=95=B0,=20=20En?= =?UTF-8?q?sureTransporterFinalized?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- session.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/session.go b/session.go index c20c73d..aa4da6e 100644 --- a/session.go +++ b/session.go @@ -7,6 +7,7 @@ import ( "net/http/cookiejar" "net/url" "reflect" + "runtime" "time" "golang.org/x/net/publicsuffix" @@ -252,3 +253,13 @@ func (ses *Session) Options(url string) *Workflow { 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() + }) +}