我在Go中的Google App Engine上遇到urlfetch超时问题。该应用程序似乎不想花费比5秒钟更长的超时时间(它会忽略较长的超时时间,并超过自己的时间超时)。GAE Golang - urlfetch超时?
我的代码是:
var TimeoutDuration time.Duration = time.Second*30
func Call(c appengine.Context, address string, allowInvalidServerCertificate bool, method string, id interface{}, params []interface{})(map[string]interface{}, error){
data, err := json.Marshal(map[string]interface{}{
"method": method,
"id": id,
"params": params,
})
if err != nil {
return nil, err
}
req, err:=http.NewRequest("POST", address, strings.NewReader(string(data)))
if err!=nil{
return nil, err
}
tr := &urlfetch.Transport{Context: c, Deadline: TimeoutDuration, AllowInvalidServerCertificate: allowInvalidServerCertificate}
resp, err:=tr.RoundTrip(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := make(map[string]interface{})
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
return result, nil
}
无论我尝试设置TimeoutDuration
到,应用程序超时约5秒钟后。如何防止它做到这一点?我的代码中有错误吗?
你不需要施放[常量](https://blog.golang.org /常数),'30 * time.Second'是首选。 –
感谢2016年更新! –