lightning-pool 4.2.2 → 4.4.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/cjs/index.js +5 -19
- package/cjs/pool-options.js +3 -2
- package/cjs/pool-request.js +1 -2
- package/cjs/pool.js +43 -31
- package/esm/index.js +1 -1
- package/esm/pool-options.js +3 -2
- package/esm/pool-request.js +1 -2
- package/esm/pool.js +41 -27
- package/package.json +7 -52
- 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/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,8 +2,7 @@
|
|
|
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
7
|
constructor(pool, callback) {
|
|
9
8
|
this.timedOut = false;
|
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,18 +155,18 @@ 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(
|
|
154
|
-
if (!
|
|
158
|
+
acquire(callback) {
|
|
159
|
+
if (!callback)
|
|
155
160
|
return putil_promisify_1.default.fromCallback(cb => this.acquire(cb));
|
|
156
|
-
const callback = arg0;
|
|
157
161
|
try {
|
|
158
162
|
this.start();
|
|
159
163
|
}
|
|
160
164
|
catch (e) {
|
|
161
165
|
return callback(e);
|
|
162
166
|
}
|
|
163
|
-
if (this.options.maxQueue && this.pending >= this.options.maxQueue)
|
|
167
|
+
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
164
168
|
return callback(new Error('Pool queue is full'));
|
|
169
|
+
}
|
|
165
170
|
this._requestQueue.push(new pool_request_js_1.PoolRequest(this, callback));
|
|
166
171
|
this._processNextRequest();
|
|
167
172
|
}
|
|
@@ -170,8 +175,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
170
175
|
*/
|
|
171
176
|
release(resource, callback) {
|
|
172
177
|
const item = this._allResources.get(resource);
|
|
173
|
-
if (item && item.state !== definitions_js_1.ResourceState.IDLE)
|
|
178
|
+
if (item && item.state !== definitions_js_1.ResourceState.IDLE) {
|
|
174
179
|
this._itemSetIdle(item, callback);
|
|
180
|
+
}
|
|
175
181
|
this._processNextRequest();
|
|
176
182
|
}
|
|
177
183
|
/**
|
|
@@ -216,8 +222,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
216
222
|
}
|
|
217
223
|
_processNextRequest() {
|
|
218
224
|
if (this._state !== definitions_js_1.PoolState.STARTED ||
|
|
219
|
-
this._requestsProcessing >= this.options.max - this.acquired)
|
|
225
|
+
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
220
226
|
return;
|
|
227
|
+
}
|
|
221
228
|
const request = this._requestQueue.shift();
|
|
222
229
|
if (!request)
|
|
223
230
|
return;
|
|
@@ -292,7 +299,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
292
299
|
this.emit('error', err, {
|
|
293
300
|
requestTime: request ? request.created : Date.now(),
|
|
294
301
|
tries,
|
|
295
|
-
maxRetries: this.options.acquireMaxRetries
|
|
302
|
+
maxRetries: this.options.acquireMaxRetries,
|
|
296
303
|
});
|
|
297
304
|
if (err instanceof abort_error_js_1.AbortError || tries >= maxRetries) {
|
|
298
305
|
this._creating--;
|
|
@@ -301,9 +308,10 @@ class Pool extends events_1.EventEmitter {
|
|
|
301
308
|
return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
|
|
302
309
|
}
|
|
303
310
|
this._creating--;
|
|
304
|
-
if (this._allResources.has(obj))
|
|
305
|
-
return callback &&
|
|
306
|
-
callback(new Error('Factory error. Resource already in pool'));
|
|
311
|
+
if (this._allResources.has(obj)) {
|
|
312
|
+
return (callback &&
|
|
313
|
+
callback(new Error('Factory error. Resource already in pool')));
|
|
314
|
+
}
|
|
307
315
|
const item = new resource_item_js_1.ResourceItem(obj);
|
|
308
316
|
this._itemSetIdle(item);
|
|
309
317
|
this._allResources.set(obj, item);
|
|
@@ -315,8 +323,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
315
323
|
try {
|
|
316
324
|
const o = this._factory.create({ tries, maxRetries });
|
|
317
325
|
/* istanbul ignore next */
|
|
318
|
-
if (!o)
|
|
326
|
+
if (!o) {
|
|
319
327
|
return handleCallback(new abort_error_js_1.AbortError('Factory returned no resource'));
|
|
328
|
+
}
|
|
320
329
|
putil_promisify_1.default.await(o, handleCallback);
|
|
321
330
|
}
|
|
322
331
|
catch (e) {
|
|
@@ -329,8 +338,10 @@ class Pool extends events_1.EventEmitter {
|
|
|
329
338
|
if (this._houseKeepTimer)
|
|
330
339
|
clearInterval(this._houseKeepTimer);
|
|
331
340
|
this._houseKeepTimer = undefined;
|
|
332
|
-
if (ms > 0 && this.state === definitions_js_1.PoolState.STARTED ||
|
|
341
|
+
if ((ms > 0 && this.state === definitions_js_1.PoolState.STARTED) ||
|
|
342
|
+
this.state === definitions_js_1.PoolState.CLOSING) {
|
|
333
343
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
344
|
+
}
|
|
334
345
|
}
|
|
335
346
|
_houseKeep() {
|
|
336
347
|
const isClosing = this._state === definitions_js_1.PoolState.CLOSING;
|
|
@@ -339,10 +350,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
339
350
|
let n = this._idleResources.length - this.options.minIdle;
|
|
340
351
|
if (isClosing || (m > 0 && n > 0)) {
|
|
341
352
|
this._idleResources.every((item) => {
|
|
342
|
-
if (isClosing ||
|
|
343
|
-
item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
353
|
+
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
344
354
|
this._itemDestroy(item);
|
|
345
|
-
return isClosing || !!(
|
|
355
|
+
return isClosing || !!(--n && --m);
|
|
346
356
|
}
|
|
347
357
|
return false;
|
|
348
358
|
});
|
|
@@ -388,6 +398,8 @@ class Pool extends events_1.EventEmitter {
|
|
|
388
398
|
item.acquiredNode.remove();
|
|
389
399
|
item.acquiredNode = undefined;
|
|
390
400
|
break;
|
|
401
|
+
default:
|
|
402
|
+
break;
|
|
391
403
|
}
|
|
392
404
|
}
|
|
393
405
|
_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
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,18 +151,18 @@ export class Pool extends EventEmitter {
|
|
|
144
151
|
closeAsync(arg0) {
|
|
145
152
|
return promisify.fromCallback(cb => this.close(arg0, cb));
|
|
146
153
|
}
|
|
147
|
-
acquire(
|
|
148
|
-
if (!
|
|
154
|
+
acquire(callback) {
|
|
155
|
+
if (!callback)
|
|
149
156
|
return promisify.fromCallback(cb => this.acquire(cb));
|
|
150
|
-
const callback = arg0;
|
|
151
157
|
try {
|
|
152
158
|
this.start();
|
|
153
159
|
}
|
|
154
160
|
catch (e) {
|
|
155
161
|
return callback(e);
|
|
156
162
|
}
|
|
157
|
-
if (this.options.maxQueue && this.pending >= this.options.maxQueue)
|
|
163
|
+
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
158
164
|
return callback(new Error('Pool queue is full'));
|
|
165
|
+
}
|
|
159
166
|
this._requestQueue.push(new PoolRequest(this, callback));
|
|
160
167
|
this._processNextRequest();
|
|
161
168
|
}
|
|
@@ -164,8 +171,9 @@ export class Pool extends EventEmitter {
|
|
|
164
171
|
*/
|
|
165
172
|
release(resource, callback) {
|
|
166
173
|
const item = this._allResources.get(resource);
|
|
167
|
-
if (item && item.state !== ResourceState.IDLE)
|
|
174
|
+
if (item && item.state !== ResourceState.IDLE) {
|
|
168
175
|
this._itemSetIdle(item, callback);
|
|
176
|
+
}
|
|
169
177
|
this._processNextRequest();
|
|
170
178
|
}
|
|
171
179
|
/**
|
|
@@ -210,8 +218,9 @@ export class Pool extends EventEmitter {
|
|
|
210
218
|
}
|
|
211
219
|
_processNextRequest() {
|
|
212
220
|
if (this._state !== PoolState.STARTED ||
|
|
213
|
-
this._requestsProcessing >= this.options.max - this.acquired)
|
|
221
|
+
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
214
222
|
return;
|
|
223
|
+
}
|
|
215
224
|
const request = this._requestQueue.shift();
|
|
216
225
|
if (!request)
|
|
217
226
|
return;
|
|
@@ -286,7 +295,7 @@ export class Pool extends EventEmitter {
|
|
|
286
295
|
this.emit('error', err, {
|
|
287
296
|
requestTime: request ? request.created : Date.now(),
|
|
288
297
|
tries,
|
|
289
|
-
maxRetries: this.options.acquireMaxRetries
|
|
298
|
+
maxRetries: this.options.acquireMaxRetries,
|
|
290
299
|
});
|
|
291
300
|
if (err instanceof AbortError || tries >= maxRetries) {
|
|
292
301
|
this._creating--;
|
|
@@ -295,9 +304,10 @@ export class Pool extends EventEmitter {
|
|
|
295
304
|
return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
|
|
296
305
|
}
|
|
297
306
|
this._creating--;
|
|
298
|
-
if (this._allResources.has(obj))
|
|
299
|
-
return callback &&
|
|
300
|
-
callback(new Error('Factory error. Resource already in pool'));
|
|
307
|
+
if (this._allResources.has(obj)) {
|
|
308
|
+
return (callback &&
|
|
309
|
+
callback(new Error('Factory error. Resource already in pool')));
|
|
310
|
+
}
|
|
301
311
|
const item = new ResourceItem(obj);
|
|
302
312
|
this._itemSetIdle(item);
|
|
303
313
|
this._allResources.set(obj, item);
|
|
@@ -309,8 +319,9 @@ export class Pool extends EventEmitter {
|
|
|
309
319
|
try {
|
|
310
320
|
const o = this._factory.create({ tries, maxRetries });
|
|
311
321
|
/* istanbul ignore next */
|
|
312
|
-
if (!o)
|
|
322
|
+
if (!o) {
|
|
313
323
|
return handleCallback(new AbortError('Factory returned no resource'));
|
|
324
|
+
}
|
|
314
325
|
promisify.await(o, handleCallback);
|
|
315
326
|
}
|
|
316
327
|
catch (e) {
|
|
@@ -323,8 +334,10 @@ export class Pool extends EventEmitter {
|
|
|
323
334
|
if (this._houseKeepTimer)
|
|
324
335
|
clearInterval(this._houseKeepTimer);
|
|
325
336
|
this._houseKeepTimer = undefined;
|
|
326
|
-
if (ms > 0 && this.state === PoolState.STARTED ||
|
|
337
|
+
if ((ms > 0 && this.state === PoolState.STARTED) ||
|
|
338
|
+
this.state === PoolState.CLOSING) {
|
|
327
339
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
340
|
+
}
|
|
328
341
|
}
|
|
329
342
|
_houseKeep() {
|
|
330
343
|
const isClosing = this._state === PoolState.CLOSING;
|
|
@@ -333,10 +346,9 @@ export class Pool extends EventEmitter {
|
|
|
333
346
|
let n = this._idleResources.length - this.options.minIdle;
|
|
334
347
|
if (isClosing || (m > 0 && n > 0)) {
|
|
335
348
|
this._idleResources.every((item) => {
|
|
336
|
-
if (isClosing ||
|
|
337
|
-
item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
349
|
+
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
338
350
|
this._itemDestroy(item);
|
|
339
|
-
return isClosing || !!(
|
|
351
|
+
return isClosing || !!(--n && --m);
|
|
340
352
|
}
|
|
341
353
|
return false;
|
|
342
354
|
});
|
|
@@ -382,6 +394,8 @@ export class Pool extends EventEmitter {
|
|
|
382
394
|
item.acquiredNode.remove();
|
|
383
395
|
item.acquiredNode = undefined;
|
|
384
396
|
break;
|
|
397
|
+
default:
|
|
398
|
+
break;
|
|
385
399
|
}
|
|
386
400
|
}
|
|
387
401
|
_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.4.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.
|
|
17
|
+
"doublylinked": "^2.5.4",
|
|
18
18
|
"putil-promisify": "^1.10.1"
|
|
19
19
|
},
|
|
20
|
-
"devDependencies": {
|
|
21
|
-
"@babel/eslint-parser": "^7.22.15",
|
|
22
|
-
"@types/jest": "^29.5.4",
|
|
23
|
-
"@types/node": "^20.6.0",
|
|
24
|
-
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
|
25
|
-
"@typescript-eslint/parser": "^6.6.0",
|
|
26
|
-
"dotenv": "^16.3.1",
|
|
27
|
-
"eslint": "^8.49.0",
|
|
28
|
-
"eslint-config-google": "^0.14.0",
|
|
29
|
-
"eslint-plugin-import": "^2.28.1",
|
|
30
|
-
"eslint-plugin-security": "^1.7.1",
|
|
31
|
-
"eslint-plugin-simple-import-sort": "^10.0.0",
|
|
32
|
-
"eslint-plugin-unused-imports": "^3.0.0",
|
|
33
|
-
"jest": "^29.6.4",
|
|
34
|
-
"npm-run-path": "^5.1.0",
|
|
35
|
-
"ts-cleanup": "^0.2.6",
|
|
36
|
-
"ts-jest": "^29.1.1",
|
|
37
|
-
"ts-loader": "^9.4.4",
|
|
38
|
-
"ts-node": "^10.9.1",
|
|
39
|
-
"tsconfig-paths": "^4.2.0",
|
|
40
|
-
"typescript": "^5.2.2"
|
|
41
|
-
},
|
|
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
|
-
}
|