英文:
Call to ExecuteTemplate receives an i/o timeout error
问题
我有一个函数,使用Go的http.Client调用外部API,解析结果,并在之后执行的模板中使用该结果。偶尔,外部API会响应缓慢(约20秒),模板执行会失败,报告"i/o timeout",或更具体地说,
template: :1:0: 在执行"page.html"时发生错误,位置在<"\n\t\t\t\t\t\t\t\t\...>: write tcp 127.0.0.1:35107: i/o timeout
这总是与API响应缓慢同时发生,但JSON对象中始终有一个有效的响应,所以http.Client正在接收到正确的响应。我只是想知道是否有人能指出在ExecuteTemplate调用中可能导致i/o超时的原因。
我尝试过在客户端传输中设置ResponseHeaderTimeout和DisableKeepAlives(包括使用和不使用这些选项),但都没有成功。我还尝试将请求的自动关闭值设置为true,但也没有成功。下面是模板生成代码的简化版本:
func viewPage(w http.ResponseWriter, r *http.Request) {
tmpl := pageTemplate{}
duration, _ := time.ParseDuration("120s")
tr := &http.Transport{
ResponseHeaderTimeout: duration,
DisableKeepAlives: true,
}
client := &http.Client{Transport: tr}
req, _ := http.NewRequest("GET", "http://example.com/some_function", nil)
req.Close = true
resp, _ := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var res api_response // 与JSON响应匹配的某个结构体
err = json.Unmarshal(body, &res)
t, _ := template.New("page.html")
err = t.ExecuteTemplate(w, "page.html", tmpl)
}
希望对你有所帮助!
英文:
I have a function that makes a call to an external API using a Go http.Client, parses the result, and uses the result in the template executed afterwards. Occasionally, the external API will respond slowly (~20s), and the template execution will fail citing "i/o timeout", or more specifically,
template: :1:0: executing "page.html" at <"\n\t\t\t\t\t\t\t\t\...>: write tcp 127.0.0.1:35107: i/o timeout
This always coincides with a slow API response, but there is always a valid response in the JSON object, so the http.Client is receiving a proper response. I am just wondering if anyone could point me towards what could be causing the i/o timeout in the ExecuteTemplate call.
I have tried ResponseHeaderTimeout and DisableKeepAlives in the client transport (both with and without those options) to no avail. I've also tried setting the request's auto-close value to true to no avail. A stripped-down version of the template generation code is below:
func viewPage(w http.ResponseWriter, r *http.Request) {
tmpl := pageTemplate{}
duration, _ := time.ParseDuration("120s")
tr := &http.Transport{
ResponseHeaderTimeout: duration,
DisableKeepAlives: true,
}
client := &http.Client{Transport: tr}
req, _ := http.NewRequest("GET", "http://example.com/some_function", nil)
req.Close = true
resp, _ := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var res api_response // some struct that matches the JSON response
err = json.Unmarshal(body, &res)
t, _ := template.New("page.html")
err = t.ExecuteTemplate(w, "page.html", tmpl)
}
答案1
得分: 3
这行代码中的超时:
err = t.ExecuteTemplate(w, "page.html", tmpl)
意味着在写入_outgoing_响应时超时,所以你在本地创建的客户端中所做的任何更改都不会影响它。在http.ListenAndServe中使用的http.Server实例上没有写入超时,所以你必须在创建的服务器上显式设置Server.WriteTimeout字段。
另外需要注意的是,在该处理程序中存在被忽略的错误,这是一种强烈不推荐的做法。
英文:
The timeout on this line:
err = t.ExecuteTemplate(w, "page.html", tmpl)
means that the outgoing response is timing out when being written into, so nothing you change in the locally created client should affect it. It also does make sense that a slow response from that client increases the chance of the timeout on w
, since the deadline is set when the response is created, before your handler is called, so a slow activity from your handler will increase the chances of a timeout.
There's no write timeout on the http.Server instance used by http.ListenAndServe, so you must be setting the Server.WriteTimeout field explicitly on the created server.
As a side note, there are errors being ignored in that handler, which is a strongly discouraged practice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论