Say I have created a RequestWithContext:

r, err := http.NewRequestWithContext(ctx, "GET", 
"http://127.0.0.1:8080/healthcheck";, nil)

Now, I make the request in the context of *another* HTTP request handling 
function:

Func myHandler(w http.ResponseWriter, r *http.Request) {
...

resp, err := client.Do(r)
if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
}
      return
}
…
}


When I cancel this context, the above http.Error() call triggers a superfluous 
write header message.
I dug down a bit and I can see that when the context is cancelled, the 
finishRequest() function 
(https://golang.org/src/net/http/server.go?h=finishRequest#L1613) is called 
which essentially writes a 200 status code. Hence, when the code above goes to 
write an error, it gets the superfluous writeHeader message.

I then updated my code above to be:

resp, err := client.Do(r)
if err != nil {
        if err != context.Canceled {
                http.Error(w, err.Error(), http.StatusInternalServerError)
        }
     return
}

Wondering if anyone has any thoughts on this pattern and if there is any reason 
why we complete the full lifecycle of finsihRequest() on context cancellation 
even if it is going to fail somewhere down the line since in this case, the 
client has already disconnected?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/FECFD0DC-85F3-4C4A-95E1-52C7F9E9A18F%40gmail.com.

Reply via email to