http.Transports shouldn't be created inside short-lived functions. See the docs on https://golang.org/pkg/net/http/#Transport which say:
"By default, Transport caches connections for future re-use. This may leave many open connections when accessing many hosts. This behavior can be managed using Transport's CloseIdleConnections method and the MaxIdleConnsPerHost and DisableKeepAlives fields. Transports should be reused instead of created as needed. Transports are safe for concurrent use by multiple goroutines." Also, the net/http package doesn't let you "create a connection" to a server. That's a detail that's hidden away, except via CloseIdleConnections. You just make requests & read responses. Move your http client/transport to globals or something shared and you should see the connection reuse you're seeking. On Wed, Aug 10, 2016 at 3:53 PM, Jeffrey Smith <jeffreysmith19...@gmail.com> wrote: > I have been playing around with http2 in the latetst 1.7RC today and had a > few questions > > I still had to import "golang.org/x/net/http2" and then set > http2.ConfigureTransport to actually use http2 when creating a client. I > was under the impression this was fixed or was I wrong? > https://github.com/golang/go/issues/14391 > > tr := &http.Transport{ > TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, > } > http2.ConfigureTransport(tr) > client := &http.Client{Transport: tr} > > > I have the below code but it appears to leak connections whenever the 20 > second timeout is hit after connecting to a client, is there anyway to > clean up the connection for reuse rather than creating a new one until I > hit my open file limit? > > func worker(id int, jobs <-chan int, results chan<- Results) { > > tr := &http.Transport{ > TLSClientConfig: &tls.Config{InsecureSkipVerify: > true}, > MaxIdleConnsPerHost: 2, > } > http2.ConfigureTransport(tr) > PostClient := &http.Client{Transport: tr, Timeout: > time.Duration(20 * time.Second)} > closeBody := false > > for { > closeBody = false > t0 := time.Now() > > req, err := http.NewRequest("GET", > Url+strconv.Itoa(<-jobs), nil) > if err != nil { > results <- Results{0, err, "0"} > continue > } > resp, err := PostClient.Do(req) > if resp != nil { > closeBody = true > //defer resp.Body.Close() > } > t1 := time.Now() > if err != nil { > results <- Results{0, err, fmt.Sprintf("%v", > t1.Sub(t0))} > if closeBody == true { > io.Copy(ioutil.Discard, resp.Body) > resp.Body.Close() > } > continue > } > //discard the body so we can reuse the connections > io.Copy(ioutil.Discard, resp.Body) > //Close the body so we can resuse > resp.Body.Close() > > results <- Results{resp.StatusCode, err, fmt.Sprintf("%v", > t1.Sub(t0))} > } > } > > Also does anyone have any recommendations with http2 should I be creating > one connection per server and fire all requests down that (not even sure > how I would do this in golang) or just create a global pool and allow the > http client deal with it? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.