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.

Reply via email to