1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:01:37 -07:00

net/http: make Server cancel its ReadTimeout between requests

Fixes #18447

Change-Id: I5d60c3632a5ce625d3bac9d85533ce689e301707
Reviewed-on: https://go-review.googlesource.com/34813
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2017-01-04 21:03:24 +00:00
parent 7fb1640613
commit ecac827573
2 changed files with 38 additions and 0 deletions

View File

@ -5089,3 +5089,40 @@ func testServerKeepAlivesEnabled(t *testing.T, h2 bool) {
t.Fatalf("test server has active conns")
}
}
// Issue 18447: test that the Server's ReadTimeout is stopped while
// the server's doing its 1-byte background read between requests,
// waiting for the connection to maybe close.
func TestServerCancelsReadTimeoutWhenIdle(t *testing.T) {
setParallel(t)
defer afterTest(t)
const timeout = 250 * time.Millisecond
ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r *Request) {
select {
case <-time.After(2 * timeout):
fmt.Fprint(w, "ok")
case <-r.Context().Done():
fmt.Fprint(w, r.Context().Err())
}
}))
ts.Config.ReadTimeout = timeout
ts.Start()
defer ts.Close()
tr := &Transport{}
defer tr.CloseIdleConnections()
c := &Client{Transport: tr}
res, err := c.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
slurp, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if string(slurp) != "ok" {
t.Fatalf("Got: %q, want ok", slurp)
}
}

View File

@ -637,6 +637,7 @@ func (cr *connReader) startBackgroundRead() {
panic("invalid concurrent Body.Read call")
}
cr.inRead = true
cr.conn.rwc.SetReadDeadline(time.Time{})
go cr.backgroundRead()
}