简体   繁体   English

GAE Golang - urlfetch超时?

[英]GAE Golang - urlfetch timeout?

I am having issues with urlfetch's timeouts on Google App Engine in Go. 我在Go上的Google App Engine上遇到urlfetch超时问题。 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). 该应用程序似乎不需要超过大约5秒的超时(它忽略更长的超时并在其自己的时间后超时)。

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. 无论我尝试将TimeoutDuration设置TimeoutDuration ,应用程序在大约5秒后超时。 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): 您需要像这样传递持续时间(否则它将默认为5秒超时):

tr := &urlfetch.Transport{Context: c, Deadline: time.Duration(30) * time.Second}

Update Jan 2 2016: 2016年1月2日更新:

With the new GAE golang packages ( google.golang.org/appengine/* ), this has changed. 使用新的GAE golang软件包( google.golang.org/appengine/* ),这已经发生了变化。 urlfetch no longer receives a deadline time duration in the transport. urlfetch不再接收传输中的截止时间。

You should now set the timeout via the new context package. 您现在应该通过新的上下文包设置超时。 For example, this is how you would set a 1 minute deadline: 例如,这是您设置1分钟截止日期的方式:

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},
        },
    }

Try the code below: 请尝试以下代码:

// createClient is urlfetch.Client with Deadline
func createClient(context appengine.Context, t time.Duration) *http.Client {
    return &http.Client{
        Transport: &urlfetch.Transport{
            Context:  context,
            Deadline: t,
        },
    }
}

Here is how to use it. 以下是如何使用它。

// urlfetch
client := createClient(c, time.Second*60)

Courtesy @gosharplite 礼貌@gosharplite

Looking at the source code of Go's appengine: 看看Go的appengine的源代码:

and the protobuffer generated code: 和protobuffer生成的代码:

Looks like there should not be a problem with Duration itself. 看起来持续时间本身应该没有问题。

My guess is that the whole application inside appengine timeouts after 5 seconds. 我的猜测是5秒后整个应用程序内部的appengine超时。

for me, this worked: 对我来说,这工作:

ctx_with_deadline, _ := context.WithTimeout(ctx, 15*time.Second)
client := urlfetch.Client(ctx_with_deadline)

This is had now changed with the recent updates to the library . 现在,随着最近对库的更新,这已经改变了。 Now the Duration of timeout/delay have to carried by the context , urlfetch.transport no more has the Deadline field in it . 现在超时/延迟的持续时间必须由上下文携带, urlfetch.transport不再有Deadline字段。 context.WithTimeout or context.WithDeadline is the method to use , here is the link https://godoc.org/golang.org/x/net/context#WithTimeout context.WithTimeoutcontext.WithDeadline是要使用的方法,这里是链接https://godoc.org/golang.org/x/net/context#WithTimeout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM