mirror of https://github.com/golang/go.git
net/http: make Transport send 'Connection: close' when DisableKeepAlives
No bug was open, but I found an old email to myself to investigate when I suspected this was happening. Change-Id: Icedefec6f15a000eaabb2693b0640b3b6c8bf82c Reviewed-on: https://go-review.googlesource.com/1578 Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
fccd942803
commit
207950ad51
|
|
@ -1064,6 +1064,10 @@ func (pc *persistConn) roundTrip(req *transportRequest) (resp *Response, err err
|
|||
req.extraHeaders().Set("Accept-Encoding", "gzip")
|
||||
}
|
||||
|
||||
if pc.t.DisableKeepAlives {
|
||||
req.extraHeaders().Set("Connection", "close")
|
||||
}
|
||||
|
||||
// Write the request concurrently with waiting for a response,
|
||||
// in case the server decides to reply before reading our full
|
||||
// request body.
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ var hostPortHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
|
|||
if r.FormValue("close") == "true" {
|
||||
w.Header().Set("Connection", "close")
|
||||
}
|
||||
w.Header().Set("X-Saw-Close", fmt.Sprint(r.Close))
|
||||
w.Write([]byte(r.RemoteAddr))
|
||||
})
|
||||
|
||||
|
|
@ -228,6 +229,10 @@ func TestTransportConnectionCloseOnRequest(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("error in connectionClose=%v, req #%d, Do: %v", connectionClose, n, err)
|
||||
}
|
||||
if got, want := res.Header.Get("X-Saw-Close"), fmt.Sprint(connectionClose); got != want {
|
||||
t.Errorf("For connectionClose = %v; handler's X-Saw-Close was %v; want %v",
|
||||
connectionClose, got, !connectionClose)
|
||||
}
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("error in connectionClose=%v, req #%d, ReadAll: %v", connectionClose, n, err)
|
||||
|
|
@ -249,6 +254,27 @@ func TestTransportConnectionCloseOnRequest(t *testing.T) {
|
|||
connSet.check(t)
|
||||
}
|
||||
|
||||
// if the Transport's DisableKeepAlives is set, all requests should
|
||||
// send Connection: close.
|
||||
func TestTransportConnectionCloseOnRequestDisableKeepAlive(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
ts := httptest.NewServer(hostPortHandler)
|
||||
defer ts.Close()
|
||||
|
||||
tr := &Transport{
|
||||
DisableKeepAlives: true,
|
||||
}
|
||||
c := &Client{Transport: tr}
|
||||
res, err := c.Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res.Body.Close()
|
||||
if res.Header.Get("X-Saw-Close") != "true" {
|
||||
t.Errorf("handler didn't see Connection: close ")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportIdleCacheKeys(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
ts := httptest.NewServer(hostPortHandler)
|
||||
|
|
|
|||
Loading…
Reference in New Issue