用于节点 js 连接管理的 AWS-SDK

AWS-SDK for node js connection management

用于节点 js 的 aws-sdk 是否通过内部池管理它的连接?

他们的文档有点让我相信这一点。

httpOptions (map) — A set of options to pass to the low-level HTTP request. Currently supported options are:

proxy [String] — the URL to proxy requests through agent [http.Agent, https.Agent] — the Agent object to perform HTTP requests with. Used for connection pooling. Defaults to the global agent (http.globalAgent) for non-SSL connections. Note that for SSL connections, a special Agent object is used in order to enable peer certificate verification. This feature is only available in the Node.js environment.

但是至少 none 我无法找到任何方法来定义任何连接池属性。

如果我想控制正在使用的并发连接,我有哪些选择?

让 SDK 处理是否更好?

可以为 http.Agent 提供任何您想要的最大插槽设置。

var AWS = require('aws-sdk');
var http = require('http');
AWS.config.update({
  httpOptions: {
    agent: new http.Agent(...)
  }
})

我对此进行了更多调查。

我四处寻找并找出正在使用的默认值。

AWS-SDK正在使用节点http模块,其中defaultSocketCountINFINITY

他们正在使用 https 模块,maxSocketCount50

相关代码片段。

sslAgent: function sslAgent() {
    var https = require('https');

    if (!AWS.NodeHttpClient.sslAgent) {
      AWS.NodeHttpClient.sslAgent = new https.Agent({rejectUnauthorized: true});
      AWS.NodeHttpClient.sslAgent.setMaxListeners(0);

      // delegate maxSockets to globalAgent, set a default limit of 50 if current value is Infinity.
      // Users can bypass this default by supplying their own Agent as part of SDK configuration.
      Object.defineProperty(AWS.NodeHttpClient.sslAgent, 'maxSockets', {
        enumerable: true,
        get: function() {
          var defaultMaxSockets = 50;
          var globalAgent = https.globalAgent;
          if (globalAgent && globalAgent.maxSockets !== Infinity && typeof globalAgent.maxSockets === 'number') {
            return globalAgent.maxSockets;
          }
          return defaultMaxSockets;
        }
      });
    }
    return AWS.NodeHttpClient.sslAgent;
  }

有关处理套接字计数的信息,请参阅 BretzL 的回答。

但是现在可以同时为 httphttps 设置代理。您可以通过在从 http 切换到 https 时更新配置来解决此问题,反之亦然。

参见:https://github.com/aws/aws-sdk-js/issues/1185