8000 Implement connection pooling to Orchestrator · DHLRPAadmin/orchestrator-nodejs@9001954 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9001954

Browse files
committed
Implement connection pooling to Orchestrator
1 parent fddfdfe commit 9001954

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ var orchestrator = new Orchestrator({
2525
password: 'yyy', // The Orchestrator password
2626
hostname: 'host.company.com', // The instance hostname
2727
isSecure: true, // optional (defaults to true)
28-
port: 443 // optional (defaults to 80 or 443 based on isSecure)
28+
port: 443, // optional (defaults to 80 or 443 based on isSecure)
29+
invalidCertificate: false, // optional (defaults to false)
30+
connectionPool: 5 // options, 0=unlimited (defaults to 1)
2931
});
3032
var apiPath = '/odata/Users';
3133
var apiQuery = {};

lib/Orchestrator.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function validateOptions(options) {
2323
if (options.isSecure !== true && options.isSecure !== false) {
2424
options.isSecure = true;
2525
}
26+
if (isNaN(options.connectionPool) || typeof options.connectionPool !== 'number' || options.connectionPool < 0) {
27+
options.connectionPool = 1;
28+
}
2629
return options;
2730
}
2831

@@ -41,11 +44,6 @@ function Orchestrator(options) {
4144
* @private
4245
*/
4346
this._queue = [];
44-
/**
45-
* @type {boolean}
46-
* @private
47-
*/
48-
this._processing = false;
4947
/**
5048
* @type {string|undefined}
5149
* @private
@@ -61,6 +59,16 @@ function Orchestrator(options) {
6159
if (options.invalidCertificate) {
6260
this._agent = restHelper.createAgent(options.isSecure, options.invalidCertificate);
6361
}
62+
/**
63+
* @type {Array.<function(Error=)>}
64+
* @private
65+
*/
66+
this._loginCallbackList = [];
67+
/**
68+
* @type {number}
69+
* @private
70+
*/
71+
this._pendingRequestCount = 0;
6472
}
6573

6674
module.exports = Orchestrator;
@@ -72,18 +80,27 @@ module.exports = Orchestrator;
7280
Orchestrator.prototype._login = function (callback) {
7381
var self = this;
7482
/** @type {OrchestratorOptions} */
75-
var options = this._options;
83+
var options;
7684
/** @type {OrchestratorRestHelperOptions} */
77-
var requestOptions = {
85+
var requestOptions;
86+
/** @type {Object} */
87+
var loginRequestData;
88+
89+
this._loginCallbackList.push(callback);
90+
if (this._loginCallbackList.length > 1) {
91+
// a request is already ongoing
92+
return;
93+
}
94+
options = this._options;
95+
requestOptions = {
7896
hostname: options.hostname,
7997
isSecure: options.isSecure,
8098
invalidCertificate: options.invalidCertificate,
8199
port: options.port,
82100
path: '/api/Account',
83101
agent: this._agent
84102
};
85-
/** @type {Object} */
86-
var loginRequestData = {
103+
loginRequestData = {
87104
'TenancyName': options.tenancyName,
88105
'UsernameOrEmailAddress': options.usernameOrEmailAddress,
89106
'Password': options.password
@@ -96,10 +113,17 @@ Orchestrator.prototype._login = function (callback) {
96113
* @param {OrchestratorLoginResponse} response
97114
*/
98115
function (err, response) {
116+
var i;
117+
/** @type {Array<function(Error=)>} */
118+
var loginCallbackList = self._loginCallbackList;
119+
120+
self._loginCallbackList = [];
99121
if (!err) {
100122
self._credentials = response.result || response.Result;
101123
}
102-
callback(err);
124+
for (i = 0; i < loginCallbackList.length; i += 1) {
125+
loginCallbackList[i](err);
126+
}
103127
}
104128
);
105129
};
@@ -115,7 +139,7 @@ Orchestrator.prototype._triggerQueueElementCallback = function (callback, err, d
115139
try {
116140
callback(err, data);
117141
} finally {
118-
this._processing = false;
142+
this._pendingRequestCount -= 1;
119143
this._processQueue();
120144
}
121145
};
@@ -188,14 +212,14 @@ Orchestrator.prototype._processQueue = function () {
188212
/** @type {OrchestratorQueueElement|undefined} */
189213
var element;
190214

191-
if (this._processing) { // already processing an element
215+
if (this._options.connectionPool > 0 && this._pendingRequestCount >= this._options.connectionPool) {
192216
return;
193217
}
194218
element = this._queue.shift();
195219
if (element === undefined) { // nothing left to process
196220
return;
197221
}
198-
this._processing = true;
222+
this._pendingRequestCount += 1;
199223
if (this._credentials) { // already logged-in, move on to processing the element
200224
this._processElement(element, false); // credentials may be expired
201225
return;
@@ -294,6 +318,7 @@ Orchestrator.prototype.put = function (path, data, callback) {
294318
* @property {boolean|undefined} isSecure
295319
* @property {number|undefined} port
296320
* @property {boolean|undefined} invalidCertificate
321+
* @property {number|undefined} connectionPool - 0=unlimited (Default: 1)
297322
*/
298323

299324
/**

0 commit comments

Comments
 (0)
0