GAE Golang - urlfetch timeout?

我怕爱的太早我们不能终老 提交于 2019-12-03 16:23:17
orcaman

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

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

Looking at the source code of Go's appengine:

and the protobuffer generated code:

Looks like there should not be a problem with Duration itself.

My guess is that the whole application inside appengine timeouts after 5 seconds.

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 . context.WithTimeout or context.WithDeadline is the method to use , here is the link https://godoc.org/golang.org/x/net/context#WithTimeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!