GAE Golang - urlfetch超时? [英] GAE Golang - urlfetch timeout?
问题描述
我在Go中的Google App Engine上遇到urlfetch超时的问题。该应用程序似乎不想超过约5秒(它忽略了更长的超时时间,并超过自己的时间后超时)超时。
我的代码是:
var TimeoutDuration time.Duration = time.Second * 30
func Call(c appengine.Context ,地址字符串,allowInvalidServerCertificate bool,方法字符串,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秒。如何防止它做到这一点?我在代码中犯了一些错误吗?
您需要像这样传递持续时间(否则它将默认为5秒超时):
tr:=& urlfetch.Transport {Context:c,Deadline:time.Duration(30) * time.Second}
2016年1月2日更新:
使用新的GAE golang包( google.golang.org/appengine / *
),这已经改变。 urlfetch
在运输中不再收到截止时间。
您现在应该通过新的上下文包设置超时时间。例如,这是你如何设置1分钟截止日期:
func someFunc(ctx context.Context){
ctx_with_deadline,_:= context.WithTimeout(ctx,1 * time.Minute)
client:=& http.Client {
Transport:& oauth2.Transport {
Base:& amp ; urlfetch.Transport {Context:ctx_with_deadline},
},
}
I am having issues with urlfetch's timeouts on Google App Engine in Go. The app does not appear to want to take a longer timeout than about 5 seconds (it ignores a longer timeout and times out after its own time).
My code is:
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
}
No matter what I try to set TimeoutDuration
to, the app times out after about 5 seconds. How prevent it from doing that? Did I make some error in my code?
You need to pass the time duration like this (otherwise it will default to the 5 sec timeout):
tr := &urlfetch.Transport{Context: c, Deadline: time.Duration(30) * time.Second}
Update Jan 2 2016:
With the new GAE golang packages (google.golang.org/appengine/*
), this has changed. urlfetch
no longer receives a deadline time duration in the transport.
You should now set the timeout via the new context package. For example, this is how you would set a 1 minute deadline:
func someFunc(ctx context.Context) {
ctx_with_deadline, _ := context.WithTimeout(ctx, 1*time.Minute)
client := &http.Client{
Transport: &oauth2.Transport{
Base: &urlfetch.Transport{Context: ctx_with_deadline},
},
}
这篇关于GAE Golang - urlfetch超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!