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 mainand I only see ONE tcp connection build in wireshark, why? I don't closeimport ( "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)
}
res.Body
so the http
client should't be reuse the tcp connection.
this problem has been solved in https://github.com/golang/go/issues/22954.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With