Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http: fix docs on Transport connection reuse specifics #22954

Closed
liaojianqi opened this issue Dec 1, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@liaojianqi
Copy link

commented Dec 1, 2017

What version of Go are you using (go version)?

go version go1.7 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

What did you do?

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)
}

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.

yeah, it say we must read body complete AND close body.
in the above code, after read resp.Body I don't close the resp.Body. so, the http client should't reuse the tcp connection. so there will be two tcp connection handshark in wireshark, but I only see ONE.
so, maybe the net/http's comment has something wrong?
thank you.

@davecheney

This comment has been minimized.

Copy link
Contributor

commented Dec 1, 2017

@liaojianqi

This comment has been minimized.

Copy link
Author

commented Dec 1, 2017

What makes me wonder is resp2 should not reuse tcp connection because I don't call resp.Body.Close(). so I will see TWO tcp handshake. but I only see ONE. so resp reuse tcp connection created by resp, but i don't call resp.Body.Close().
and in net/http response 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.

@bradfitz bradfitz changed the title net/http Response's comment error net/http: fix docs on Transport connection reuse specifics Dec 1, 2017

@bradfitz bradfitz added this to the Go1.10 milestone Dec 1, 2017

@bradfitz bradfitz self-assigned this Dec 1, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

commented Dec 1, 2017

I'll update it to something slightly vaguer and less promise-sounding.

The reality is that it'll do it if it can.

@gopherbot

This comment has been minimized.

Copy link

commented Jan 4, 2018

Change https://golang.org/cl/86276 mentions this issue: net/http: soften wording around when the Transport reuses connections

@gopherbot gopherbot closed this in 3639929 Jan 5, 2018

@golang golang locked and limited conversation to collaborators Jan 5, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.