Google App Engine将HTTP请求转到慢页面 [英] Google App Engine Go HTTP request to a slow page

查看:105
本文介绍了Google App Engine将HTTP请求转到慢页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在转到响应缓慢的网站时使用Google App Engine发出HTTP请求。我的代码是:

I want to make a HTTP request using Google App Engine in Go to a slow-responding website. My code is:

func request(w http.ResponseWriter, r *http.Request) {
    c:=appengine.NewContext(r)
    client := urlfetch.Client(c)
    resp, err := client.Get("http://www.example.com")
    if err != nil {
        log.Print("err %s", err.String())
        return
    }
    ...}

打电话给我的网站后,我得到这样一个错误:

After calling my website, I get such an error:

API error 1 (urlfetch: INVALID_URL): ApplicationError: 5 timed out

一个bi,我发现一些运输类的参考允许等待长达60秒的HTTP请求 ,但我找不到任何描述或例子如何做这项工作。

After looking a bi into it, I found some reference to Transport class that allows to wait for HTTP requests for up to 60 seconds, but I can't find any description or example on how to make that work.

我如何做一个HTTP请求我n GAE Go的截止时间会超过5秒吗?

How can I make a HTTP request in GAE Go that will have a deadline longer than 5 seconds?

推荐答案

我会推荐如果可能的话,使用任务队列API 的主要请求处理程序。

I would recommend if possible, doing the urlfetch outside of your main request handler using Tasks Queue API.

在构建urlfetch.Transport时,您应该可以覆盖默认的截止日期,如下所示:

When constructing urlfetch.Transport, you should be able to override the default deadline like this:

t := urlfetch.Transport{Context:c, DeadlineSeconds: 5.0}
client := http.Client{Transport: &t}

这篇关于Google App Engine将HTTP请求转到慢页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆