5

in GO net/http Response Body annotation says:

It is the caller's responsibility to close Body. The default HTTP client's Transport does not attempt to reuse HTTP/1.0 or HTTP/1.1 TCP connections ("keep-alive") unless the Body is read to completion and is closed.

It's mean: if I use http.Get and don't call resp.Body.Close() then it will not resue HTTP/1.0 or HTTP/1.1 TCP connections ("keep-alive") yeah?

so I write some code:

package main

import ( "time" "fmt" "io/ioutil" "net/http" )

func main() { resp, err := http.Get("http://127.0.0.1:8588")

if err != nil {
    panic(err)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
    panic(err)
}

resp2, err := http.Get("http://127.0.0.1:8588")

if err != nil {
    panic(err)
}
_, err = ioutil.ReadAll(resp2.Body)
if err != nil {
    panic(err)
}

fmt.Println("before time sleep")
time.Sleep(time.Second * 35)

}

and I only see ONE tcp connection build in wireshark, why? I don't close res.Body so the http client should't be reuse the tcp connection.

1
  • @CeriseLimón yes, it's a error on comment. github.com/golang/go/issues/22954 but the response body not read EOF. just read content-Length in http header then read Content-Length bytes from body. Commented Dec 1, 2017 at 7:21

2 Answers 2

2

this problem has been solved in https://github.com/golang/go/issues/22954.

Sign up to request clarification or add additional context in comments.

Comments

0

You have read it till the end in first occurence of line:

_, err = ioutil.ReadAll(resp.Body)

So the connection is ready to be resused. Try not to read and run again.

2 Comments

but in GO net/http comment, it says: "The default HTTP client's Transport does not attempt to reuse HTTP/1.0 or HTTP/1.1 TCP connections ("keep-alive") unless the Body is read to completion and is closed." but i don't close the resp.Body. so does we don't need close? just read completion is ready to be reuse?
the comment is not correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.