0

我目前正在处理需要通过 node.js 服务器(节点 v4.2.6)发送大量 https 请求(每秒约 15K~20K)的项目。一般相关的代码结构可以在这篇文章的最后看到。

我们尝试每秒发送大约 10~20 个 https 请求,并且每件事都运行成功,这意味着请求头和请求体本身应该没有问题。

但是,当我们开始扩展 Nodejs 服务器以每秒发送数千和数百万个请求时,就会出现问题。在这种情况下,看起来发送的请求的响应永远不会回来,相反,Nodejs 服务器将关闭套接字并抛出以下异常:

{ [Error: socket hang up] code: 'ECONNRESET' }
Fri, 24 Feb 2017 06:08:39 GMT  Caught exception: Error: socket hang up Details: {"code":"ECONNRESET"} Stack: Error: socket hang up
    at createHangUpError (_http_client.js:202:15)
    at TLSSocket.socketOnEnd (_http_client.js:287:23)
    at emitNone (events.js:72:20)
    at TLSSocket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickCallback (node.js:355:17)
{ [Error: socket hang up] code: 'ECONNRESET' }
Fri, 24 Feb 2017 06:08:39 GMT  Caught exception: Error: socket hang up Details: {"code":"ECONNRESET"} Stack: Error: socket hang up
    at createHangUpError (_http_client.js:202:15)
    at TLSSocket.socketCloseListener (_http_client.js:234:23)
    at emitOne (events.js:82:20)
    at TLSSocket.emit (events.js:169:7)
    at TCP._onclose (net.js:469:12)

我们认为问题是由https库本身引起的,如果发送的请求过多,套接字将自动关闭!因此我想知道这个 https 库是否有任何错误修复版本?还是有任何替代的 https post/get 库?任何建议表示赞赏!:-)

var https = require("https");
// do something
option_1.agent = new https.Agent({ keepAlive: true });
var req = https.get(option_1, function(res) {
  res.on('data', function (body) {
    // get the response string
  });
  res.on('end', function () {
    // get the result1
  }); 
});

if (result1 == null) return;
// using result1 to generate option_2
option_2.agent = new https.Agent({ keepAlive: true });
var req2 = https.request(option_2, function(res) {
  res.on('data', function (body) {
    // get the response string
  });
  res.on('end', function () {
    // get the result2
  })
});

if (result2 == null) return;
// using result2 to generate option_3
option_3.agent = new https.Agent({ keepAlive: true });
var req3 = https.request(option_3, function(res) {
  res.on('data', function (body) {
    // get the response string
  });
  res.on('end', function () {
    // get the result3
  })
});

console.log(result3);

4

0 回答 0