Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Avoid leaking http.Transports.
Browse files Browse the repository at this point in the history
Avoids leaking http.Transports on client creation by re-using
transports.

Removes duplicate close of response body in httpRespToAPIResp which
resulted in undefined behavior.
  • Loading branch information
croseborough committed Nov 15, 2016
1 parent 7b23de9 commit 924cf8d
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ func Username(u string) metaOp {
}
}

var (
secureTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
insecureTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
)

// New returns a pointer to a snap api client
// if ver is an empty string, v1 is used by default
func New(url, ver string, insecure bool, opts ...metaOp) (*Client, error) {
Expand All @@ -110,16 +119,18 @@ func New(url, ver string, insecure bool, opts ...metaOp) (*Client, error) {
if ver == "" {
ver = "v1"
}
var t *http.Transport
if insecure {
t = insecureTransport
} else {
t = secureTransport
}
c := &Client{
URL: url,
Version: ver,

http: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
},
Transport: t,
},
}
for _, opt := range opts {
Expand Down Expand Up @@ -249,7 +260,6 @@ func httpRespToAPIResp(rsp *http.Response) (*rbody.APIResponse, error) {
}
resp := new(rbody.APIResponse)
b, err := ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 924cf8d

Please sign in to comment.