Skip to content

Commit

Permalink
Merge ff80263 into 45f60f4
Browse files Browse the repository at this point in the history
  • Loading branch information
icalderontt committed Mar 1, 2019
2 parents 45f60f4 + ff80263 commit d44e528
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 19 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.15.0
4 changes: 4 additions & 0 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.4.6 - 2018/11/02

* Fixed bug that were not allowing the use of a connection pool

# v1.4.5 - 2018/11/02

* Fixed bug in many sub-modules where ensuring authentication headers was called improperly
Expand Down
23 changes: 9 additions & 14 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
var
coreUtil = require('util'),
events = require('events'),
http = require('http'),
https = require('https'),
qs = require('querystring'),
url = require('url'),
util = require('util'),

utils = require('./utils'),
validation = require('./validation');

const
DEFAULT_KEEP_ALIVE = true,
DEFAULT_KEEP_ALIVE_MSECS = 60000,
DEFAULT_MAX_REDIRECT_COUNT = 5,
DEFAULT_RETRY_COUNT = 3,
DEFAULT_TIMEOUT = 30000,
Expand Down Expand Up @@ -168,9 +167,12 @@ module.exports = (function (self) {
augmented.keepAlive,
DEFAULT_KEEP_ALIVE);

augmented.keepAliveMsecs = validation.coalesce(
augmented.keepAliveMsecs,
DEFAULT_KEEP_ALIVE_MSECS);
// here we make sure that the appropriate http.Agent is used
if (augmented.keepAlive) {
augmented.agent = utils.getHTTPAgent.keepAlive(augmented.secure);
} else {
augmented.agent = utils.getHTTPAgent.default(augmented.secure);
}

// create `path` from pathname and query.
augmented.path = validation.coalesce(
Expand Down Expand Up @@ -218,13 +220,6 @@ module.exports = (function (self) {
options.headers = options.headers || {};
tryCount = tryCount || FIRST_TRY;

if (options.keepAlive) {
options.agent = new (options.secure ? https : http).Agent({
keepAlive: options.keepAlive,
keepAliveMsecs: options.keepAliveMsecs
});
}

let
exec,
redirectCount = 0;
Expand Down Expand Up @@ -459,7 +454,7 @@ module.exports = (function (self) {
}

// enable events
util.inherits(Request, events.EventEmitter);
coreUtil.inherits(Request, events.EventEmitter);

// return the ability to create a new request
self.Request = Request;
Expand Down
75 changes: 75 additions & 0 deletions lib/utils.js
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
};
}());
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "playnetwork-sdk",
"version": "1.4.5",
"version": "1.4.6",
"contributors": [
{
"name": "Joshua Thomas",
Expand Down
5 changes: 1 addition & 4 deletions test/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
var
chai = require('chai'),
nock = require('nock'),

request = require('../../lib/request'),

request = require('../../lib/request');
should = chai.should();


describe('request', () => {
'use strict';

Expand Down
79 changes: 79 additions & 0 deletions test/lib/utils.js
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;
}
});
});
});
});

0 comments on commit d44e528

Please sign in to comment.