数千个并发请求 - EAI_AGAIN / ECONNRESET错误

时间:2016-04-10 17:52:01

标签: linux node.js ubuntu limit node-request

我需要在一小时内执行一百万个API调用(服务器可以处理这么多流量),为此,我使用节点并行运行多个请求,但是当我尝试运行~1000个并发请求时我一直收到这些错误:

EAI_AGAIN

{ [Error: getaddrinfo EAI_AGAIN google.com:80]
  code: 'EAI_AGAIN',
  errno: 'EAI_AGAIN',
  syscall: 'getaddrinfo',
  hostname: 'google.com',
  host: 'google.com',
  port: 80 }

ECONNRESET

{
    [Error: read ECONNRESET] code: 'ECONNRESET',
    errno: 'ECONNRESET',
    syscall: 'read'
}

如何在不减少500请求的情况下防止这种情况发生?

这是一个代码示例,在一次运行500个请求时效果很好,但在限制超过1000时失败。(您的限制可能不同)。

"use strict";
const http = require('http');
http.globalAgent.maxSockets = Infinity;
const async = require('async');
const request = require("request");

let success = 0;
let error = 0;

function iterateAsync() {
    let rows = [];
    for (let i = 0; i < 500; i++) {
        rows.push(i);
    }

    console.time("Requests");
    async.each(
        rows, 
        (item, callback) => get(callback), 
        (err) => {
            console.log("Failed: " + error);
            console.log("Success: " + success);
            console.timeEnd("Requests");
    });
}


function get(callback) {

    request("http://example.com", (err, response, body) => {
        if (err) {
            console.log(err);
            error++;
            return callback();
        }

        success++;
        callback();
    });
}

iterateAsync();

我添加了http.globalAgent.maxSockets = Infinity;,即使它是默认值。

500次请求

enter image description here

1000次请求

enter image description here

有条件的信息:

我正在 ubuntu 14.04&amp; 15.04 ,我在file-max中修改了tcp_fin_timeouttcp_tw_reuseip_local_port_range/etc/sysctl.conf默认值,如post所述

  • file-max: 100000
  • tcp_fin_timeout: 15
  • tcp_tw_reuse: 1
  • ip_local_port_range: 10000 65000

在/etc/security/limits.conf中添加了以下行

*     soft    nofile          100000
*     hard    nofile          100000

有了这个值,我仍然会收到错误。

我已阅读所有其他类似帖子:

有没有办法可以知道我的系统可以管理的确切并发请求数?

1 个答案:

答案 0 :(得分:0)

以下是我在@jfriend00输入后的解决方法。

我使用dns.resolve4解析主机IP地址,然后使用IP执行请求,这样错误就消失了,我可以在更短的时间内执行更多请求。

"use strict";

const dns = require("dns");
const http = require('http');

http.globalAgent.maxSockets = Infinity;

const async = require('async');
const request = require("request");

let success = 0;
let error = 0;

function iterateAsync() {
    let rows = [];
    for (let i = 0; i < 500; i++) {
        rows.push(i);
    }

    console.time("Requests");

    dns.resolve4("example.com", (err, addresses) =>{ //Resolve host and obtain IP address

        if(err) //Handle error
            return false;

        async.each(
            rows,
            (item, callback) => get(`http://${addresses[0]}`, callback),
            (err) => {
                console.log("Failed: " + error);
                console.log("Success: " + success);
                console.timeEnd("Requests");
            });

    });


}

function get(host, callback) {

    request(host, (err, response, body) => {
        if (err) {
            console.log(err);
            error++;
            return callback();
        }

        success++;
        callback();
    });
}


//Start requests
iterateAsync();

输出:

Failed: 0
Success: 500
Requests: 6295.799ms