我在使用 Go 语言的 Google App Engine 进行 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秒钟后都会超时。如何防止这种情况发生?我的代码是否有错误?
30 * time.Second
。 - Dave C