lightning-pool 4.2.1 → 4.3.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/README.md +7 -1
- package/cjs/definitions.js +2 -2
- package/cjs/index.js +5 -19
- package/cjs/pool-options.js +3 -2
- package/cjs/pool-request.js +3 -3
- package/cjs/pool.js +55 -33
- package/esm/index.js +1 -1
- package/esm/pool-options.js +3 -2
- package/esm/pool-request.js +3 -3
- package/esm/pool.js +53 -29
- package/package.json +8 -53
- package/esm/abort-error.d.ts +0 -2
- package/esm/definitions.d.ts +0 -34
- package/esm/index.d.ts +0 -6
- package/esm/pool-options.d.ts +0 -42
- package/esm/pool-request.d.ts +0 -10
- package/esm/pool.d.ts +0 -104
- package/esm/resource-item.d.ts +0 -11
package/README.md
CHANGED
|
@@ -121,7 +121,13 @@ Acquires a `resource` from the `Pool` or create a new one.
|
|
|
121
121
|
|
|
122
122
|
##### Usage
|
|
123
123
|
|
|
124
|
-
`pool.acquire()
|
|
124
|
+
`pool.acquire(): Promise<any>`
|
|
125
|
+
|
|
126
|
+
`pool.acquire(factoryCreateOptions: any): Promise<any>`
|
|
127
|
+
|
|
128
|
+
`pool.acquire(callback:Callback): Promise<any>`
|
|
129
|
+
|
|
130
|
+
`pool.acquire(factoryCreateOptions?: any, callback:Callback): Promise<any>`
|
|
125
131
|
|
|
126
132
|
- *Returns*: A Promise
|
|
127
133
|
|
package/cjs/definitions.js
CHANGED
|
@@ -7,10 +7,10 @@ var PoolState;
|
|
|
7
7
|
PoolState[PoolState["STARTED"] = 1] = "STARTED";
|
|
8
8
|
PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
|
|
9
9
|
PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
|
|
10
|
-
})(PoolState
|
|
10
|
+
})(PoolState || (exports.PoolState = PoolState = {}));
|
|
11
11
|
var ResourceState;
|
|
12
12
|
(function (ResourceState) {
|
|
13
13
|
ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
|
|
14
14
|
ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
|
|
15
15
|
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
|
|
16
|
-
})(ResourceState
|
|
16
|
+
})(ResourceState || (exports.ResourceState = ResourceState = {}));
|
package/cjs/index.js
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.createPool =
|
|
3
|
+
exports.createPool = createPool;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
18
5
|
const pool_js_1 = require("./pool.js");
|
|
19
|
-
__exportStar(require("./
|
|
20
|
-
__exportStar(require("./
|
|
21
|
-
__exportStar(require("./
|
|
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);
|
|
22
9
|
function createPool(factory, config) {
|
|
23
10
|
return new pool_js_1.Pool(factory, config);
|
|
24
11
|
}
|
|
25
|
-
exports.createPool = createPool;
|
package/cjs/pool-options.js
CHANGED
|
@@ -13,7 +13,7 @@ const defaultValues = {
|
|
|
13
13
|
minIdle: 0,
|
|
14
14
|
max: 10,
|
|
15
15
|
maxQueue: 1000,
|
|
16
|
-
validation: true
|
|
16
|
+
validation: true,
|
|
17
17
|
};
|
|
18
18
|
class PoolOptions extends events_1.EventEmitter {
|
|
19
19
|
constructor(pool) {
|
|
@@ -50,7 +50,8 @@ class PoolOptions extends events_1.EventEmitter {
|
|
|
50
50
|
return this._acquireTimeoutMillis;
|
|
51
51
|
}
|
|
52
52
|
set acquireTimeoutMillis(val) {
|
|
53
|
-
this._acquireTimeoutMillis =
|
|
53
|
+
this._acquireTimeoutMillis =
|
|
54
|
+
val >= 0 ? val : defaultValues.acquireTimeoutMillis;
|
|
54
55
|
this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis);
|
|
55
56
|
}
|
|
56
57
|
get fifo() {
|
package/cjs/pool-request.js
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PoolRequest = void 0;
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
5
|
-
function noop() {
|
|
6
|
-
}
|
|
5
|
+
function noop() { }
|
|
7
6
|
class PoolRequest {
|
|
8
|
-
constructor(pool, callback) {
|
|
7
|
+
constructor(pool, callback, options) {
|
|
9
8
|
this.timedOut = false;
|
|
10
9
|
this.created = Date.now();
|
|
11
10
|
this.callback = callback || noop;
|
|
11
|
+
this.options = options;
|
|
12
12
|
if (pool.options.acquireTimeoutMillis) {
|
|
13
13
|
this.timeoutHandle = setTimeout(() => {
|
|
14
14
|
this.timedOut = true;
|
package/cjs/pool.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Pool = void 0;
|
|
7
|
-
const
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const doublylinked_1 = tslib_1.__importDefault(require("doublylinked"));
|
|
8
6
|
const events_1 = require("events");
|
|
9
|
-
const putil_promisify_1 = __importDefault(require("putil-promisify"));
|
|
7
|
+
const putil_promisify_1 = tslib_1.__importDefault(require("putil-promisify"));
|
|
10
8
|
const abort_error_js_1 = require("./abort-error.js");
|
|
11
9
|
const definitions_js_1 = require("./definitions.js");
|
|
12
10
|
const pool_options_js_1 = require("./pool-options.js");
|
|
@@ -22,17 +20,22 @@ class Pool extends events_1.EventEmitter {
|
|
|
22
20
|
this._creating = 0;
|
|
23
21
|
this._requestsProcessing = 0;
|
|
24
22
|
this._state = definitions_js_1.PoolState.IDLE;
|
|
25
|
-
if (typeof factory !== 'object')
|
|
23
|
+
if (typeof factory !== 'object') {
|
|
26
24
|
throw new TypeError('You must provide `factory` object');
|
|
27
|
-
|
|
25
|
+
}
|
|
26
|
+
if (typeof factory.create !== 'function') {
|
|
28
27
|
throw new TypeError('factory.create must be a function');
|
|
29
|
-
|
|
28
|
+
}
|
|
29
|
+
if (typeof factory.destroy !== 'function') {
|
|
30
30
|
throw new TypeError('factory.destroy must be a function');
|
|
31
|
-
|
|
31
|
+
}
|
|
32
|
+
if (factory.validate && typeof factory.validate !== 'function') {
|
|
32
33
|
throw new TypeError('factory.validate can be a function');
|
|
33
|
-
|
|
34
|
+
}
|
|
35
|
+
if (factory.reset && typeof factory.reset !== 'function') {
|
|
34
36
|
throw new TypeError('factory.reset can be a function');
|
|
35
|
-
|
|
37
|
+
}
|
|
38
|
+
const opts = (this._options = new pool_options_js_1.PoolOptions(this));
|
|
36
39
|
opts.on('change', (prop, val) => {
|
|
37
40
|
if (prop === 'houseKeepInterval')
|
|
38
41
|
this._setHouseKeep(val);
|
|
@@ -93,8 +96,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
93
96
|
start() {
|
|
94
97
|
if (this._state === definitions_js_1.PoolState.STARTED)
|
|
95
98
|
return;
|
|
96
|
-
if (this._state >= definitions_js_1.PoolState.CLOSING)
|
|
99
|
+
if (this._state >= definitions_js_1.PoolState.CLOSING) {
|
|
97
100
|
throw new Error('Closed pool can not be started again');
|
|
101
|
+
}
|
|
98
102
|
this._state = definitions_js_1.PoolState.STARTED;
|
|
99
103
|
this._setHouseKeep(this.options.houseKeepInterval);
|
|
100
104
|
this._ensureMin();
|
|
@@ -106,14 +110,15 @@ class Pool extends events_1.EventEmitter {
|
|
|
106
110
|
if (typeof arg0 === 'function')
|
|
107
111
|
callback = arg0;
|
|
108
112
|
else {
|
|
109
|
-
terminateWait = typeof arg0 === 'number' ? arg0 :
|
|
110
|
-
(!!arg0 ? 0 : Infinity);
|
|
113
|
+
terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity;
|
|
111
114
|
callback = arg1;
|
|
112
115
|
}
|
|
113
|
-
if (!callback)
|
|
116
|
+
if (!callback) {
|
|
114
117
|
return putil_promisify_1.default.fromCallback(cb => this.close(terminateWait, cb));
|
|
115
|
-
|
|
118
|
+
}
|
|
119
|
+
if (this._state === definitions_js_1.PoolState.CLOSED || this._state === definitions_js_1.PoolState.IDLE) {
|
|
116
120
|
return callback();
|
|
121
|
+
}
|
|
117
122
|
if (this._state === definitions_js_1.PoolState.CLOSING) {
|
|
118
123
|
this.once('close', callback);
|
|
119
124
|
return;
|
|
@@ -150,19 +155,29 @@ class Pool extends events_1.EventEmitter {
|
|
|
150
155
|
closeAsync(arg0) {
|
|
151
156
|
return putil_promisify_1.default.fromCallback(cb => this.close(arg0, cb));
|
|
152
157
|
}
|
|
153
|
-
acquire(arg0) {
|
|
154
|
-
|
|
158
|
+
acquire(arg0, arg1) {
|
|
159
|
+
let callback;
|
|
160
|
+
let factoryCreateOptions;
|
|
161
|
+
if (typeof arg0 === 'function') {
|
|
162
|
+
callback = arg0;
|
|
163
|
+
}
|
|
164
|
+
else if (typeof arg0 === 'object') {
|
|
165
|
+
factoryCreateOptions = arg0;
|
|
166
|
+
if (typeof arg1 === 'function')
|
|
167
|
+
callback = arg1;
|
|
168
|
+
}
|
|
169
|
+
if (!callback)
|
|
155
170
|
return putil_promisify_1.default.fromCallback(cb => this.acquire(cb));
|
|
156
|
-
const callback = arg0;
|
|
157
171
|
try {
|
|
158
172
|
this.start();
|
|
159
173
|
}
|
|
160
174
|
catch (e) {
|
|
161
175
|
return callback(e);
|
|
162
176
|
}
|
|
163
|
-
if (this.options.maxQueue && this.pending >= this.options.maxQueue)
|
|
177
|
+
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
164
178
|
return callback(new Error('Pool queue is full'));
|
|
165
|
-
|
|
179
|
+
}
|
|
180
|
+
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback, factoryCreateOptions));
|
|
166
181
|
this._processNextRequest();
|
|
167
182
|
}
|
|
168
183
|
/**
|
|
@@ -170,8 +185,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
170
185
|
*/
|
|
171
186
|
release(resource, callback) {
|
|
172
187
|
const item = this._allResources.get(resource);
|
|
173
|
-
if (item && item.state !== definitions_js_1.ResourceState.IDLE)
|
|
188
|
+
if (item && item.state !== definitions_js_1.ResourceState.IDLE) {
|
|
174
189
|
this._itemSetIdle(item, callback);
|
|
190
|
+
}
|
|
175
191
|
this._processNextRequest();
|
|
176
192
|
}
|
|
177
193
|
/**
|
|
@@ -216,8 +232,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
216
232
|
}
|
|
217
233
|
_processNextRequest() {
|
|
218
234
|
if (this._state !== definitions_js_1.PoolState.STARTED ||
|
|
219
|
-
this._requestsProcessing >= this.options.max - this.acquired)
|
|
235
|
+
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
220
236
|
return;
|
|
237
|
+
}
|
|
221
238
|
const request = this._requestQueue.shift();
|
|
222
239
|
if (!request)
|
|
223
240
|
return;
|
|
@@ -292,7 +309,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
292
309
|
this.emit('error', err, {
|
|
293
310
|
requestTime: request ? request.created : Date.now(),
|
|
294
311
|
tries,
|
|
295
|
-
maxRetries: this.options.acquireMaxRetries
|
|
312
|
+
maxRetries: this.options.acquireMaxRetries,
|
|
296
313
|
});
|
|
297
314
|
if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) {
|
|
298
315
|
this._creating--;
|
|
@@ -301,9 +318,10 @@ class Pool extends events_1.EventEmitter {
|
|
|
301
318
|
return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
|
|
302
319
|
}
|
|
303
320
|
this._creating--;
|
|
304
|
-
if (this._allResources.has(obj))
|
|
305
|
-
return callback &&
|
|
306
|
-
callback(new Error('Factory error. Resource already in pool'));
|
|
321
|
+
if (this._allResources.has(obj)) {
|
|
322
|
+
return (callback &&
|
|
323
|
+
callback(new Error('Factory error. Resource already in pool')));
|
|
324
|
+
}
|
|
307
325
|
const item = new resource_item_js_1.ResourceItem(obj);
|
|
308
326
|
this._itemSetIdle(item);
|
|
309
327
|
this._allResources.set(obj, item);
|
|
@@ -313,10 +331,11 @@ class Pool extends events_1.EventEmitter {
|
|
|
313
331
|
};
|
|
314
332
|
const tryCreate = () => {
|
|
315
333
|
try {
|
|
316
|
-
const o = this._factory.create({ tries, maxRetries });
|
|
334
|
+
const o = this._factory.create({ tries, maxRetries }, request?.options);
|
|
317
335
|
/* istanbul ignore next */
|
|
318
|
-
if (!o)
|
|
336
|
+
if (!o) {
|
|
319
337
|
return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource'));
|
|
338
|
+
}
|
|
320
339
|
putil_promisify_1.default.await(o, handleCallback);
|
|
321
340
|
}
|
|
322
341
|
catch (e) {
|
|
@@ -329,8 +348,10 @@ class Pool extends events_1.EventEmitter {
|
|
|
329
348
|
if (this._houseKeepTimer)
|
|
330
349
|
clearInterval(this._houseKeepTimer);
|
|
331
350
|
this._houseKeepTimer = undefined;
|
|
332
|
-
if (ms > 0 && this.state === definitions_js_1.PoolState.STARTED ||
|
|
351
|
+
if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) ||
|
|
352
|
+
this.state === definitions_js_1.PoolState.CLOSING) {
|
|
333
353
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
354
|
+
}
|
|
334
355
|
}
|
|
335
356
|
_houseKeep() {
|
|
336
357
|
const isClosing = this._state === definitions_js_1.PoolState.CLOSING;
|
|
@@ -339,10 +360,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
339
360
|
let n = this._idleResources.length - this.options.minIdle;
|
|
340
361
|
if (isClosing || (m > 0 && n > 0)) {
|
|
341
362
|
this._idleResources.every((item) => {
|
|
342
|
-
if (isClosing ||
|
|
343
|
-
item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
363
|
+
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
344
364
|
this._itemDestroy(item);
|
|
345
|
-
return isClosing || !!(
|
|
365
|
+
return isClosing || !!(--n && --m);
|
|
346
366
|
}
|
|
347
367
|
return false;
|
|
348
368
|
});
|
|
@@ -388,6 +408,8 @@ class Pool extends events_1.EventEmitter {
|
|
|
388
408
|
item.acquiredNode.remove();
|
|
389
409
|
item.acquiredNode = undefined;
|
|
390
410
|
break;
|
|
411
|
+
default:
|
|
412
|
+
break;
|
|
391
413
|
}
|
|
392
414
|
}
|
|
393
415
|
_itemSetIdle(item, callback) {
|
package/esm/index.js
CHANGED
package/esm/pool-options.js
CHANGED
|
@@ -10,7 +10,7 @@ const defaultValues = {
|
|
|
10
10
|
minIdle: 0,
|
|
11
11
|
max: 10,
|
|
12
12
|
maxQueue: 1000,
|
|
13
|
-
validation: true
|
|
13
|
+
validation: true,
|
|
14
14
|
};
|
|
15
15
|
export class PoolOptions extends EventEmitter {
|
|
16
16
|
constructor(pool) {
|
|
@@ -47,7 +47,8 @@ export class PoolOptions extends EventEmitter {
|
|
|
47
47
|
return this._acquireTimeoutMillis;
|
|
48
48
|
}
|
|
49
49
|
set acquireTimeoutMillis(val) {
|
|
50
|
-
this._acquireTimeoutMillis =
|
|
50
|
+
this._acquireTimeoutMillis =
|
|
51
|
+
val >= 0 ? val : defaultValues.acquireTimeoutMillis;
|
|
51
52
|
this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis);
|
|
52
53
|
}
|
|
53
54
|
get fifo() {
|
package/esm/pool-request.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
2
|
-
function noop() {
|
|
3
|
-
}
|
|
2
|
+
function noop() { }
|
|
4
3
|
export class PoolRequest {
|
|
5
|
-
constructor(pool, callback) {
|
|
4
|
+
constructor(pool, callback, options) {
|
|
6
5
|
this.timedOut = false;
|
|
7
6
|
this.created = Date.now();
|
|
8
7
|
this.callback = callback || noop;
|
|
8
|
+
this.options = options;
|
|
9
9
|
if (pool.options.acquireTimeoutMillis) {
|
|
10
10
|
this.timeoutHandle = setTimeout(() => {
|
|
11
11
|
this.timedOut = true;
|
package/esm/pool.js
CHANGED
|
@@ -2,7 +2,7 @@ import DoublyLinked from 'doublylinked';
|
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
3
|
import promisify from 'putil-promisify';
|
|
4
4
|
import { AbortError } from './abort-error.js';
|
|
5
|
-
import { PoolState, ResourceState } from './definitions.js';
|
|
5
|
+
import { PoolState, ResourceState, } from './definitions.js';
|
|
6
6
|
import { PoolOptions } from './pool-options.js';
|
|
7
7
|
import { PoolRequest } from './pool-request.js';
|
|
8
8
|
import { ResourceItem } from './resource-item.js';
|
|
@@ -16,17 +16,22 @@ export class Pool extends EventEmitter {
|
|
|
16
16
|
this._creating = 0;
|
|
17
17
|
this._requestsProcessing = 0;
|
|
18
18
|
this._state = PoolState.IDLE;
|
|
19
|
-
if (typeof factory !== 'object')
|
|
19
|
+
if (typeof factory !== 'object') {
|
|
20
20
|
throw new TypeError('You must provide `factory` object');
|
|
21
|
-
|
|
21
|
+
}
|
|
22
|
+
if (typeof factory.create !== 'function') {
|
|
22
23
|
throw new TypeError('factory.create must be a function');
|
|
23
|
-
|
|
24
|
+
}
|
|
25
|
+
if (typeof factory.destroy !== 'function') {
|
|
24
26
|
throw new TypeError('factory.destroy must be a function');
|
|
25
|
-
|
|
27
|
+
}
|
|
28
|
+
if (factory.validate && typeof factory.validate !== 'function') {
|
|
26
29
|
throw new TypeError('factory.validate can be a function');
|
|
27
|
-
|
|
30
|
+
}
|
|
31
|
+
if (factory.reset && typeof factory.reset !== 'function') {
|
|
28
32
|
throw new TypeError('factory.reset can be a function');
|
|
29
|
-
|
|
33
|
+
}
|
|
34
|
+
const opts = (this._options = new PoolOptions(this));
|
|
30
35
|
opts.on('change', (prop, val) => {
|
|
31
36
|
if (prop === 'houseKeepInterval')
|
|
32
37
|
this._setHouseKeep(val);
|
|
@@ -87,8 +92,9 @@ export class Pool extends EventEmitter {
|
|
|
87
92
|
start() {
|
|
88
93
|
if (this._state === PoolState.STARTED)
|
|
89
94
|
return;
|
|
90
|
-
if (this._state >= PoolState.CLOSING)
|
|
95
|
+
if (this._state >= PoolState.CLOSING) {
|
|
91
96
|
throw new Error('Closed pool can not be started again');
|
|
97
|
+
}
|
|
92
98
|
this._state = PoolState.STARTED;
|
|
93
99
|
this._setHouseKeep(this.options.houseKeepInterval);
|
|
94
100
|
this._ensureMin();
|
|
@@ -100,14 +106,15 @@ export class Pool extends EventEmitter {
|
|
|
100
106
|
if (typeof arg0 === 'function')
|
|
101
107
|
callback = arg0;
|
|
102
108
|
else {
|
|
103
|
-
terminateWait = typeof arg0 === 'number' ? arg0 :
|
|
104
|
-
(!!arg0 ? 0 : Infinity);
|
|
109
|
+
terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity;
|
|
105
110
|
callback = arg1;
|
|
106
111
|
}
|
|
107
|
-
if (!callback)
|
|
112
|
+
if (!callback) {
|
|
108
113
|
return promisify.fromCallback(cb => this.close(terminateWait, cb));
|
|
109
|
-
|
|
114
|
+
}
|
|
115
|
+
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) {
|
|
110
116
|
return callback();
|
|
117
|
+
}
|
|
111
118
|
if (this._state === PoolState.CLOSING) {
|
|
112
119
|
this.once('close', callback);
|
|
113
120
|
return;
|
|
@@ -144,19 +151,29 @@ export class Pool extends EventEmitter {
|
|
|
144
151
|
closeAsync(arg0) {
|
|
145
152
|
return promisify.fromCallback(cb => this.close(arg0, cb));
|
|
146
153
|
}
|
|
147
|
-
acquire(arg0) {
|
|
148
|
-
|
|
154
|
+
acquire(arg0, arg1) {
|
|
155
|
+
let callback;
|
|
156
|
+
let factoryCreateOptions;
|
|
157
|
+
if (typeof arg0 === 'function') {
|
|
158
|
+
callback = arg0;
|
|
159
|
+
}
|
|
160
|
+
else if (typeof arg0 === 'object') {
|
|
161
|
+
factoryCreateOptions = arg0;
|
|
162
|
+
if (typeof arg1 === 'function')
|
|
163
|
+
callback = arg1;
|
|
164
|
+
}
|
|
165
|
+
if (!callback)
|
|
149
166
|
return promisify.fromCallback(cb => this.acquire(cb));
|
|
150
|
-
const callback = arg0;
|
|
151
167
|
try {
|
|
152
168
|
this.start();
|
|
153
169
|
}
|
|
154
170
|
catch (e) {
|
|
155
171
|
return callback(e);
|
|
156
172
|
}
|
|
157
|
-
if (this.options.maxQueue && this.pending >= this.options.maxQueue)
|
|
173
|
+
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
158
174
|
return callback(new Error('Pool queue is full'));
|
|
159
|
-
|
|
175
|
+
}
|
|
176
|
+
this._requestQueue.push(new PoolRequest(this, callback, factoryCreateOptions));
|
|
160
177
|
this._processNextRequest();
|
|
161
178
|
}
|
|
162
179
|
/**
|
|
@@ -164,8 +181,9 @@ export class Pool extends EventEmitter {
|
|
|
164
181
|
*/
|
|
165
182
|
release(resource, callback) {
|
|
166
183
|
const item = this._allResources.get(resource);
|
|
167
|
-
if (item && item.state !== ResourceState.IDLE)
|
|
184
|
+
if (item && item.state !== ResourceState.IDLE) {
|
|
168
185
|
this._itemSetIdle(item, callback);
|
|
186
|
+
}
|
|
169
187
|
this._processNextRequest();
|
|
170
188
|
}
|
|
171
189
|
/**
|
|
@@ -210,8 +228,9 @@ export class Pool extends EventEmitter {
|
|
|
210
228
|
}
|
|
211
229
|
_processNextRequest() {
|
|
212
230
|
if (this._state !== PoolState.STARTED ||
|
|
213
|
-
this._requestsProcessing >= this.options.max - this.acquired)
|
|
231
|
+
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
214
232
|
return;
|
|
233
|
+
}
|
|
215
234
|
const request = this._requestQueue.shift();
|
|
216
235
|
if (!request)
|
|
217
236
|
return;
|
|
@@ -286,7 +305,7 @@ export class Pool extends EventEmitter {
|
|
|
286
305
|
this.emit('error', err, {
|
|
287
306
|
requestTime: request ? request.created : Date.now(),
|
|
288
307
|
tries,
|
|
289
|
-
maxRetries: this.options.acquireMaxRetries
|
|
308
|
+
maxRetries: this.options.acquireMaxRetries,
|
|
290
309
|
});
|
|
291
310
|
if (err instanceof AbortError || tries >= maxRetries) {
|
|
292
311
|
this._creating--;
|
|
@@ -295,9 +314,10 @@ export class Pool extends EventEmitter {
|
|
|
295
314
|
return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
|
|
296
315
|
}
|
|
297
316
|
this._creating--;
|
|
298
|
-
if (this._allResources.has(obj))
|
|
299
|
-
return callback &&
|
|
300
|
-
callback(new Error('Factory error. Resource already in pool'));
|
|
317
|
+
if (this._allResources.has(obj)) {
|
|
318
|
+
return (callback &&
|
|
319
|
+
callback(new Error('Factory error. Resource already in pool')));
|
|
320
|
+
}
|
|
301
321
|
const item = new ResourceItem(obj);
|
|
302
322
|
this._itemSetIdle(item);
|
|
303
323
|
this._allResources.set(obj, item);
|
|
@@ -307,10 +327,11 @@ export class Pool extends EventEmitter {
|
|
|
307
327
|
};
|
|
308
328
|
const tryCreate = () => {
|
|
309
329
|
try {
|
|
310
|
-
const o = this._factory.create({ tries, maxRetries });
|
|
330
|
+
const o = this._factory.create({ tries, maxRetries }, request?.options);
|
|
311
331
|
/* istanbul ignore next */
|
|
312
|
-
if (!o)
|
|
332
|
+
if (!o) {
|
|
313
333
|
return handleCallback(new AbortError('Factory returned no resource'));
|
|
334
|
+
}
|
|
314
335
|
promisify.await(o, handleCallback);
|
|
315
336
|
}
|
|
316
337
|
catch (e) {
|
|
@@ -323,8 +344,10 @@ export class Pool extends EventEmitter {
|
|
|
323
344
|
if (this._houseKeepTimer)
|
|
324
345
|
clearInterval(this._houseKeepTimer);
|
|
325
346
|
this._houseKeepTimer = undefined;
|
|
326
|
-
if (ms > 0 && this.state === PoolState.STARTED ||
|
|
347
|
+
if ((ms > 0 && this.state === PoolState.STARTED) ||
|
|
348
|
+
this.state === PoolState.CLOSING) {
|
|
327
349
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
350
|
+
}
|
|
328
351
|
}
|
|
329
352
|
_houseKeep() {
|
|
330
353
|
const isClosing = this._state === PoolState.CLOSING;
|
|
@@ -333,10 +356,9 @@ export class Pool extends EventEmitter {
|
|
|
333
356
|
let n = this._idleResources.length - this.options.minIdle;
|
|
334
357
|
if (isClosing || (m > 0 && n > 0)) {
|
|
335
358
|
this._idleResources.every((item) => {
|
|
336
|
-
if (isClosing ||
|
|
337
|
-
item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
359
|
+
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
338
360
|
this._itemDestroy(item);
|
|
339
|
-
return isClosing || !!(
|
|
361
|
+
return isClosing || !!(--n && --m);
|
|
340
362
|
}
|
|
341
363
|
return false;
|
|
342
364
|
});
|
|
@@ -382,6 +404,8 @@ export class Pool extends EventEmitter {
|
|
|
382
404
|
item.acquiredNode.remove();
|
|
383
405
|
item.acquiredNode = undefined;
|
|
384
406
|
break;
|
|
407
|
+
default:
|
|
408
|
+
break;
|
|
385
409
|
}
|
|
386
410
|
}
|
|
387
411
|
_itemSetIdle(item, callback) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lightning-pool",
|
|
3
3
|
"description": "Fastest generic Pool written with TypeScript",
|
|
4
|
-
"version": "4.
|
|
4
|
+
"version": "4.3.0",
|
|
5
5
|
"author": "Panates Ltd.",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"Eray Hanoglu <e.hanoglu@panates.com>"
|
|
@@ -14,62 +14,17 @@
|
|
|
14
14
|
"generic-pool"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"doublylinked": "^2.5.
|
|
18
|
-
"putil-promisify": "^1.
|
|
19
|
-
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@babel/eslint-parser": "^7.19.1",
|
|
22
|
-
"@types/jest": "^29.2.5",
|
|
23
|
-
"@types/node": "^18.11.18",
|
|
24
|
-
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
25
|
-
"@typescript-eslint/parser": "^5.48.0",
|
|
26
|
-
"dotenv": "^16.0.3",
|
|
27
|
-
"eslint": "^8.31.0",
|
|
28
|
-
"eslint-config-google": "^0.14.0",
|
|
29
|
-
"eslint-plugin-import": "^2.26.0",
|
|
30
|
-
"eslint-plugin-security": "^1.5.0",
|
|
31
|
-
"eslint-plugin-simple-import-sort": "^8.0.0",
|
|
32
|
-
"eslint-plugin-unused-imports": "^2.0.0",
|
|
33
|
-
"jest": "^29.3.1",
|
|
34
|
-
"npm-run-path": "^5.1.0",
|
|
35
|
-
"ts-cleanup": "^0.2.6",
|
|
36
|
-
"ts-jest": "^29.0.3",
|
|
37
|
-
"ts-loader": "^9.4.2",
|
|
38
|
-
"ts-node": "^10.9.1",
|
|
39
|
-
"tsconfig-paths": "^4.1.2",
|
|
40
|
-
"typescript": "^4.9.4"
|
|
17
|
+
"doublylinked": "^2.5.4",
|
|
18
|
+
"putil-promisify": "^1.10.1"
|
|
41
19
|
},
|
|
42
20
|
"type": "module",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
"require": "./cjs/index.js",
|
|
47
|
-
"default": "./esm/index.js"
|
|
48
|
-
},
|
|
49
|
-
"./cjs": "./cjs/index.js",
|
|
50
|
-
"./esm": "./esm/index.js"
|
|
51
|
-
},
|
|
21
|
+
"main": "./cjs/index.js",
|
|
22
|
+
"module": "./esm/index.js",
|
|
23
|
+
"types": "./esm/index.d.ts",
|
|
52
24
|
"files": [
|
|
53
|
-
"bin/",
|
|
54
25
|
"cjs/",
|
|
55
26
|
"esm/",
|
|
56
27
|
"LICENSE",
|
|
57
28
|
"README.md"
|
|
58
|
-
]
|
|
59
|
-
|
|
60
|
-
"lint": "eslint .",
|
|
61
|
-
"clean": "npm run clean:src && npm run clean:dist",
|
|
62
|
-
"clean:dist": "rimraf cjs esm coverage",
|
|
63
|
-
"clean:src": "ts-cleanup -s src --all | ts-cleanup -s test",
|
|
64
|
-
"prebuild": "npm run clean:dist && npm run lint",
|
|
65
|
-
"build": "npm run build:cjs && npm run build:esm",
|
|
66
|
-
"build:cjs": "tsc -b tsconfig-build-cjs.json",
|
|
67
|
-
"build:esm": "tsc -b tsconfig-build-esm.json",
|
|
68
|
-
"postbuild": "cp package.cjs.json ./cjs/package.json",
|
|
69
|
-
"test": "jest --runInBand --detectOpenHandles",
|
|
70
|
-
"precover": "rimraf coverage",
|
|
71
|
-
"cover": "jest --maxWorkers=1 --coverage",
|
|
72
|
-
"precitest": "rimraf coverage",
|
|
73
|
-
"citest": "jest --coverage --coverageReporters=lcov"
|
|
74
|
-
}
|
|
75
|
-
}
|
|
29
|
+
]
|
|
30
|
+
}
|
package/esm/abort-error.d.ts
DELETED
package/esm/definitions.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export type Callback = (e?: Error, ...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/esm/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { PoolConfiguration, PoolFactory } from './definitions.js';
|
|
2
|
-
import { Pool } from './pool.js';
|
|
3
|
-
export * from './definitions.js';
|
|
4
|
-
export * from './pool.js';
|
|
5
|
-
export * from './abort-error.js';
|
|
6
|
-
export declare function createPool<T = any>(factory: PoolFactory<T>, config?: PoolConfiguration): Pool<T>;
|
package/esm/pool-options.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { EventEmitter } from 'events';
|
|
3
|
-
import { PoolConfiguration } from './definitions.js';
|
|
4
|
-
import { Pool } from './pool.js';
|
|
5
|
-
export declare class PoolOptions extends EventEmitter {
|
|
6
|
-
readonly pool: Pool;
|
|
7
|
-
private _acquireMaxRetries;
|
|
8
|
-
private _acquireRetryWait;
|
|
9
|
-
private _acquireTimeoutMillis;
|
|
10
|
-
private _fifo;
|
|
11
|
-
private _idleTimeoutMillis;
|
|
12
|
-
private _houseKeepInterval;
|
|
13
|
-
private _min;
|
|
14
|
-
private _minIdle;
|
|
15
|
-
private _max;
|
|
16
|
-
private _maxQueue;
|
|
17
|
-
private _validation;
|
|
18
|
-
constructor(pool: Pool);
|
|
19
|
-
get acquireMaxRetries(): number;
|
|
20
|
-
set acquireMaxRetries(val: number);
|
|
21
|
-
get acquireRetryWait(): number;
|
|
22
|
-
set acquireRetryWait(val: number);
|
|
23
|
-
get acquireTimeoutMillis(): number;
|
|
24
|
-
set acquireTimeoutMillis(val: number);
|
|
25
|
-
get fifo(): boolean;
|
|
26
|
-
set fifo(val: boolean);
|
|
27
|
-
get idleTimeoutMillis(): number;
|
|
28
|
-
set idleTimeoutMillis(val: number);
|
|
29
|
-
get houseKeepInterval(): number;
|
|
30
|
-
set houseKeepInterval(val: number);
|
|
31
|
-
get min(): number;
|
|
32
|
-
set min(val: number);
|
|
33
|
-
get minIdle(): number;
|
|
34
|
-
set minIdle(val: number);
|
|
35
|
-
get max(): number;
|
|
36
|
-
set max(val: number);
|
|
37
|
-
get maxQueue(): number;
|
|
38
|
-
set maxQueue(val: number);
|
|
39
|
-
get validation(): boolean;
|
|
40
|
-
set validation(val: boolean);
|
|
41
|
-
assign(values: PoolConfiguration | PoolOptions): void;
|
|
42
|
-
}
|
package/esm/pool-request.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Callback } from './definitions.js';
|
|
2
|
-
import { Pool } from './pool.js';
|
|
3
|
-
export declare class PoolRequest {
|
|
4
|
-
created: number;
|
|
5
|
-
callback: Callback;
|
|
6
|
-
timeoutHandle: any;
|
|
7
|
-
timedOut: boolean;
|
|
8
|
-
constructor(pool: Pool, callback?: Callback);
|
|
9
|
-
stopTimout(): void;
|
|
10
|
-
}
|
package/esm/pool.d.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { EventEmitter } from 'events';
|
|
3
|
-
import { Callback, PoolConfiguration, PoolFactory, PoolState } from './definitions.js';
|
|
4
|
-
import { PoolOptions } from './pool-options.js';
|
|
5
|
-
export declare class Pool<T = any> extends EventEmitter {
|
|
6
|
-
private readonly _options;
|
|
7
|
-
private readonly _factory;
|
|
8
|
-
private _requestQueue;
|
|
9
|
-
private _allResources;
|
|
10
|
-
private _acquiredResources;
|
|
11
|
-
private _idleResources;
|
|
12
|
-
private _creating;
|
|
13
|
-
private _requestsProcessing;
|
|
14
|
-
private _state;
|
|
15
|
-
private _houseKeepTimer;
|
|
16
|
-
private _closeWaitTimer;
|
|
17
|
-
constructor(factory: PoolFactory<T>, config?: PoolConfiguration);
|
|
18
|
-
/**
|
|
19
|
-
* Returns Pool options
|
|
20
|
-
*/
|
|
21
|
-
get options(): PoolOptions;
|
|
22
|
-
/**
|
|
23
|
-
* Returns number of resources that are currently acquired
|
|
24
|
-
*/
|
|
25
|
-
get acquired(): number;
|
|
26
|
-
/**
|
|
27
|
-
* Returns number of unused resources in the pool
|
|
28
|
-
*/
|
|
29
|
-
get available(): number;
|
|
30
|
-
/**
|
|
31
|
-
* Returns number of resources currently creating
|
|
32
|
-
*/
|
|
33
|
-
get creating(): number;
|
|
34
|
-
/**
|
|
35
|
-
* Returns number of callers waiting to acquire a resource
|
|
36
|
-
*/
|
|
37
|
-
get pending(): number;
|
|
38
|
-
/**
|
|
39
|
-
* Returns number of resources in the pool
|
|
40
|
-
* regardless of whether they are idle or in use
|
|
41
|
-
*/
|
|
42
|
-
get size(): number;
|
|
43
|
-
/**
|
|
44
|
-
* Returns state of the pool
|
|
45
|
-
*/
|
|
46
|
-
get state(): PoolState;
|
|
47
|
-
/**
|
|
48
|
-
* Starts the pool and begins creating of resources, starts house keeping and any other internal logic.
|
|
49
|
-
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
50
|
-
*/
|
|
51
|
-
start(): void;
|
|
52
|
-
/**
|
|
53
|
-
* Shuts down the pool and destroys all resources.
|
|
54
|
-
*/
|
|
55
|
-
close(callback?: Callback): void;
|
|
56
|
-
close(terminateWait: number, callback?: Callback): void;
|
|
57
|
-
close(force: boolean, callback?: Callback): void;
|
|
58
|
-
closeAsync(): Promise<void>;
|
|
59
|
-
closeAsync(terminateWait: number): Promise<void>;
|
|
60
|
-
closeAsync(force: boolean): Promise<void>;
|
|
61
|
-
/**
|
|
62
|
-
* Acquires `resource` from the pool or create a new one
|
|
63
|
-
*/
|
|
64
|
-
acquire(): Promise<T>;
|
|
65
|
-
acquire(callback: any): void;
|
|
66
|
-
/**
|
|
67
|
-
* Releases an allocated `resource` and let it back to pool.
|
|
68
|
-
*/
|
|
69
|
-
release(resource: T, callback?: Callback): void;
|
|
70
|
-
/**
|
|
71
|
-
* Async version of release().
|
|
72
|
-
*/
|
|
73
|
-
releaseAsync(resource: T): Promise<void>;
|
|
74
|
-
/**
|
|
75
|
-
* Releases, destroys and removes any `resource` from `Pool`.
|
|
76
|
-
*/
|
|
77
|
-
destroy(resource: T, callback?: Callback): any;
|
|
78
|
-
/**
|
|
79
|
-
* Async version of destroy().
|
|
80
|
-
*/
|
|
81
|
-
destroyAsync(resource: T): Promise<void>;
|
|
82
|
-
/**
|
|
83
|
-
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
84
|
-
*/
|
|
85
|
-
isAcquired(resource: T): boolean;
|
|
86
|
-
/**
|
|
87
|
-
* Returns if the pool contains a `resource`
|
|
88
|
-
*/
|
|
89
|
-
includes(resource: T): boolean;
|
|
90
|
-
private _processNextRequest;
|
|
91
|
-
emit(event: string | symbol, ...args: any[]): boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Creates new resource
|
|
94
|
-
*/
|
|
95
|
-
private _createResource;
|
|
96
|
-
private _setHouseKeep;
|
|
97
|
-
private _houseKeep;
|
|
98
|
-
private _ensureMin;
|
|
99
|
-
private _itemSetAcquired;
|
|
100
|
-
private _itemDetach;
|
|
101
|
-
private _itemSetIdle;
|
|
102
|
-
private _itemDestroy;
|
|
103
|
-
private _itemValidate;
|
|
104
|
-
}
|
package/esm/resource-item.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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
|
-
}
|