lightning-pool 4.5.0 → 4.6.1
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 +1 -5
- package/esm/definitions.js +4 -7
- package/esm/index.js +6 -10
- package/esm/pool-options.js +2 -6
- package/esm/pool-request.js +1 -5
- package/esm/pool.js +49 -54
- package/esm/resource-item.js +3 -7
- package/package.json +1 -1
package/esm/abort-error.js
CHANGED
package/esm/definitions.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ResourceState = exports.PoolState = void 0;
|
|
4
|
-
var PoolState;
|
|
1
|
+
export var PoolState;
|
|
5
2
|
(function (PoolState) {
|
|
6
3
|
PoolState[PoolState["IDLE"] = 0] = "IDLE";
|
|
7
4
|
PoolState[PoolState["STARTED"] = 1] = "STARTED";
|
|
8
5
|
PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
|
|
9
6
|
PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
|
|
10
|
-
})(PoolState || (
|
|
11
|
-
var ResourceState;
|
|
7
|
+
})(PoolState || (PoolState = {}));
|
|
8
|
+
export var ResourceState;
|
|
12
9
|
(function (ResourceState) {
|
|
13
10
|
ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
|
|
14
11
|
ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
|
|
15
12
|
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
|
|
16
|
-
})(ResourceState || (
|
|
13
|
+
})(ResourceState || (ResourceState = {}));
|
package/esm/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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);
|
|
1
|
+
import { Pool } from './pool.js';
|
|
2
|
+
export * from './abort-error.js';
|
|
3
|
+
export * from './definitions.js';
|
|
4
|
+
export * from './pool.js';
|
|
5
|
+
export function createPool(factory, config) {
|
|
6
|
+
return new Pool(factory, config);
|
|
11
7
|
}
|
package/esm/pool-options.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PoolOptions = void 0;
|
|
4
|
-
const events_1 = require("events");
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
5
2
|
const defaultValues = {
|
|
6
3
|
acquireMaxRetries: 0,
|
|
7
4
|
acquireRetryWait: 2000,
|
|
@@ -15,7 +12,7 @@ const defaultValues = {
|
|
|
15
12
|
maxQueue: 1000,
|
|
16
13
|
validation: true,
|
|
17
14
|
};
|
|
18
|
-
class PoolOptions extends
|
|
15
|
+
export class PoolOptions extends EventEmitter {
|
|
19
16
|
constructor(pool) {
|
|
20
17
|
super();
|
|
21
18
|
this.pool = pool;
|
|
@@ -125,4 +122,3 @@ class PoolOptions extends events_1.EventEmitter {
|
|
|
125
122
|
}
|
|
126
123
|
}
|
|
127
124
|
}
|
|
128
|
-
exports.PoolOptions = PoolOptions;
|
package/esm/pool-request.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PoolRequest = void 0;
|
|
4
1
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
5
2
|
function noop() { }
|
|
6
|
-
class PoolRequest {
|
|
3
|
+
export class PoolRequest {
|
|
7
4
|
constructor(pool, callback, options) {
|
|
8
5
|
this.timedOut = false;
|
|
9
6
|
this.created = Date.now();
|
|
@@ -23,4 +20,3 @@ class PoolRequest {
|
|
|
23
20
|
clearTimeout(this.timeoutHandle);
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
|
-
exports.PoolRequest = PoolRequest;
|
package/esm/pool.js
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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 {
|
|
1
|
+
import DoublyLinked from 'doublylinked';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import promisify from 'putil-promisify';
|
|
4
|
+
import { AbortError } from './abort-error.js';
|
|
5
|
+
import { PoolState, ResourceState, } from './definitions.js';
|
|
6
|
+
import { PoolOptions } from './pool-options.js';
|
|
7
|
+
import { PoolRequest } from './pool-request.js';
|
|
8
|
+
import { ResourceItem } from './resource-item.js';
|
|
9
|
+
export class Pool extends EventEmitter {
|
|
14
10
|
constructor(factory, config) {
|
|
15
11
|
super();
|
|
16
|
-
this._requestQueue = new
|
|
12
|
+
this._requestQueue = new DoublyLinked();
|
|
17
13
|
this._allResources = new Map();
|
|
18
|
-
this._acquiredResources = new
|
|
19
|
-
this._idleResources = new
|
|
14
|
+
this._acquiredResources = new DoublyLinked();
|
|
15
|
+
this._idleResources = new DoublyLinked();
|
|
20
16
|
this._creating = 0;
|
|
21
17
|
this._requestsProcessing = 0;
|
|
22
|
-
this._state =
|
|
18
|
+
this._state = PoolState.IDLE;
|
|
23
19
|
if (typeof factory !== 'object') {
|
|
24
20
|
throw new TypeError('You must provide `factory` object');
|
|
25
21
|
}
|
|
@@ -35,7 +31,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
35
31
|
if (factory.reset && typeof factory.reset !== 'function') {
|
|
36
32
|
throw new TypeError('factory.reset can be a function');
|
|
37
33
|
}
|
|
38
|
-
const opts = (this._options = new
|
|
34
|
+
const opts = (this._options = new PoolOptions(this));
|
|
39
35
|
opts.on('change', (prop, val) => {
|
|
40
36
|
if (prop === 'houseKeepInterval')
|
|
41
37
|
this._setHouseKeep(val);
|
|
@@ -94,12 +90,12 @@ class Pool extends events_1.EventEmitter {
|
|
|
94
90
|
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
95
91
|
*/
|
|
96
92
|
start() {
|
|
97
|
-
if (this._state ===
|
|
93
|
+
if (this._state === PoolState.STARTED)
|
|
98
94
|
return;
|
|
99
|
-
if (this._state >=
|
|
95
|
+
if (this._state >= PoolState.CLOSING) {
|
|
100
96
|
throw new Error('Closed pool can not be started again');
|
|
101
97
|
}
|
|
102
|
-
this._state =
|
|
98
|
+
this._state = PoolState.STARTED;
|
|
103
99
|
this._setHouseKeep(this.options.houseKeepInterval);
|
|
104
100
|
this._ensureMin();
|
|
105
101
|
this.emit('start');
|
|
@@ -114,21 +110,21 @@ class Pool extends events_1.EventEmitter {
|
|
|
114
110
|
callback = arg1;
|
|
115
111
|
}
|
|
116
112
|
if (!callback) {
|
|
117
|
-
return
|
|
113
|
+
return promisify.fromCallback(cb => this.close(terminateWait, cb));
|
|
118
114
|
}
|
|
119
|
-
if (this._state ===
|
|
115
|
+
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) {
|
|
120
116
|
return callback();
|
|
121
117
|
}
|
|
122
|
-
if (this._state ===
|
|
118
|
+
if (this._state === PoolState.CLOSING) {
|
|
123
119
|
this.once('close', callback);
|
|
124
120
|
return;
|
|
125
121
|
}
|
|
126
122
|
this.emit('closing');
|
|
127
123
|
if (this._houseKeepTimer)
|
|
128
124
|
clearTimeout(this._houseKeepTimer);
|
|
129
|
-
this._state =
|
|
125
|
+
this._state = PoolState.CLOSING;
|
|
130
126
|
this._requestQueue.forEach(t => t.stopTimout());
|
|
131
|
-
this._requestQueue = new
|
|
127
|
+
this._requestQueue = new DoublyLinked();
|
|
132
128
|
this._requestsProcessing = 0;
|
|
133
129
|
if (terminateWait <= 0) {
|
|
134
130
|
this._acquiredResources.forEach(t => this.destroy(t.resource));
|
|
@@ -153,11 +149,11 @@ class Pool extends events_1.EventEmitter {
|
|
|
153
149
|
this.once('close', callback);
|
|
154
150
|
}
|
|
155
151
|
closeAsync(arg0) {
|
|
156
|
-
return
|
|
152
|
+
return promisify.fromCallback(cb => this.close(arg0, cb));
|
|
157
153
|
}
|
|
158
154
|
acquire(callback) {
|
|
159
155
|
if (!callback)
|
|
160
|
-
return
|
|
156
|
+
return promisify.fromCallback(cb => this.acquire(cb));
|
|
161
157
|
try {
|
|
162
158
|
this.start();
|
|
163
159
|
}
|
|
@@ -167,7 +163,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
167
163
|
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
168
164
|
return callback(new Error('Pool queue is full'));
|
|
169
165
|
}
|
|
170
|
-
this._requestQueue.push(new
|
|
166
|
+
this._requestQueue.push(new PoolRequest(this, callback));
|
|
171
167
|
this._processNextRequest();
|
|
172
168
|
}
|
|
173
169
|
/**
|
|
@@ -175,7 +171,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
175
171
|
*/
|
|
176
172
|
release(resource, callback) {
|
|
177
173
|
const item = this._allResources.get(resource);
|
|
178
|
-
if (item && item.state !==
|
|
174
|
+
if (item && item.state !== ResourceState.IDLE) {
|
|
179
175
|
this._itemSetIdle(item, callback);
|
|
180
176
|
}
|
|
181
177
|
this._processNextRequest();
|
|
@@ -184,7 +180,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
184
180
|
* Async version of release().
|
|
185
181
|
*/
|
|
186
182
|
releaseAsync(resource) {
|
|
187
|
-
return
|
|
183
|
+
return promisify.fromCallback(cb => this.release(resource, cb));
|
|
188
184
|
}
|
|
189
185
|
/**
|
|
190
186
|
* Releases, destroys and removes any `resource` from `Pool`.
|
|
@@ -205,7 +201,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
205
201
|
* Async version of destroy().
|
|
206
202
|
*/
|
|
207
203
|
destroyAsync(resource) {
|
|
208
|
-
return
|
|
204
|
+
return promisify.fromCallback(cb => this.destroy(resource, cb));
|
|
209
205
|
}
|
|
210
206
|
/**
|
|
211
207
|
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
@@ -221,7 +217,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
221
217
|
return this._allResources.has(resource);
|
|
222
218
|
}
|
|
223
219
|
_processNextRequest() {
|
|
224
|
-
if (this._state !==
|
|
220
|
+
if (this._state !== PoolState.STARTED ||
|
|
225
221
|
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
226
222
|
return;
|
|
227
223
|
}
|
|
@@ -235,7 +231,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
235
231
|
try {
|
|
236
232
|
if (item) {
|
|
237
233
|
/* istanbul ignore next : Hard to simulate */
|
|
238
|
-
if (this._state !==
|
|
234
|
+
if (this._state !== PoolState.STARTED) {
|
|
239
235
|
this._itemDestroy(item);
|
|
240
236
|
return;
|
|
241
237
|
}
|
|
@@ -301,7 +297,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
301
297
|
tries,
|
|
302
298
|
maxRetries: this.options.acquireMaxRetries,
|
|
303
299
|
});
|
|
304
|
-
if (err instanceof
|
|
300
|
+
if (err instanceof AbortError || tries >= maxRetries) {
|
|
305
301
|
this._creating--;
|
|
306
302
|
return callback && callback(err);
|
|
307
303
|
}
|
|
@@ -312,7 +308,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
312
308
|
return (callback &&
|
|
313
309
|
callback(new Error('Factory error. Resource already in pool')));
|
|
314
310
|
}
|
|
315
|
-
const item = new
|
|
311
|
+
const item = new ResourceItem(obj);
|
|
316
312
|
this._itemSetIdle(item);
|
|
317
313
|
this._allResources.set(obj, item);
|
|
318
314
|
if (callback)
|
|
@@ -324,9 +320,9 @@ class Pool extends events_1.EventEmitter {
|
|
|
324
320
|
const o = this._factory.create({ tries, maxRetries });
|
|
325
321
|
/* istanbul ignore next */
|
|
326
322
|
if (!o) {
|
|
327
|
-
return handleCallback(new
|
|
323
|
+
return handleCallback(new AbortError('Factory returned no resource'));
|
|
328
324
|
}
|
|
329
|
-
|
|
325
|
+
promisify.await(o, handleCallback);
|
|
330
326
|
}
|
|
331
327
|
catch (e) {
|
|
332
328
|
handleCallback(e);
|
|
@@ -338,13 +334,13 @@ class Pool extends events_1.EventEmitter {
|
|
|
338
334
|
if (this._houseKeepTimer)
|
|
339
335
|
clearInterval(this._houseKeepTimer);
|
|
340
336
|
this._houseKeepTimer = undefined;
|
|
341
|
-
if ((ms > 0 && this.state ===
|
|
342
|
-
this.state ===
|
|
337
|
+
if ((ms > 0 && this.state === PoolState.STARTED) ||
|
|
338
|
+
this.state === PoolState.CLOSING) {
|
|
343
339
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
344
340
|
}
|
|
345
341
|
}
|
|
346
342
|
_houseKeep() {
|
|
347
|
-
const isClosing = this._state ===
|
|
343
|
+
const isClosing = this._state === PoolState.CLOSING;
|
|
348
344
|
const now = Date.now();
|
|
349
345
|
let m = this._allResources.size - this.options.min;
|
|
350
346
|
let n = this._idleResources.length - this.options.minIdle;
|
|
@@ -362,7 +358,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
362
358
|
if (this._allResources.size)
|
|
363
359
|
return;
|
|
364
360
|
clearInterval(this._houseKeepTimer);
|
|
365
|
-
this._state =
|
|
361
|
+
this._state = PoolState.CLOSED;
|
|
366
362
|
this._requestsProcessing = 0;
|
|
367
363
|
this.emit('close');
|
|
368
364
|
}
|
|
@@ -375,24 +371,24 @@ class Pool extends events_1.EventEmitter {
|
|
|
375
371
|
});
|
|
376
372
|
}
|
|
377
373
|
_itemSetAcquired(item) {
|
|
378
|
-
if (item.state !==
|
|
374
|
+
if (item.state !== ResourceState.ACQUIRED) {
|
|
379
375
|
this._itemDetach(item);
|
|
380
|
-
item.state =
|
|
376
|
+
item.state = ResourceState.ACQUIRED;
|
|
381
377
|
this._acquiredResources.push(item);
|
|
382
378
|
item.acquiredNode = this._acquiredResources.tail;
|
|
383
379
|
}
|
|
384
380
|
}
|
|
385
381
|
_itemDetach(item) {
|
|
386
382
|
switch (item.state) {
|
|
387
|
-
case
|
|
383
|
+
case ResourceState.IDLE:
|
|
388
384
|
item.idleTime = 0;
|
|
389
385
|
/* istanbul ignore next*/
|
|
390
386
|
if (item.idleNode)
|
|
391
387
|
item.idleNode.remove();
|
|
392
388
|
item.idleNode = undefined;
|
|
393
389
|
break;
|
|
394
|
-
case
|
|
395
|
-
case
|
|
390
|
+
case ResourceState.ACQUIRED:
|
|
391
|
+
case ResourceState.VALIDATION:
|
|
396
392
|
/* istanbul ignore next*/
|
|
397
393
|
if (item.acquiredNode)
|
|
398
394
|
item.acquiredNode.remove();
|
|
@@ -403,13 +399,13 @@ class Pool extends events_1.EventEmitter {
|
|
|
403
399
|
}
|
|
404
400
|
}
|
|
405
401
|
_itemSetIdle(item, callback) {
|
|
406
|
-
const isAcquired = item.state ===
|
|
402
|
+
const isAcquired = item.state === ResourceState.ACQUIRED;
|
|
407
403
|
const handleCallback = (err) => {
|
|
408
404
|
if (err)
|
|
409
405
|
return this._itemDestroy(item, callback);
|
|
410
406
|
this._itemDetach(item);
|
|
411
407
|
item.idleTime = Date.now();
|
|
412
|
-
item.state =
|
|
408
|
+
item.state = ResourceState.IDLE;
|
|
413
409
|
if (this.options.fifo) {
|
|
414
410
|
this._idleResources.push(item);
|
|
415
411
|
item.idleNode = this._idleResources.tail;
|
|
@@ -428,7 +424,7 @@ class Pool extends events_1.EventEmitter {
|
|
|
428
424
|
if (isAcquired && this._factory.reset) {
|
|
429
425
|
try {
|
|
430
426
|
const o = this._factory.reset(item.resource);
|
|
431
|
-
|
|
427
|
+
promisify.await(o, handleCallback);
|
|
432
428
|
}
|
|
433
429
|
catch (e) {
|
|
434
430
|
handleCallback(e);
|
|
@@ -454,18 +450,18 @@ class Pool extends events_1.EventEmitter {
|
|
|
454
450
|
this._allResources.delete(item.resource);
|
|
455
451
|
this._processNextRequest();
|
|
456
452
|
const o = this._factory.destroy(item.resource);
|
|
457
|
-
|
|
453
|
+
promisify.await(o, handleCallback);
|
|
458
454
|
}
|
|
459
455
|
catch (e) {
|
|
460
456
|
handleCallback(e);
|
|
461
457
|
}
|
|
462
458
|
}
|
|
463
459
|
_itemValidate(item, callback) {
|
|
464
|
-
item.state =
|
|
460
|
+
item.state = ResourceState.VALIDATION;
|
|
465
461
|
try {
|
|
466
462
|
const o = this._factory.validate?.(item.resource);
|
|
467
463
|
// @ts-ignore
|
|
468
|
-
|
|
464
|
+
promisify.await(o, callback);
|
|
469
465
|
}
|
|
470
466
|
catch (e) {
|
|
471
467
|
if (callback)
|
|
@@ -473,4 +469,3 @@ class Pool extends events_1.EventEmitter {
|
|
|
473
469
|
}
|
|
474
470
|
}
|
|
475
471
|
}
|
|
476
|
-
exports.Pool = Pool;
|
package/esm/resource-item.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.ResourceItem = void 0;
|
|
4
|
-
const definitions_js_1 = require("./definitions.js");
|
|
5
|
-
class ResourceItem {
|
|
1
|
+
import { ResourceState } from './definitions.js';
|
|
2
|
+
export class ResourceItem {
|
|
6
3
|
constructor(resource) {
|
|
7
|
-
this.state =
|
|
4
|
+
this.state = ResourceState.IDLE;
|
|
8
5
|
this.idleTime = 0;
|
|
9
6
|
this.destroyed = false;
|
|
10
7
|
this.resource = resource;
|
|
11
8
|
}
|
|
12
9
|
}
|
|
13
|
-
exports.ResourceItem = ResourceItem;
|