-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
What version of Go are you using (go version
)?
https://golang.org/pkg/net/http/#Request.Context
Does this issue reproduce with the latest release?
Same docs on https://tip.golang.org/pkg/net/http/#Request.Context
What did you do?
I read:
For outgoing client requests, the context controls cancelation.
I thought the context only affected its associated Request
, and that the Response
returned from Client.Do(Request)
would be unaffected by the context.
I searched the net/http Godoc for any mentions of Context relating to a Response but didn't find anything.
Finally, after a long discussion with coworkers, we came up with a manual test case demonstrating that the request context does in fact affect the response:
package main
import (
"context"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"time"
)
func main() {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("serving", r.URL)
io.WriteString(w, "Hold on...")
if f, ok := w.(http.Flusher); ok {
f.Flush()
} else {
log.Println("cannot flush!")
}
time.Sleep(10 * time.Second)
io.WriteString(w, "... okay")
}))
defer s.Close()
req, err := http.NewRequest("GET", s.URL, nil)
if err != nil {
log.Fatal("Error making request:", err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
log.Println("client made request, starting to read response (this should time out in 1s)")
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading response body:", err)
}
log.Println("client read response:", string(body))
}
// Output like:
// 2018/06/27 17:07:06 serving /
// 2018/06/27 17:07:06 client made request, starting to read response (this should time out in 1s)
// 2018/06/27 17:07:07 Error reading response body:context deadline exceeded
We would have saved a lot of time today if the doc said something more like "For outgoing client requests and their corresponding responses, the context controls cancelation."