Look at two code:
server.go:
package main
import "net/http"
import (
"time"
"fmt"
)
func main() {
http.HandleFunc("/test", test)
http.ListenAndServe(":9999", nil)
}
func test(w http.ResponseWriter, r *http.Request){
fmt.Println("test func")
time.Sleep(100000000000000)
}
client.go
package main
import (
"net/http"
"fmt"
)
func main() {
resp, err := http.Get("http://127.0.0.1:9999/test")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(resp.StatusCode)
}
}
when we run client.go, it will stuck. please notice it.
What change in particular are you suggesting? That a timeout of 0 convert to some default, or that the DefaultClient declaration add one?
The former will break many programs, as there would be no way to disable the timeout. The latter would still break some, as they may depend on DefaultClient not having a timeout.
And there's always the question of what timeout to use. If it's too short, it will break far too many programs. If it's too long, to some people it will still "get stuck".
Why not use a custom http.Client? In your client program, it would be one extra line and you'd be able to use whatever timeout suits you.
Maybe we can do a 3 minute timeout or something high.
Enough other stuff breaks or times out around 3 minutes in the wild that this doesn't seem too invasive.
Thoughts, @tombergan?
I don't want to hijack this specific issue but I fully agree with all network operations (i.e. not just HTTP) having timeouts by default. We have been bitten so many times by components that don't allow to specify timeouts for network operations (or that have no sane default timeout) that is now one of the first things I check when evaluating the use of a new component/library.
Maybe 3 minute is enough.
Change https://golang.org/cl/116356 mentions this issue: net/http: add 5 minute Timeout to DefaultClient
I sent CL 116356 to add a 5 minute timeout, but right after I mailed it I realized it would break people wanting to do long downloads.
I think the only conservative thing we could do by default is to add some sort of InactivityTimeout, but it's too late for that, so bumping to Go 1.12.
thanks @bradfitz
Per discussion with proposal review, retitled with understanding that step 2 is to set it to a good default in #24138.
Most helpful comment
Maybe we can do a 3 minute timeout or something high.
Enough other stuff breaks or times out around 3 minutes in the wild that this doesn't seem too invasive.
Thoughts, @tombergan?