-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
170 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8.15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
const DEFAULT_KEEP_ALIVE_MSECS = 60000; | ||
const DEFAULT_KEEP_ALIVE_MAX_SOCKETS = 32; | ||
const DEFAULT_KEEP_ALIVE_MAX_FREE_SOCKETS = 32; | ||
|
||
/** | ||
* Limits the creation of http.Agent instance to only one agent for keepAlive | ||
* connections and another one for 'non' keepAlive connections | ||
* The appropriate already created agent will be returned for any of the | ||
* subsequent calls to the methods so that the same agents are reused for | ||
* all requests | ||
*/ | ||
module.exports.getHTTPAgent = (function () { | ||
let keepAliveAgent; | ||
let defaultAgent; | ||
|
||
/** | ||
* Retuns a new instance of the http.Agent | ||
* @param {Boolean} isSecure is https? | ||
* @param {Object} [options] can contain | ||
* - options.keepAlive {Boolean} | ||
* - options.keepAliveMsecs {Integer} | ||
* - options.maxSockets {Integer} | ||
* - options.maxFreeSockets {Integer} | ||
* | ||
* @returns {http.Agent} Agent | ||
*/ | ||
function createAgent(isSecure, options = {}) { | ||
return new (isSecure ? https : http).Agent(options); | ||
} | ||
|
||
/** | ||
* Returns an instance of the 'keep-alive' http.Agent that _ALLOWS_ connection | ||
* pooling | ||
* It will create a new instance only the first time is invoked | ||
* @param {Boolean} isSecure is https? | ||
* | ||
* @returns {http.Agent} Agent | ||
*/ | ||
function getKeepAliveAgent(isSecure) { | ||
if (!keepAliveAgent) { | ||
keepAliveAgent = createAgent(isSecure, { | ||
keepAlive: true, | ||
keepAliveMsecs: DEFAULT_KEEP_ALIVE_MSECS, | ||
maxSockets: DEFAULT_KEEP_ALIVE_MAX_SOCKETS, | ||
maxFreeSockets: DEFAULT_KEEP_ALIVE_MAX_FREE_SOCKETS | ||
}); | ||
} | ||
|
||
return keepAliveAgent; | ||
} | ||
|
||
/** | ||
* Returns an instance of the default http.Agent that _DOES_NOT_ allow | ||
* connection pooling | ||
* It will create a new instance only the first time is invoked | ||
* @param {Boolean} isSecure is https? | ||
* | ||
* @returns {http.Agent} Agent | ||
*/ | ||
function getDefaultAgent(isSecure) { | ||
if (!defaultAgent) { | ||
defaultAgent = createAgent(isSecure); | ||
} | ||
|
||
return defaultAgent; | ||
} | ||
|
||
return { | ||
default: getDefaultAgent, | ||
keepAlive: getKeepAliveAgent | ||
}; | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*eslint no-magic-numbers: 0*/ | ||
/*eslint no-unused-expressions: 0*/ | ||
const { expect } = require('chai'); | ||
const utils = require('../../lib/utils'); | ||
|
||
describe('utils', () => { | ||
// the getHTTPAgent() function should create only an instance for http and for | ||
// https and should return the already created instance for every subsequent | ||
// request | ||
describe('getHttpAgent()', () => { | ||
describe('https', () => { | ||
it('should get keep alive agent', () => { | ||
const maxIterations = 5; | ||
const isSecure = true; | ||
|
||
// get agent that should be reused | ||
const agent = utils.getHTTPAgent.keepAlive(isSecure); | ||
// set a dummy param so that we can verify that next time the same | ||
// agent is returned. | ||
agent.foo = true; | ||
|
||
for (let i = 0; i < maxIterations; i++) { | ||
let fooAgent = utils.getHTTPAgent.keepAlive(isSecure); | ||
expect(fooAgent.foo).to.be.true; | ||
} | ||
}); | ||
|
||
it('should get default agent', () => { | ||
const maxIterations = 5; | ||
const isSecure = true; | ||
|
||
// get agent that should be reused | ||
const agent = utils.getHTTPAgent.default(isSecure); | ||
// set a dummy param so that we can verify that next time the same | ||
// agent is returned. | ||
agent.foo = true; | ||
|
||
for (let i = 0; i < maxIterations; i++) { | ||
let fooAgent = utils.getHTTPAgent.default(isSecure); | ||
expect(fooAgent.foo).to.be.true; | ||
} | ||
}); | ||
}); | ||
|
||
describe('http', () => { | ||
it('should get keep alive agent', () => { | ||
const maxIterations = 5; | ||
const isSecure = false; | ||
|
||
// get agent that should be reused | ||
const agent = utils.getHTTPAgent.keepAlive(isSecure); | ||
// set a dummy param so that we can verify that next time the same | ||
// agent is returned. | ||
agent.foo = true; | ||
|
||
for (let i = 0; i < maxIterations; i++) { | ||
let fooAgent = utils.getHTTPAgent.keepAlive(isSecure); | ||
expect(fooAgent.foo).to.be.true; | ||
} | ||
}); | ||
|
||
it('should get default agent', () => { | ||
const maxIterations = 5; | ||
const isSecure = false; | ||
|
||
// get agent that should be reused | ||
const agent = utils.getHTTPAgent.default(isSecure); | ||
// set a dummy param so that we can verify that next time the same | ||
// agent is returned. | ||
agent.foo = true; | ||
|
||
for (let i = 0; i < maxIterations; i++) { | ||
let fooAgent = utils.getHTTPAgent.default(isSecure); | ||
expect(fooAgent.foo).to.be.true; | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |