Skip to content

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

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

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

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

liaojianqi opened this issue Dec 1, 2017 · 4 comments
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Milestone

Comments

@liaojianqi
Copy link

liaojianqi 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
Copy link
Contributor

davecheney commented Dec 1, 2017 via email

@liaojianqi
Copy link
Author

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
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Dec 1, 2017
@bradfitz bradfitz added this to the Go1.10 milestone Dec 1, 2017
@bradfitz bradfitz self-assigned this Dec 1, 2017
@bradfitz
Copy link
Contributor

bradfitz 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
Copy link
Contributor

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

@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.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants