我想在 Go 中使用 Google App Engine 向响应缓慢的网站发出 HTTP 请求。我的代码是:
func request(w http.ResponseWriter, r *http.Request) {
c:=appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.Get("http://www.example.com")
if err != nil {
log.Print("err %s", err.String())
return
}
...}
调用我的网站后,我收到这样的错误:
API error 1 (urlfetch: INVALID_URL): ApplicationError: 5 timed out
仔细研究后,我发现了一些对 Transport class 的引用允许等待 up to 60 seconds 的 HTTP 请求,但我找不到任何有关如何实现该功能的描述或示例。
如何在 GAE Go 中发出截止时间超过 5 秒的 HTTP 请求?
最佳答案
如果可能的话,我建议在主请求处理程序之外使用 Tasks Queue API 执行 urlfetch .
构建 urlfetch.Transport 时,您应该能够像这样覆盖默认截止日期:
t := urlfetch.Transport{Context:c, DeadlineSeconds: 5.0}
client := http.Client{Transport: &t}
关于http - Google App Engine Go 对慢速页面的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939614/