Thinking > guide
Go Http Client guide

Always set a sane value for timeouts when using http.Client

When initializing a HTTP Client generally people use

client := &http.Client{}

However, this allows someone to hijack your goroutines. Look at this simple example:

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    time.Sleep(time.Hour)
}))

When you use

http.Get(svr.URL)

Your client will hang for an hour and then terminate. In order to remedy this always specify a sane timeout:

client := &http.Client{ Timeout: time.second * 10 }

Reference: Don't use Go's default HTTP client (in production)

Recent Posts