whistle 2.10.13 → 2.10.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/config.js CHANGED
@@ -107,7 +107,6 @@ config.PLUGIN_HOOKS = {
107
107
  TUNNEL_RES_WRITE: 'tunnel-res-write-' + uid
108
108
  };
109
109
 
110
- var KEEP_ALIVE_MSECS = 10000;
111
110
  var CONN_TIMEOUT = common.CONN_TIMEOUT;
112
111
  var DNS_ORDERS_OPTS = ['', 'ipv4first', 'ipv4first', hasIPv6First ? 'ipv6first' : '', '', 'verbatim'];
113
112
  var DOMAIN_STAR_RE = /([*~]+)(\\.)?/g;
@@ -490,6 +489,30 @@ function readUIExt(uiExt) {
490
489
  }
491
490
  }
492
491
 
492
+ function keepAgentUnblocked(agent) {
493
+ setInterval(function() {
494
+ var requests = agent.requests;
495
+ var keys = requests && Object.keys(requests);
496
+ keys && keys.forEach(function (key) {
497
+ var reqs = requests[key];
498
+ var len = reqs && reqs.length;
499
+ var sockets = agent.sockets[key];
500
+ var sockLen = len && sockets && sockets.length;
501
+ if (sockLen) {
502
+ len = Math.min(sockLen, Math.ceil(len / 2));
503
+ sockets = sockets.slice();
504
+ for (var i = 0; i < len; i++) {
505
+ var socket = sockets[i];
506
+ if (socket.readyState === 'open') {
507
+ socket.emit('agentRemove');
508
+ }
509
+ }
510
+ }
511
+ });
512
+ }, 1000);
513
+ return agent;
514
+ }
515
+
493
516
  exports.extend = function (newConf) {
494
517
  config.pluginHostMap = {};
495
518
  config.uiport = config.port;
@@ -1036,16 +1059,18 @@ exports.extend = function (newConf) {
1036
1059
  if (config.strict) {
1037
1060
  config.sockets = Math.max(config.sockets, 360);
1038
1061
  }
1039
- var agentConfig = newAgentConf || {
1040
- maxSockets: config.sockets,
1062
+ // fix: kern.ipc.somaxconn
1063
+ var localAgentConf = {
1041
1064
  keepAlive: true,
1042
- keepAliveMsecs: KEEP_ALIVE_MSECS,
1065
+ maxSockets: config.sockets,
1043
1066
  maxFreeSockets: 0
1044
1067
  };
1068
+ config.localAgent = keepAgentUnblocked(new HttpAgent(localAgentConf));
1069
+ var agentConfig = newAgentConf || localAgentConf;
1045
1070
  // node 11及以上版本缓存连接有问题,默认禁掉
1046
1071
  var disableAgent = !newAgentConf && !useAgent;
1047
- config.httpAgent = disableAgent ? false : new HttpAgent(agentConfig);
1048
- config.httpsAgent = disableAgent ? false : new HttpsAgent(agentConfig);
1072
+ config.httpAgent = disableAgent ? false : keepAgentUnblocked(new HttpAgent(agentConfig));
1073
+ config.httpsAgent = disableAgent ? false : keepAgentUnblocked(new HttpsAgent(agentConfig));
1049
1074
  config.getSocksAgent = function (options) {
1050
1075
  var key = getCacheKey(options, true);
1051
1076
  var agent = socksAgents.get(key);
@@ -1044,7 +1044,7 @@ function toHttp1(req, res) {
1044
1044
  var options = util.parseUrl(util.getFullUrl(req));
1045
1045
  options.protocol = null;
1046
1046
  options.hostname = null;
1047
- options.agent = false;
1047
+ options.agent = config.localAgent;
1048
1048
  options.host = config.host || LOCALHOST;
1049
1049
  options.port = config.port;
1050
1050
  options.headers = headers;