Skip to content
This repository was archived by the owner on Feb 5, 2020. It is now read-only.

Commit 7083307

Browse files
committed
Use an agent to regulate connections.
1 parent 065a724 commit 7083307

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/util/HttpClient.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var TransformIterator = require('asynciterator').TransformIterator,
1212
* It performs request pooling and time-based content negotiation.
1313
* @param {String} [options.request] The HTTP request module to use
1414
* @param {String} [options.contentType=* / *] The desired content type of representations
15+
* @param {integer} [options.concurrentRequests=10] Maximum number of concurrent requests per client
1516
* @constructor
1617
*/
1718
function HttpClient(options) {
@@ -27,7 +28,7 @@ function HttpClient(options) {
2728
'accept-datetime': options.datetime && options.datetime.toUTCString(),
2829
}, _.identity);
2930
this._logger = options.logger || logger('HttpClient');
30-
this._maxActiveRequestCount = options.concurrentRequests || 5;
31+
this._maxActiveRequestCount = options.concurrentRequests || 10;
3132

3233
// Set up request queue
3334
this._requestId = 0;

lib/util/Request.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ var EventEmitter = require('events').EventEmitter,
88
https = require('follow-redirects').https,
99
zlib = require('zlib');
1010

11+
// Try to keep connections open, and set a maximum number of connections per server
12+
var AGENT_SETTINGS = { keepAlive: true, maxSockets: 5 };
13+
var AGENTS = {
14+
'http:': new http.Agent(AGENT_SETTINGS),
15+
'https:': new https.Agent(AGENT_SETTINGS),
16+
};
17+
18+
// Decode encoded streams with these decoders
1119
var DECODERS = { gzip: zlib.createGunzip, deflate: zlib.createInflate };
1220

1321
// Creates an HTTP request with the given settings
@@ -19,6 +27,7 @@ function createRequest(settings) {
1927
// Emit the response through a proxy
2028
var request, requestProxy = new EventEmitter(),
2129
requester = settings.protocol === 'http:' ? http : https;
30+
settings.agent = AGENTS[settings.protocol];
2231
request = requester.request(settings, function (response) {
2332
response = decode(response);
2433
response.setEncoding('utf8');

0 commit comments

Comments
 (0)