Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove finalizers #28

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"net/http"
"os"
"runtime"

"gopkg.in/h2non/gentleman.v1/context"
"gopkg.in/h2non/gentleman.v1/utils"
Expand Down Expand Up @@ -71,9 +70,6 @@ func buildResponse(ctx *context.Context) (*Response, error) {
Cookies: resp.Cookies(),
buffer: bytes.NewBuffer([]byte{}),
}
if !isChunkedResponse(resp) {
EnsureResponseFinalized(res)
}

return res, res.Error
}
Expand Down Expand Up @@ -228,14 +224,6 @@ func (r *Response) getInternalReader() io.Reader {
return r
}

// EnsureResponseFinalized will ensure that when the Response is GCed
// the request body is closed so we aren't leaking fds.
func EnsureResponseFinalized(httpResp *Response) {
runtime.SetFinalizer(&httpResp, func(httpResponseInt **Response) {
(*httpResponseInt).RawResponse.Body.Close()
})
}

// isChunkedResponse iterates over the response's transfer encodings
// and returns either true whether 'chunked' is found, or false, otherwise.
func isChunkedResponse(res *http.Response) bool {
Expand Down
19 changes: 0 additions & 19 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"io/ioutil"
"net/http"
"runtime"
"strconv"
)

Expand Down Expand Up @@ -51,21 +50,3 @@ func (nopCloser) Close() error { return nil }
func NopCloser() io.ReadCloser {
return nopCloser{bytes.NewBuffer([]byte{})}
}

// 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()
})
}

// SetTransportFinalizer sets a finalizer on the transport to ensure that
// idle connections are closed prior to garbage collection; otherwise
// these may leak.
func SetTransportFinalizer(transport *http.Transport) {
runtime.SetFinalizer(&transport, func(t **http.Transport) {
(*t).CloseIdleConnections()
})
}