@@ -23,6 +23,9 @@ function validateOptions(options) {
23
23
if ( options . isSecure !== true && options . isSecure !== false ) {
24
24
options . isSecure = true ;
25
25
}
26
+ if ( isNaN ( options . connectionPool ) || typeof options . connectionPool !== 'number' || options . connectionPool < 0 ) {
27
+ options . connectionPool = 1 ;
28
+ }
26
29
return options ;
27
30
}
28
31
@@ -41,11 +44,6 @@ function Orchestrator(options) {
41
44
* @private
42
45
*/
43
46
this . _queue = [ ] ;
44
- /**
45
- * @type {boolean }
46
- * @private
47
- */
48
- this . _processing = false ;
49
47
/**
50
48
* @type {string|undefined }
51
49
* @private
@@ -61,6 +59,16 @@ function Orchestrator(options) {
61
59
if ( options . invalidCertificate ) {
62
60
this . _agent = restHelper . createAgent ( options . isSecure , options . invalidCertificate ) ;
63
61
}
62
+ /**
63
+ * @type {Array.<function(Error=)> }
64
+ * @private
65
+ */
66
+ this . _loginCallbackList = [ ] ;
67
+ /**
68
+ * @type {number }
69
+ * @private
70
+ */
71
+ this . _pendingRequestCount = 0 ;
64
72
}
65
73
66
74
module . exports = Orchestrator ;
@@ -72,18 +80,27 @@ module.exports = Orchestrator;
72
80
Orchestrator . prototype . _login = function ( callback ) {
73
81
var self = this ;
74
82
/** @type {OrchestratorOptions } */
75
- var options = this . _options ;
83
+ var options ;
76
84
/** @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 = {
78
96
hostname : options . hostname ,
79
97
isSecure : options . isSecure ,
80
98
invalidCertificate : options . invalidCertificate ,
81
99
port : options . port ,
82
100
path : '/api/Account' ,
83
101
agent : this . _agent
84
102
} ;
85
- /** @type {Object } */
86
- var loginRequestData = {
103
+ loginRequestData = {
87
104
'TenancyName' : options . tenancyName ,
88
105
'UsernameOrEmailAddress' : options . usernameOrEmailAddress ,
89
106
'Password' : options . password
@@ -96,10 +113,17 @@ Orchestrator.prototype._login = function (callback) {
96
113
* @param {OrchestratorLoginResponse } response
97
114
*/
98
115
function ( err , response ) {
116
+ var i ;
117
+ /** @type {Array<function(Error=)> } */
118
+ var loginCallbackList = self . _loginCallbackList ;
119
+
120
+ self . _loginCallbackList = [ ] ;
99
121
if ( ! err ) {
100
122
self . _credentials = response . result || response . Result ;
101
123
}
102
- callback ( err ) ;
124
+ for ( i = 0 ; i < loginCallbackList . length ; i += 1 ) {
125
+ loginCallbackList [ i ] ( err ) ;
126
+ }
103
127
}
104
128
) ;
105
129
} ;
@@ -115,7 +139,7 @@ Orchestrator.prototype._triggerQueueElementCallback = function (callback, err, d
115
139
try {
116
140
callback ( err , data ) ;
117
141
} finally {
118
- this . _processing = false ;
142
+ this . _pendingRequestCount -= 1 ;
119
143
this . _processQueue ( ) ;
120
144
}
121
145
} ;
@@ -188,14 +212,14 @@ Orchestrator.prototype._processQueue = function () {
188
212
/** @type {OrchestratorQueueElement|undefined } */
189
213
var element ;
190
214
191
- if ( this . _processing ) { // already processing an element
215
+ if ( this . _options . connectionPool > 0 && this . _pendingRequestCount >= this . _options . connectionPool ) {
192
216
return ;
193
217
}
194
218
element = this . _queue . shift ( ) ;
195
219
if ( element === undefined ) { // nothing left to process
196
220
return ;
197
221
}
198
- this . _processing = true ;
222
+ this . _pendingRequestCount += 1 ;
199
223
if ( this . _credentials ) { // already logged-in, move on to processing the element
200
224
this . _processElement ( element , false ) ; // credentials may be expired
201
225
return ;
@@ -294,6 +318,7 @@ Orchestrator.prototype.put = function (path, data, callback) {
294
318
* @property {boolean|undefined } isSecure
295
319
* @property {number|undefined } port
296
320
* @property {boolean|undefined } invalidCertificate
321
+ * @property {number|undefined } connectionPool - 0=unlimited (Default: 1)
297
322
*/
298
323
299
324
/**
0 commit comments