-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
What version of Go are you using (go version
)?
$ go version go version go1.14 darwin/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/georgysavva/Library/Caches/go-build" GOENV="/Users/georgysavva/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/georgysavva/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gd/lq1x2k352p3d74_5zby2jjbc0000gn/T/go-build124744337=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
I have a simple program that uses the default http transport: http.DefautlTransport
and initializes two different http.Client
(with different timeout settings for example) on top of it:
https://play.golang.org/p/-AQxYXiG0El
I know that http transport keeps opened tcp connections in the pool and I want to close them before my program finishes because I don't need them anymore and it's a good practice to clean up resources.
I can call firstHTTPClient.CloseIdleConnections()
and secondHTTPClient.CloseIdleConnections()
it will close the underlying http.DefaultTransport
connections, but it feels a bit as a side effect behavior. It will call .CloseIdleConnections()
on the same transport twice, it's not a problem, but again, feels a bit unoptimized.
I would better instead close the transport directly and only one. But the problem is that http.DefaultTranport
is an interface type http.RoundTripper
, not http.Transport
and it doesn't have .CloseIdleConnections()
method. In order to call it you need to assert the type:
if tr, ok := httpTransport.(interface{ CloseIdleConnections() }); ok {
tr.CloseIdleConnections()
}
This is that you basically do in the http.Client.CloseIdleConnections()
implementation. But It's inconvenient for the user code to do the type assertion on the default http transport every time. Wouldn't it be better to create a separate function and expose it to the user:
func CloseIdleConnections(transport http.RoundTripper) {
if tr, ok := transport.(interface{ CloseIdleConnections() }); ok {
tr.CloseIdleConnections()
}
}
You could actually reuse that function in the http.Client.CloseIdleConnections()
implementation.