Solutions for fixing node.js https library when huge amount of request need be sent concurrently

若如初见. 提交于 2020-01-07 07:49:09

问题


I am currently working with project that huge amount of https requests (~15K~20K per second) need be sent via node.js server (node v4.2.6). The general relevant code structure can be seen at final of this post.

We have tried by sending approximately 10~20 https requests per second, and every thing runs successfully, meaning there should not have problems on requests header and body itself.

However, issues occurs when we start scaling up Nodejs server for sending thousands and millions requests per seconds. In this case, it looks like the responses of the requests be sent will not come back ever, instead the Nodejs server will shutdown the socket and throws following exceptions:

{ [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)

We felt that the problems is caused by https library it self that socket will be automatically shutdown if there are too many requests being sent! Therefore i am wondering is there any bug-fixed version of this https library? or is there any alternative https post/get library?? Any suggestions are appreciated! :-)

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);

来源:https://stackoverflow.com/questions/42432213/solutions-for-fixing-node-js-https-library-when-huge-amount-of-request-need-be-s

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