lightning-pool 4.4.2 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/abort-error.js +5 -1
- package/esm/definitions.js +7 -4
- package/esm/index.js +10 -6
- package/esm/pool-options.js +6 -2
- package/esm/pool-request.js +5 -1
- package/esm/pool.js +54 -49
- package/esm/resource-item.js +7 -3
- package/package.json +18 -11
- package/types/abort-error.d.ts +2 -0
- package/types/definitions.d.ts +34 -0
- package/types/index.d.ts +6 -0
- package/types/pool-options.d.ts +41 -0
- package/types/pool-request.d.ts +11 -0
- package/types/pool.d.ts +103 -0
- package/types/resource-item.d.ts +11 -0
- package/cjs/package.json +0 -3
package/esm/abort-error.js
CHANGED
package/esm/definitions.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourceState = exports.PoolState = void 0;
|
|
4
|
+
var PoolState;
|
|
2
5
|
(function (PoolState) {
|
|
3
6
|
PoolState[PoolState["IDLE"] = 0] = "IDLE";
|
|
4
7
|
PoolState[PoolState["STARTED"] = 1] = "STARTED";
|
|
5
8
|
PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
|
|
6
9
|
PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
|
|
7
|
-
})(PoolState || (PoolState = {}));
|
|
8
|
-
|
|
10
|
+
})(PoolState || (exports.PoolState = PoolState = {}));
|
|
11
|
+
var ResourceState;
|
|
9
12
|
(function (ResourceState) {
|
|
10
13
|
ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
|
|
11
14
|
ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
|
|
12
15
|
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
|
|
13
|
-
})(ResourceState || (ResourceState = {}));
|
|
16
|
+
})(ResourceState || (exports.ResourceState = ResourceState = {}));
|
package/esm/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPool = createPool;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const pool_js_1 = require("./pool.js");
|
|
6
|
+
tslib_1.__exportStar(require("./abort-error.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./definitions.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./pool.js"), exports);
|
|
9
|
+
function createPool(factory, config) {
|
|
10
|
+
return new pool_js_1.Pool(factory, config);
|
|
7
11
|
}
|
package/esm/pool-options.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PoolOptions = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
2
5
|
const defaultValues = {
|
|
3
6
|
acquireMaxRetries: 0,
|
|
4
7
|
acquireRetryWait: 2000,
|
|
@@ -12,7 +15,7 @@ const defaultValues = {
|
|
|
12
15
|
maxQueue: 1000,
|
|
13
16
|
validation: true,
|
|
14
17
|
};
|
|
15
|
-
|
|
18
|
+
class PoolOptions extends events_1.EventEmitter {
|
|
16
19
|
constructor(pool) {
|
|
17
20
|
super();
|
|
18
21
|
this.pool = pool;
|
|
@@ -122,3 +125,4 @@ export class PoolOptions extends EventEmitter {
|
|
|
122
125
|
}
|
|
123
126
|
}
|
|
124
127
|
}
|
|
128
|
+
exports.PoolOptions = PoolOptions;
|
package/esm/pool-request.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PoolRequest = void 0;
|
|
1
4
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
2
5
|
function noop() { }
|
|
3
|
-
|
|
6
|
+
class PoolRequest {
|
|
4
7
|
constructor(pool, callback, options) {
|
|
5
8
|
this.timedOut = false;
|
|
6
9
|
this.created = Date.now();
|
|
@@ -20,3 +23,4 @@ export class PoolRequest {
|
|
|
20
23
|
clearTimeout(this.timeoutHandle);
|
|
21
24
|
}
|
|
22
25
|
}
|
|
26
|
+
exports.PoolRequest = PoolRequest;
|
package/esm/pool.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Pool = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
|
|
6
|
+
const events_1 = require("events");
|
|
7
|
+
const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
|
|
8
|
+
const abort_error_js_1 = require("./abort-error.js");
|
|
9
|
+
const definitions_js_1 = require("./definitions.js");
|
|
10
|
+
const pool_options_js_1 = require("./pool-options.js");
|
|
11
|
+
const pool_request_js_1 = require("./pool-request.js");
|
|
12
|
+
const resource_item_js_1 = require("./resource-item.js");
|
|
13
|
+
class Pool extends events_1.EventEmitter {
|
|
10
14
|
constructor(factory, config) {
|
|
11
15
|
super();
|
|
12
|
-
this._requestQueue = new
|
|
16
|
+
this._requestQueue = new doublylinked_1.default();
|
|
13
17
|
this._allResources = new Map();
|
|
14
|
-
this._acquiredResources = new
|
|
15
|
-
this._idleResources = new
|
|
18
|
+
this._acquiredResources = new doublylinked_1.default();
|
|
19
|
+
this._idleResources = new doublylinked_1.default();
|
|
16
20
|
this._creating = 0;
|
|
17
21
|
this._requestsProcessing = 0;
|
|
18
|
-
this._state = PoolState.IDLE;
|
|
22
|
+
this._state = definitions_js_1.PoolState.IDLE;
|
|
19
23
|
if (typeof factory !== 'object') {
|
|
20
24
|
throw new TypeError('You must provide `factory` object');
|
|
21
25
|
}
|
|
@@ -31,7 +35,7 @@ export class Pool extends EventEmitter {
|
|
|
31
35
|
if (factory.reset && typeof factory.reset !== 'function') {
|
|
32
36
|
throw new TypeError('factory.reset can be a function');
|
|
33
37
|
}
|
|
34
|
-
const opts = (this._options = new PoolOptions(this));
|
|
38
|
+
const opts = (this._options = new pool_options_js_1.PoolOptions(this));
|
|
35
39
|
opts.on('change', (prop, val) => {
|
|
36
40
|
if (prop === 'houseKeepInterval')
|
|
37
41
|
this._setHouseKeep(val);
|
|
@@ -90,12 +94,12 @@ export class Pool extends EventEmitter {
|
|
|
90
94
|
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
91
95
|
*/
|
|
92
96
|
start() {
|
|
93
|
-
if (this._state === PoolState.STARTED)
|
|
97
|
+
if (this._state === definitions_js_1.PoolState.STARTED)
|
|
94
98
|
return;
|
|
95
|
-
if (this._state >= PoolState.CLOSING) {
|
|
99
|
+
if (this._state >= definitions_js_1.PoolState.CLOSING) {
|
|
96
100
|
throw new Error('Closed pool can not be started again');
|
|
97
101
|
}
|
|
98
|
-
this._state = PoolState.STARTED;
|
|
102
|
+
this._state = definitions_js_1.PoolState.STARTED;
|
|
99
103
|
this._setHouseKeep(this.options.houseKeepInterval);
|
|
100
104
|
this._ensureMin();
|
|
101
105
|
this.emit('start');
|
|
@@ -110,21 +114,21 @@ export class Pool extends EventEmitter {
|
|
|
110
114
|
callback = arg1;
|
|
111
115
|
}
|
|
112
116
|
if (!callback) {
|
|
113
|
-
return
|
|
117
|
+
return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb));
|
|
114
118
|
}
|
|
115
|
-
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) {
|
|
119
|
+
if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) {
|
|
116
120
|
return callback();
|
|
117
121
|
}
|
|
118
|
-
if (this._state === PoolState.CLOSING) {
|
|
122
|
+
if (this._state === definitions_js_1.PoolState.CLOSING) {
|
|
119
123
|
this.once('close', callback);
|
|
120
124
|
return;
|
|
121
125
|
}
|
|
122
126
|
this.emit('closing');
|
|
123
127
|
if (this._houseKeepTimer)
|
|
124
128
|
clearTimeout(this._houseKeepTimer);
|
|
125
|
-
this._state = PoolState.CLOSING;
|
|
129
|
+
this._state = definitions_js_1.PoolState.CLOSING;
|
|
126
130
|
this._requestQueue.forEach(t => t.stopTimout());
|
|
127
|
-
this._requestQueue = new
|
|
131
|
+
this._requestQueue = new doublylinked_1.default();
|
|
128
132
|
this._requestsProcessing = 0;
|
|
129
133
|
if (terminateWait <= 0) {
|
|
130
134
|
this._acquiredResources.forEach(t => this.destroy(t.resource));
|
|
@@ -149,11 +153,11 @@ export class Pool extends EventEmitter {
|
|
|
149
153
|
this.once('close', callback);
|
|
150
154
|
}
|
|
151
155
|
closeAsync(arg0) {
|
|
152
|
-
return
|
|
156
|
+
return putil_promisify_1.default.fromCallback(cb => this.close(arg0, cb));
|
|
153
157
|
}
|
|
154
158
|
acquire(callback) {
|
|
155
159
|
if (!callback)
|
|
156
|
-
return
|
|
160
|
+
return putil_promisify_1.default.fromCallback(cb => this.acquire(cb));
|
|
157
161
|
try {
|
|
158
162
|
this.start();
|
|
159
163
|
}
|
|
@@ -163,7 +167,7 @@ export class Pool extends EventEmitter {
|
|
|
163
167
|
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
164
168
|
return callback(new Error('Pool queue is full'));
|
|
165
169
|
}
|
|
166
|
-
this._requestQueue.push(new PoolRequest(this, callback));
|
|
170
|
+
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback));
|
|
167
171
|
this._processNextRequest();
|
|
168
172
|
}
|
|
169
173
|
/**
|
|
@@ -171,7 +175,7 @@ export class Pool extends EventEmitter {
|
|
|
171
175
|
*/
|
|
172
176
|
release(resource, callback) {
|
|
173
177
|
const item = this._allResources.get(resource);
|
|
174
|
-
if (item && item.state !== ResourceState.IDLE) {
|
|
178
|
+
if (item && item.state !== definitions_js_1.ResourceState.IDLE) {
|
|
175
179
|
this._itemSetIdle(item, callback);
|
|
176
180
|
}
|
|
177
181
|
this._processNextRequest();
|
|
@@ -180,7 +184,7 @@ export class Pool extends EventEmitter {
|
|
|
180
184
|
* Async version of release().
|
|
181
185
|
*/
|
|
182
186
|
releaseAsync(resource) {
|
|
183
|
-
return
|
|
187
|
+
return putil_promisify_1.default.fromCallback(cb => this.release(resource, cb));
|
|
184
188
|
}
|
|
185
189
|
/**
|
|
186
190
|
* Releases, destroys and removes any `resource` from `Pool`.
|
|
@@ -201,7 +205,7 @@ export class Pool extends EventEmitter {
|
|
|
201
205
|
* Async version of destroy().
|
|
202
206
|
*/
|
|
203
207
|
destroyAsync(resource) {
|
|
204
|
-
return
|
|
208
|
+
return putil_promisify_1.default.fromCallback(cb => this.destroy(resource, cb));
|
|
205
209
|
}
|
|
206
210
|
/**
|
|
207
211
|
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
@@ -217,7 +221,7 @@ export class Pool extends EventEmitter {
|
|
|
217
221
|
return this._allResources.has(resource);
|
|
218
222
|
}
|
|
219
223
|
_processNextRequest() {
|
|
220
|
-
if (this._state !== PoolState.STARTED ||
|
|
224
|
+
if (this._state !== definitions_js_1.PoolState.STARTED ||
|
|
221
225
|
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
222
226
|
return;
|
|
223
227
|
}
|
|
@@ -231,7 +235,7 @@ export class Pool extends EventEmitter {
|
|
|
231
235
|
try {
|
|
232
236
|
if (item) {
|
|
233
237
|
/* istanbul ignore next : Hard to simulate */
|
|
234
|
-
if (this._state !== PoolState.STARTED) {
|
|
238
|
+
if (this._state !== definitions_js_1.PoolState.STARTED) {
|
|
235
239
|
this._itemDestroy(item);
|
|
236
240
|
return;
|
|
237
241
|
}
|
|
@@ -297,7 +301,7 @@ export class Pool extends EventEmitter {
|
|
|
297
301
|
tries,
|
|
298
302
|
maxRetries: this.options.acquireMaxRetries,
|
|
299
303
|
});
|
|
300
|
-
if (err instanceof AbortError || tries >= maxRetries) {
|
|
304
|
+
if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) {
|
|
301
305
|
this._creating--;
|
|
302
306
|
return callback && callback(err);
|
|
303
307
|
}
|
|
@@ -308,7 +312,7 @@ export class Pool extends EventEmitter {
|
|
|
308
312
|
return (callback &&
|
|
309
313
|
callback(new Error('Factory error. Resource already in pool')));
|
|
310
314
|
}
|
|
311
|
-
const item = new ResourceItem(obj);
|
|
315
|
+
const item = new resource_item_js_1.ResourceItem(obj);
|
|
312
316
|
this._itemSetIdle(item);
|
|
313
317
|
this._allResources.set(obj, item);
|
|
314
318
|
if (callback)
|
|
@@ -320,9 +324,9 @@ export class Pool extends EventEmitter {
|
|
|
320
324
|
const o = this._factory.create({ tries, maxRetries });
|
|
321
325
|
/* istanbul ignore next */
|
|
322
326
|
if (!o) {
|
|
323
|
-
return handleCallback(new AbortError('Factory returned no resource'));
|
|
327
|
+
return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource'));
|
|
324
328
|
}
|
|
325
|
-
|
|
329
|
+
putil_promisify_1.default.await(o, handleCallback);
|
|
326
330
|
}
|
|
327
331
|
catch (e) {
|
|
328
332
|
handleCallback(e);
|
|
@@ -334,13 +338,13 @@ export class Pool extends EventEmitter {
|
|
|
334
338
|
if (this._houseKeepTimer)
|
|
335
339
|
clearInterval(this._houseKeepTimer);
|
|
336
340
|
this._houseKeepTimer = undefined;
|
|
337
|
-
if ((ms > 0 && this.state === PoolState.STARTED) ||
|
|
338
|
-
this.state === PoolState.CLOSING) {
|
|
341
|
+
if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) ||
|
|
342
|
+
this.state === definitions_js_1.PoolState.CLOSING) {
|
|
339
343
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
340
344
|
}
|
|
341
345
|
}
|
|
342
346
|
_houseKeep() {
|
|
343
|
-
const isClosing = this._state === PoolState.CLOSING;
|
|
347
|
+
const isClosing = this._state === definitions_js_1.PoolState.CLOSING;
|
|
344
348
|
const now = Date.now();
|
|
345
349
|
let m = this._allResources.size - this.options.min;
|
|
346
350
|
let n = this._idleResources.length - this.options.minIdle;
|
|
@@ -358,7 +362,7 @@ export class Pool extends EventEmitter {
|
|
|
358
362
|
if (this._allResources.size)
|
|
359
363
|
return;
|
|
360
364
|
clearInterval(this._houseKeepTimer);
|
|
361
|
-
this._state = PoolState.CLOSED;
|
|
365
|
+
this._state = definitions_js_1.PoolState.CLOSED;
|
|
362
366
|
this._requestsProcessing = 0;
|
|
363
367
|
this.emit('close');
|
|
364
368
|
}
|
|
@@ -371,24 +375,24 @@ export class Pool extends EventEmitter {
|
|
|
371
375
|
});
|
|
372
376
|
}
|
|
373
377
|
_itemSetAcquired(item) {
|
|
374
|
-
if (item.state !== ResourceState.ACQUIRED) {
|
|
378
|
+
if (item.state !== definitions_js_1.ResourceState.ACQUIRED) {
|
|
375
379
|
this._itemDetach(item);
|
|
376
|
-
item.state = ResourceState.ACQUIRED;
|
|
380
|
+
item.state = definitions_js_1.ResourceState.ACQUIRED;
|
|
377
381
|
this._acquiredResources.push(item);
|
|
378
382
|
item.acquiredNode = this._acquiredResources.tail;
|
|
379
383
|
}
|
|
380
384
|
}
|
|
381
385
|
_itemDetach(item) {
|
|
382
386
|
switch (item.state) {
|
|
383
|
-
case ResourceState.IDLE:
|
|
387
|
+
case definitions_js_1.ResourceState.IDLE:
|
|
384
388
|
item.idleTime = 0;
|
|
385
389
|
/* istanbul ignore next*/
|
|
386
390
|
if (item.idleNode)
|
|
387
391
|
item.idleNode.remove();
|
|
388
392
|
item.idleNode = undefined;
|
|
389
393
|
break;
|
|
390
|
-
case ResourceState.ACQUIRED:
|
|
391
|
-
case ResourceState.VALIDATION:
|
|
394
|
+
case definitions_js_1.ResourceState.ACQUIRED:
|
|
395
|
+
case definitions_js_1.ResourceState.VALIDATION:
|
|
392
396
|
/* istanbul ignore next*/
|
|
393
397
|
if (item.acquiredNode)
|
|
394
398
|
item.acquiredNode.remove();
|
|
@@ -399,13 +403,13 @@ export class Pool extends EventEmitter {
|
|
|
399
403
|
}
|
|
400
404
|
}
|
|
401
405
|
_itemSetIdle(item, callback) {
|
|
402
|
-
const isAcquired = item.state === ResourceState.ACQUIRED;
|
|
406
|
+
const isAcquired = item.state === definitions_js_1.ResourceState.ACQUIRED;
|
|
403
407
|
const handleCallback = (err) => {
|
|
404
408
|
if (err)
|
|
405
409
|
return this._itemDestroy(item, callback);
|
|
406
410
|
this._itemDetach(item);
|
|
407
411
|
item.idleTime = Date.now();
|
|
408
|
-
item.state = ResourceState.IDLE;
|
|
412
|
+
item.state = definitions_js_1.ResourceState.IDLE;
|
|
409
413
|
if (this.options.fifo) {
|
|
410
414
|
this._idleResources.push(item);
|
|
411
415
|
item.idleNode = this._idleResources.tail;
|
|
@@ -424,7 +428,7 @@ export class Pool extends EventEmitter {
|
|
|
424
428
|
if (isAcquired && this._factory.reset) {
|
|
425
429
|
try {
|
|
426
430
|
const o = this._factory.reset(item.resource);
|
|
427
|
-
|
|
431
|
+
putil_promisify_1.default.await(o, handleCallback);
|
|
428
432
|
}
|
|
429
433
|
catch (e) {
|
|
430
434
|
handleCallback(e);
|
|
@@ -450,18 +454,18 @@ export class Pool extends EventEmitter {
|
|
|
450
454
|
this._allResources.delete(item.resource);
|
|
451
455
|
this._processNextRequest();
|
|
452
456
|
const o = this._factory.destroy(item.resource);
|
|
453
|
-
|
|
457
|
+
putil_promisify_1.default.await(o, handleCallback);
|
|
454
458
|
}
|
|
455
459
|
catch (e) {
|
|
456
460
|
handleCallback(e);
|
|
457
461
|
}
|
|
458
462
|
}
|
|
459
463
|
_itemValidate(item, callback) {
|
|
460
|
-
item.state = ResourceState.VALIDATION;
|
|
464
|
+
item.state = definitions_js_1.ResourceState.VALIDATION;
|
|
461
465
|
try {
|
|
462
466
|
const o = this._factory.validate?.(item.resource);
|
|
463
467
|
// @ts-ignore
|
|
464
|
-
|
|
468
|
+
putil_promisify_1.default.await(o, callback);
|
|
465
469
|
}
|
|
466
470
|
catch (e) {
|
|
467
471
|
if (callback)
|
|
@@ -469,3 +473,4 @@ export class Pool extends EventEmitter {
|
|
|
469
473
|
}
|
|
470
474
|
}
|
|
471
475
|
}
|
|
476
|
+
exports.Pool = Pool;
|
package/esm/resource-item.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResourceItem = void 0;
|
|
4
|
+
const definitions_js_1 = require("./definitions.js");
|
|
5
|
+
class ResourceItem {
|
|
3
6
|
constructor(resource) {
|
|
4
|
-
this.state = ResourceState.IDLE;
|
|
7
|
+
this.state = definitions_js_1.ResourceState.IDLE;
|
|
5
8
|
this.idleTime = 0;
|
|
6
9
|
this.destroyed = false;
|
|
7
10
|
this.resource = resource;
|
|
8
11
|
}
|
|
9
12
|
}
|
|
13
|
+
exports.ResourceItem = ResourceItem;
|
package/package.json
CHANGED
|
@@ -1,30 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lightning-pool",
|
|
3
3
|
"description": "Fastest generic Pool written with TypeScript",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.5.0",
|
|
5
5
|
"author": "Panates Ltd.",
|
|
6
|
-
"contributors": [
|
|
7
|
-
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
8
|
-
],
|
|
9
6
|
"license": "MIT",
|
|
10
7
|
"repository": "panates/lightning-pool",
|
|
11
|
-
"keywords": [
|
|
12
|
-
"pool",
|
|
13
|
-
"generic",
|
|
14
|
-
"generic-pool"
|
|
15
|
-
],
|
|
16
8
|
"dependencies": {
|
|
17
9
|
"doublylinked": "^2.5.4",
|
|
18
10
|
"putil-promisify": "^1.10.1",
|
|
19
11
|
"tslib": "^2.6.3"
|
|
20
12
|
},
|
|
21
|
-
"type": "module",
|
|
22
13
|
"main": "./cjs/index.js",
|
|
23
14
|
"module": "./esm/index.js",
|
|
24
|
-
"types": "./
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": "./cjs/index.js",
|
|
19
|
+
"import": "./esm/index.js",
|
|
20
|
+
"types": "./types/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"contributors": [
|
|
24
|
+
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"pool",
|
|
28
|
+
"generic",
|
|
29
|
+
"generic-pool"
|
|
30
|
+
],
|
|
25
31
|
"files": [
|
|
26
32
|
"cjs/",
|
|
27
33
|
"esm/",
|
|
34
|
+
"types/",
|
|
28
35
|
"LICENSE",
|
|
29
36
|
"README.md"
|
|
30
37
|
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type Callback = (e?: unknown, ...args: any[]) => void;
|
|
2
|
+
export declare enum PoolState {
|
|
3
|
+
IDLE = 0,
|
|
4
|
+
STARTED = 1,
|
|
5
|
+
CLOSING = 2,
|
|
6
|
+
CLOSED = 3
|
|
7
|
+
}
|
|
8
|
+
export declare enum ResourceState {
|
|
9
|
+
IDLE = 0,
|
|
10
|
+
ACQUIRED = 1,
|
|
11
|
+
VALIDATION = 2
|
|
12
|
+
}
|
|
13
|
+
export interface PoolFactory<T = any> {
|
|
14
|
+
create(info?: {
|
|
15
|
+
tries: number;
|
|
16
|
+
maxRetries: number;
|
|
17
|
+
}): Promise<T> | T;
|
|
18
|
+
destroy(resource: T): Promise<void> | void;
|
|
19
|
+
reset?(resource: T): Promise<void> | void;
|
|
20
|
+
validate?(resource: T): Promise<void> | void;
|
|
21
|
+
}
|
|
22
|
+
export interface PoolConfiguration {
|
|
23
|
+
acquireMaxRetries?: number;
|
|
24
|
+
acquireRetryWait?: number;
|
|
25
|
+
acquireTimeoutMillis?: number;
|
|
26
|
+
fifo?: boolean;
|
|
27
|
+
idleTimeoutMillis?: number;
|
|
28
|
+
houseKeepInterval?: number;
|
|
29
|
+
min?: number;
|
|
30
|
+
minIdle?: number;
|
|
31
|
+
max?: number;
|
|
32
|
+
maxQueue?: number;
|
|
33
|
+
validation?: boolean;
|
|
34
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PoolConfiguration, PoolFactory } from './definitions.js';
|
|
2
|
+
import { Pool } from './pool.js';
|
|
3
|
+
export * from './abort-error.js';
|
|
4
|
+
export * from './definitions.js';
|
|
5
|
+
export * from './pool.js';
|
|
6
|
+
export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { PoolConfiguration } from './definitions.js';
|
|
3
|
+
import { Pool } from './pool.js';
|
|
4
|
+
export declare class PoolOptions extends EventEmitter {
|
|
5
|
+
readonly pool: Pool;
|
|
6
|
+
private _acquireMaxRetries;
|
|
7
|
+
private _acquireRetryWait;
|
|
8
|
+
private _acquireTimeoutMillis;
|
|
9
|
+
private _fifo;
|
|
10
|
+
private _idleTimeoutMillis;
|
|
11
|
+
private _houseKeepInterval;
|
|
12
|
+
private _min;
|
|
13
|
+
private _minIdle;
|
|
14
|
+
private _max;
|
|
15
|
+
private _maxQueue;
|
|
16
|
+
private _validation;
|
|
17
|
+
constructor(pool: Pool);
|
|
18
|
+
get acquireMaxRetries(): number;
|
|
19
|
+
set acquireMaxRetries(val: number);
|
|
20
|
+
get acquireRetryWait(): number;
|
|
21
|
+
set acquireRetryWait(val: number);
|
|
22
|
+
get acquireTimeoutMillis(): number;
|
|
23
|
+
set acquireTimeoutMillis(val: number);
|
|
24
|
+
get fifo(): boolean;
|
|
25
|
+
set fifo(val: boolean);
|
|
26
|
+
get idleTimeoutMillis(): number;
|
|
27
|
+
set idleTimeoutMillis(val: number);
|
|
28
|
+
get houseKeepInterval(): number;
|
|
29
|
+
set houseKeepInterval(val: number);
|
|
30
|
+
get min(): number;
|
|
31
|
+
set min(val: number);
|
|
32
|
+
get minIdle(): number;
|
|
33
|
+
set minIdle(val: number);
|
|
34
|
+
get max(): number;
|
|
35
|
+
set max(val: number);
|
|
36
|
+
get maxQueue(): number;
|
|
37
|
+
set maxQueue(val: number);
|
|
38
|
+
get validation(): boolean;
|
|
39
|
+
set validation(val: boolean);
|
|
40
|
+
assign(values: PoolConfiguration | PoolOptions): void;
|
|
41
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Callback } from './definitions.js';
|
|
2
|
+
import { Pool } from './pool.js';
|
|
3
|
+
export declare class PoolRequest {
|
|
4
|
+
created: number;
|
|
5
|
+
callback: Callback;
|
|
6
|
+
options?: any;
|
|
7
|
+
timeoutHandle: any;
|
|
8
|
+
timedOut: boolean;
|
|
9
|
+
constructor(pool: Pool, callback?: Callback, options?: any);
|
|
10
|
+
stopTimout(): void;
|
|
11
|
+
}
|
package/types/pool.d.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Callback, PoolConfiguration, PoolFactory, PoolState } from './definitions.js';
|
|
3
|
+
import { PoolOptions } from './pool-options.js';
|
|
4
|
+
export declare class Pool<T = any> extends EventEmitter {
|
|
5
|
+
private readonly _options;
|
|
6
|
+
private readonly _factory;
|
|
7
|
+
private _requestQueue;
|
|
8
|
+
private _allResources;
|
|
9
|
+
private _acquiredResources;
|
|
10
|
+
private _idleResources;
|
|
11
|
+
private _creating;
|
|
12
|
+
private _requestsProcessing;
|
|
13
|
+
private _state;
|
|
14
|
+
private _houseKeepTimer;
|
|
15
|
+
private _closeWaitTimer;
|
|
16
|
+
constructor(factory: PoolFactory<T>, config?: PoolConfiguration);
|
|
17
|
+
/**
|
|
18
|
+
* Returns Pool options
|
|
19
|
+
*/
|
|
20
|
+
get options(): PoolOptions;
|
|
21
|
+
/**
|
|
22
|
+
* Returns number of resources that are currently acquired
|
|
23
|
+
*/
|
|
24
|
+
get acquired(): number;
|
|
25
|
+
/**
|
|
26
|
+
* Returns number of unused resources in the pool
|
|
27
|
+
*/
|
|
28
|
+
get available(): number;
|
|
29
|
+
/**
|
|
30
|
+
* Returns number of resources currently creating
|
|
31
|
+
*/
|
|
32
|
+
get creating(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Returns number of callers waiting to acquire a resource
|
|
35
|
+
*/
|
|
36
|
+
get pending(): number;
|
|
37
|
+
/**
|
|
38
|
+
* Returns number of resources in the pool
|
|
39
|
+
* regardless of whether they are idle or in use
|
|
40
|
+
*/
|
|
41
|
+
get size(): number;
|
|
42
|
+
/**
|
|
43
|
+
* Returns state of the pool
|
|
44
|
+
*/
|
|
45
|
+
get state(): PoolState;
|
|
46
|
+
/**
|
|
47
|
+
* Starts the pool and begins creating of resources, starts house keeping and any other internal logic.
|
|
48
|
+
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
49
|
+
*/
|
|
50
|
+
start(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Shuts down the pool and destroys all resources.
|
|
53
|
+
*/
|
|
54
|
+
close(callback?: Callback): void;
|
|
55
|
+
close(terminateWait: number, callback?: Callback): void;
|
|
56
|
+
close(force: boolean, callback?: Callback): void;
|
|
57
|
+
closeAsync(): Promise<void>;
|
|
58
|
+
closeAsync(terminateWait: number): Promise<void>;
|
|
59
|
+
closeAsync(force: boolean): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Acquires `resource` from the pool or create a new one
|
|
62
|
+
*/
|
|
63
|
+
acquire(): Promise<T>;
|
|
64
|
+
acquire(callback: Callback): void;
|
|
65
|
+
/**
|
|
66
|
+
* Releases an allocated `resource` and let it back to pool.
|
|
67
|
+
*/
|
|
68
|
+
release(resource: T, callback?: Callback): void;
|
|
69
|
+
/**
|
|
70
|
+
* Async version of release().
|
|
71
|
+
*/
|
|
72
|
+
releaseAsync(resource: T): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Releases, destroys and removes any `resource` from `Pool`.
|
|
75
|
+
*/
|
|
76
|
+
destroy(resource: T, callback?: Callback): any;
|
|
77
|
+
/**
|
|
78
|
+
* Async version of destroy().
|
|
79
|
+
*/
|
|
80
|
+
destroyAsync(resource: T): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
83
|
+
*/
|
|
84
|
+
isAcquired(resource: T): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Returns if the pool contains a `resource`
|
|
87
|
+
*/
|
|
88
|
+
includes(resource: T): boolean;
|
|
89
|
+
private _processNextRequest;
|
|
90
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Creates new resource
|
|
93
|
+
*/
|
|
94
|
+
private _createResource;
|
|
95
|
+
private _setHouseKeep;
|
|
96
|
+
private _houseKeep;
|
|
97
|
+
private _ensureMin;
|
|
98
|
+
private _itemSetAcquired;
|
|
99
|
+
private _itemDetach;
|
|
100
|
+
private _itemSetIdle;
|
|
101
|
+
private _itemDestroy;
|
|
102
|
+
private _itemValidate;
|
|
103
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DoublyLinked } from 'doublylinked';
|
|
2
|
+
import { ResourceState } from './definitions.js';
|
|
3
|
+
export declare class ResourceItem<T> {
|
|
4
|
+
state: ResourceState;
|
|
5
|
+
acquiredNode?: DoublyLinked.Node;
|
|
6
|
+
idleNode?: DoublyLinked.Node;
|
|
7
|
+
resource: T;
|
|
8
|
+
idleTime: number;
|
|
9
|
+
destroyed: boolean;
|
|
10
|
+
constructor(resource: T);
|
|
11
|
+
}
|
package/cjs/package.json
DELETED