Skip to content

Commit 3fad4f7

Browse files
committed
Default to using maxSockets setting from https.globalAgent.
This change allows the SDK to inherit the maxSockets property from the global agent in Node.js so that users do not have to pass in a custom agent to increase maxSockets. Instead, this change allows users to do the following to increase maxSockets: ```javascript require('https').globalAgent.maxSockets = 20; var s3 = new AWS.S3(); for (var i = 0; i < 20; i++) { s3.getObject(params, function (err, data) { /* ... */ }); } ``` Fixes #224
1 parent f359c7f commit 3fad4f7

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/http/node.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ AWS.NodeHttpClient = AWS.util.inherit({
3131
};
3232

3333
if (useSSL && !httpOptions.agent) {
34-
options.agent = this.sslAgent(http);
34+
options.agent = this.sslAgent();
3535
}
3636

3737
AWS.util.update(options, httpOptions);
@@ -80,12 +80,18 @@ AWS.NodeHttpClient = AWS.util.inherit({
8080
}
8181
},
8282

83-
sslAgent: function sslAgent(http) {
83+
sslAgent: function sslAgent() {
84+
var https = require('https');
85+
8486
if (!AWS.NodeHttpClient.sslAgent) {
85-
AWS.NodeHttpClient.sslAgent = new http.Agent({
86-
rejectUnauthorized: true
87-
});
87+
AWS.NodeHttpClient.sslAgent = new https.Agent({rejectUnauthorized: true});
8888
AWS.NodeHttpClient.sslAgent.setMaxListeners(0);
89+
90+
// delegate maxSockets to globalAgent
91+
Object.defineProperty(AWS.NodeHttpClient.sslAgent, 'maxSockets', {
92+
enumerable: true,
93+
get: function() { return https.globalAgent.maxSockets; }
94+
});
8995
}
9096
return AWS.NodeHttpClient.sslAgent;
9197
},

test/node_http_client.spec.coffee

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ if AWS.util.isNode()
55
describe 'AWS.NodeHttpClient', ->
66
http = new AWS.NodeHttpClient()
77

8+
describe 'maxSockets delegation', ->
9+
it 'delegates maxSockets from agent to globalAgent', ->
10+
https = require('https')
11+
agent = http.sslAgent()
12+
expect(https.globalAgent.maxSockets).toEqual(agent.maxSockets)
13+
https.globalAgent.maxSockets += 1
14+
expect(https.globalAgent.maxSockets).toEqual(agent.maxSockets)
15+
816
describe 'handleRequest', ->
917
it 'emits error event', ->
1018
error = null

0 commit comments

Comments
 (0)