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 8, 2016
1 parent 57a6e26 commit ac70099
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/asaskevich/govalidator"

Expand Down Expand Up @@ -101,6 +102,17 @@ func Username(u string) metaOp {
}
}

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

// 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 +122,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 +263,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 ac70099

Please sign in to comment.