Skip to content

Commit

Permalink
Merge pull request #28 from eyalpost/master
Browse files Browse the repository at this point in the history
Remove finalizers
  • Loading branch information
h2non committed May 30, 2017
2 parents af66fea + d2ede54 commit 54b02e0
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 31 deletions.
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()
})
}

0 comments on commit 54b02e0

Please sign in to comment.