【问题标题】:Node.js : Unhandled error on parallel GET requests on a certain timeNode.js:特定时间并行 GET 请求的未处理错误
【发布时间】:2015-11-04 08:44:57
【问题描述】:

我正在使用 request/request 模块向 API 发送大约 100 个并行 GET 请求。我遇到的问题是关于80th to 100th requestnode 随机抛出这个异常:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 0.0.0.0:80 //a dummy ip
    at Object.exports._errnoException (util.js:860:11)
    at exports._exceptionWithHostPort (util.js:883:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)

这是我的请求默认值:

var apiRequest = request.defaults({

    url : url, //response is in xml format
    headers : {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Language': 'en-US,en;q=0.8'
    },
    method : 'GET',
    gzip : true,
    encoding : 'utf8',
    pool: {maxSockets: Infinity},
    forever : true,
    timeout : 120000

});

我还要求在数组上使用循环

【问题讨论】:

  • 大约需要多长时间才能到达错误?几分钟?
  • @MattWay 是的,大约 1 或 2 分钟。它的变种。
  • 试着提高你的timeout 看看会发生什么。
  • @RaminOmrani 对您来说似乎没有任何问题,您是否检查过这些 API 是否正确响应?
  • @MattWay 再次抛出异常

标签: javascript node.js api http get


【解决方案1】:

通过请求失败的 url on error event 递归解决了我的问题,直到没有超时发生:

function getAPI(url) {
    var apiRequest = request.defaults({

        url : url,
        headers : {
            'Accept': 'text/html,application/xhtml+xml,application/xml'
        },
        method : 'GET',
        gzip : true
    });

    apiRequest(function (error, response, body) {

    }).on('error', function(e){
        getAPI(this.uri.href);//this is important
    }).end();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2019-05-13
    相关资源
    最近更新 更多