lightning-pool 4.13.0 → 4.14.2
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 +6 -293
- package/abort-error.d.ts +11 -0
- package/abort-error.js +11 -0
- package/constants.d.ts +12 -0
- package/constants.js +12 -0
- package/index.d.ts +8 -0
- package/index.js +8 -0
- package/package.json +9 -3
- package/pool-options.d.ts +72 -0
- package/pool-options.js +81 -3
- package/pool-request.d.ts +15 -0
- package/pool-request.js +17 -0
- package/pool.d.ts +125 -10
- package/pool.js +156 -19
- package/resource-item.d.ts +10 -0
- package/resource-item.js +10 -0
- package/types.d.ts +124 -0
- package/CHANGELOG.md +0 -436
package/pool.js
CHANGED
|
@@ -6,6 +6,27 @@ import { PoolState, ResourceState } from './constants.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';
|
|
9
|
+
/**
|
|
10
|
+
* Like putil-promisify's `promisify.await()`, but attaches both handlers to
|
|
11
|
+
* the original promise via `.then(onFulfilled, onRejected)` instead of
|
|
12
|
+
* chaining a `.catch()` off a second promise `.then()` returns - one fewer
|
|
13
|
+
* Promise allocated per call on a path (create/destroy/reset/validate) that
|
|
14
|
+
* every acquire/release goes through. Mirrors promisify.await's own loose
|
|
15
|
+
* `(x: any, callback: (error?: Error, value?: T) => void)` signature.
|
|
16
|
+
*/
|
|
17
|
+
function awaitResult(value, callback) {
|
|
18
|
+
if (value && typeof value.then === 'function') {
|
|
19
|
+
value.then((v) => callback && callback(undefined, v), (e) => callback && callback(e));
|
|
20
|
+
}
|
|
21
|
+
else if (callback)
|
|
22
|
+
callback(undefined, value);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A generic resource pool: acquires, reuses, and (when idle too long or on
|
|
26
|
+
* validation failure) discards resources created by a `PoolFactory`.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam T - The resource type this pool manages (e.g. a DB connection).
|
|
29
|
+
*/
|
|
9
30
|
export class Pool extends EventEmitter {
|
|
10
31
|
_options;
|
|
11
32
|
_factory;
|
|
@@ -18,6 +39,12 @@ export class Pool extends EventEmitter {
|
|
|
18
39
|
_state = PoolState.IDLE;
|
|
19
40
|
_houseKeepTimer;
|
|
20
41
|
_closeWaitTimer;
|
|
42
|
+
/**
|
|
43
|
+
* @param factory - Creates, destroys, and (optionally) resets/validates resources.
|
|
44
|
+
* @param config - Initial options; every option can also be changed later via `pool.options`.
|
|
45
|
+
* @throws \{@link TypeError\} If `factory` isn't an object, or `create`/`destroy` aren't
|
|
46
|
+
* functions, or `reset`/`validate` are present but aren't functions.
|
|
47
|
+
*/
|
|
21
48
|
constructor(factory, config) {
|
|
22
49
|
super();
|
|
23
50
|
if (typeof factory !== 'object') {
|
|
@@ -92,6 +119,8 @@ export class Pool extends EventEmitter {
|
|
|
92
119
|
/**
|
|
93
120
|
* Starts the pool and begins creating of resources, starts the housekeeper and any other internal logic.
|
|
94
121
|
* Note: This method is not needed to be called. Pool instance will automatically be started when acquire() method is called
|
|
122
|
+
*
|
|
123
|
+
* @throws \{@link Error\} If the pool is currently `CLOSING`.
|
|
95
124
|
*/
|
|
96
125
|
start() {
|
|
97
126
|
if (this._state === PoolState.STARTED)
|
|
@@ -137,7 +166,16 @@ export class Pool extends EventEmitter {
|
|
|
137
166
|
if (this._houseKeepTimer)
|
|
138
167
|
clearTimeout(this._houseKeepTimer);
|
|
139
168
|
this._state = PoolState.CLOSING;
|
|
140
|
-
|
|
169
|
+
const closingError = new Error('Pool is closing');
|
|
170
|
+
this._requestQueue.forEach(t => {
|
|
171
|
+
t.stopTimout();
|
|
172
|
+
try {
|
|
173
|
+
t.callback(closingError);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// ignored
|
|
177
|
+
}
|
|
178
|
+
});
|
|
141
179
|
this._requestQueue = new DoublyLinked();
|
|
142
180
|
this._requestsProcessing = 0;
|
|
143
181
|
if (terminateWait <= 0) {
|
|
@@ -178,27 +216,49 @@ export class Pool extends EventEmitter {
|
|
|
178
216
|
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
179
217
|
return callback(new Error('Pool queue is full'));
|
|
180
218
|
}
|
|
181
|
-
|
|
219
|
+
const request = new PoolRequest(this, callback);
|
|
220
|
+
/* A timed-out request leaves the queue at once, so `pending` drops and
|
|
221
|
+
* the dispatcher never picks it up. No-op once it is being processed. */
|
|
222
|
+
request.onTimeout = () => this._requestQueue.remove(request);
|
|
223
|
+
this._requestQueue.push(request);
|
|
182
224
|
this._processNextRequest();
|
|
183
225
|
}
|
|
184
226
|
/**
|
|
185
|
-
* Releases
|
|
227
|
+
* Releases a previously-acquired `resource` back to the pool so it can be
|
|
228
|
+
* reused (running `factory.reset()` first, if provided).
|
|
229
|
+
*
|
|
230
|
+
* @param resource - A previously acquired resource. A no-op (aside from invoking
|
|
231
|
+
* `callback`) if it isn't currently acquired or isn't in this pool.
|
|
232
|
+
* @param callback - Called once released (and `reset()`, if any, has finished).
|
|
233
|
+
* @remarks Returns immediately (`undefined`) regardless of `callback` -
|
|
234
|
+
* it does not wait for `reset()` to finish. Use `releaseAsync()` (or pass
|
|
235
|
+
* `callback`) to know when the release has actually completed.
|
|
186
236
|
*/
|
|
187
237
|
release(resource, callback) {
|
|
188
238
|
const item = this._allResources.get(resource);
|
|
189
239
|
if (item && item.state !== ResourceState.IDLE) {
|
|
190
240
|
this._itemSetIdle(item, callback);
|
|
191
241
|
}
|
|
242
|
+
else if (callback)
|
|
243
|
+
callback();
|
|
192
244
|
this._processNextRequest();
|
|
193
245
|
}
|
|
194
246
|
/**
|
|
195
|
-
*
|
|
247
|
+
* Promise-returning equivalent of `release()`.
|
|
248
|
+
*
|
|
249
|
+
* @param resource - A previously acquired resource.
|
|
250
|
+
* @returns A `Promise` that resolves once released (and `reset()`, if any, has finished).
|
|
196
251
|
*/
|
|
197
252
|
releaseAsync(resource) {
|
|
198
253
|
return promisify.fromCallback(cb => this.release(resource, cb));
|
|
199
254
|
}
|
|
200
255
|
/**
|
|
201
|
-
* Releases, destroys and removes
|
|
256
|
+
* Releases, destroys, and removes a `resource` from the pool entirely -
|
|
257
|
+
* it will not be reused (running `factory.destroy()`).
|
|
258
|
+
*
|
|
259
|
+
* @param resource - A previously acquired (or idle) resource. A no-op
|
|
260
|
+
* (aside from invoking `callback`) if it isn't in this pool.
|
|
261
|
+
* @param callback - Called once destroyed (or on `factory.destroy()` failure).
|
|
202
262
|
*/
|
|
203
263
|
destroy(resource, callback) {
|
|
204
264
|
try {
|
|
@@ -213,24 +273,38 @@ export class Pool extends EventEmitter {
|
|
|
213
273
|
}
|
|
214
274
|
}
|
|
215
275
|
/**
|
|
216
|
-
*
|
|
276
|
+
* Promise-returning equivalent of `destroy()`.
|
|
277
|
+
*
|
|
278
|
+
* @param resource - A previously acquired (or idle) resource.
|
|
279
|
+
* @returns A `Promise` that resolves once destroyed.
|
|
217
280
|
*/
|
|
218
281
|
destroyAsync(resource) {
|
|
219
282
|
return promisify.fromCallback(cb => this.destroy(resource, cb));
|
|
220
283
|
}
|
|
221
284
|
/**
|
|
222
285
|
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
286
|
+
*
|
|
287
|
+
* @param resource - The resource to check.
|
|
223
288
|
*/
|
|
224
289
|
isAcquired(resource) {
|
|
225
290
|
const item = this._allResources.get(resource);
|
|
226
291
|
return !!(item && item.acquiredNode);
|
|
227
292
|
}
|
|
228
293
|
/**
|
|
229
|
-
* Returns if the pool contains a `resource`
|
|
294
|
+
* Returns if the pool contains a `resource` - acquired, idle, or being
|
|
295
|
+
* validated (not yet destroyed).
|
|
296
|
+
*
|
|
297
|
+
* @param resource - The resource to check.
|
|
230
298
|
*/
|
|
231
299
|
includes(resource) {
|
|
232
300
|
return this._allResources.has(resource);
|
|
233
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* Serves the next queued `acquire()` request, if any and if capacity
|
|
304
|
+
* allows: from an idle resource (validating it first if enabled), or by
|
|
305
|
+
* creating a new one. Called after anything that could free up capacity
|
|
306
|
+
* or add a request (release, destroy, acquire, options change).
|
|
307
|
+
*/
|
|
234
308
|
_processNextRequest() {
|
|
235
309
|
if (this._state !== PoolState.STARTED ||
|
|
236
310
|
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
@@ -250,12 +324,19 @@ export class Pool extends EventEmitter {
|
|
|
250
324
|
this._itemDestroy(item);
|
|
251
325
|
return;
|
|
252
326
|
}
|
|
327
|
+
if (request.timedOut) {
|
|
328
|
+
/* Request already failed with a timeout error; return the
|
|
329
|
+
* resource to the idle pool instead of handing it to an
|
|
330
|
+
* abandoned caller. */
|
|
331
|
+
this._itemSetIdle(item);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
253
334
|
this._itemSetAcquired(item);
|
|
254
335
|
this._ensureMin();
|
|
255
336
|
request.callback(undefined, item.resource);
|
|
256
337
|
this.emit('acquire', item.resource);
|
|
257
338
|
}
|
|
258
|
-
else
|
|
339
|
+
else if (!request.timedOut)
|
|
259
340
|
request.callback(err);
|
|
260
341
|
}
|
|
261
342
|
catch {
|
|
@@ -273,7 +354,10 @@ export class Pool extends EventEmitter {
|
|
|
273
354
|
this._itemDestroy(item);
|
|
274
355
|
this.emit('validate-error', err, item.resource);
|
|
275
356
|
this._requestsProcessing--;
|
|
276
|
-
|
|
357
|
+
/* Retry with another resource, unless the request timed out
|
|
358
|
+
* while the resource was being validated */
|
|
359
|
+
if (!request.timedOut)
|
|
360
|
+
this._requestQueue.unshift(request);
|
|
277
361
|
this._processNextRequest();
|
|
278
362
|
}
|
|
279
363
|
else
|
|
@@ -286,6 +370,19 @@ export class Pool extends EventEmitter {
|
|
|
286
370
|
/** There is no idle resource. We need to create new one **/
|
|
287
371
|
this._createResource(request, handleCallback);
|
|
288
372
|
}
|
|
373
|
+
/**
|
|
374
|
+
* Overrides `EventEmitter.emit()`: identical, except an error thrown by a
|
|
375
|
+
* listener is swallowed instead of propagating out of `emit()` - a
|
|
376
|
+
* misbehaving listener on, say, `'acquire'` can't take down the internal
|
|
377
|
+
* code path that just emitted it.
|
|
378
|
+
*
|
|
379
|
+
* @param event - Event name. See the class-level events list in `docs/API.md`
|
|
380
|
+
* (`start`, `closing`, `close`, `terminate`, `create`, `error`, `acquire`,
|
|
381
|
+
* `return`, `destroy`, `destroy-error`, `validate-error`, `request-timeout`).
|
|
382
|
+
* @param args - Arguments passed to each listener.
|
|
383
|
+
* @returns `true` if the event had listeners, `false` otherwise (same as
|
|
384
|
+
* `EventEmitter.emit()`); also `true` if a listener threw.
|
|
385
|
+
*/
|
|
289
386
|
emit(event, ...args) {
|
|
290
387
|
// Prevents errors while calling emit()
|
|
291
388
|
try {
|
|
@@ -296,15 +393,20 @@ export class Pool extends EventEmitter {
|
|
|
296
393
|
}
|
|
297
394
|
}
|
|
298
395
|
/**
|
|
299
|
-
* Creates new resource
|
|
396
|
+
* Creates a new resource via `factory.create()`, retrying on failure up
|
|
397
|
+
* to `acquireMaxRetries` times (waiting `acquireRetryWait` between
|
|
398
|
+
* attempts), unless `request` has already timed out or the factory threw
|
|
399
|
+
* an `AbortError`. On success, adds the resource to the pool as idle.
|
|
400
|
+
*
|
|
401
|
+
* @param request - The acquire request this creation is for, if any (`_ensureMin()`
|
|
402
|
+
* creates resources with no associated request).
|
|
403
|
+
* @param callback - Called with the new `ResourceItem`, or the final error.
|
|
300
404
|
*/
|
|
301
405
|
_createResource(request, callback) {
|
|
302
406
|
const maxRetries = this.options.acquireMaxRetries;
|
|
303
407
|
let tries = 0;
|
|
304
408
|
this._creating++;
|
|
305
409
|
const handleCallback = (err, obj) => {
|
|
306
|
-
if (request && request.timedOut)
|
|
307
|
-
return;
|
|
308
410
|
if (err || !obj) {
|
|
309
411
|
tries++;
|
|
310
412
|
this.emit('error', err, {
|
|
@@ -312,7 +414,9 @@ export class Pool extends EventEmitter {
|
|
|
312
414
|
tries,
|
|
313
415
|
maxRetries: this.options.acquireMaxRetries,
|
|
314
416
|
});
|
|
315
|
-
|
|
417
|
+
/* Stop retrying for a request that already timed out */
|
|
418
|
+
const abandoned = !!(request && request.timedOut);
|
|
419
|
+
if (abandoned || err instanceof AbortError || tries >= maxRetries) {
|
|
316
420
|
this._creating--;
|
|
317
421
|
return callback && callback(err);
|
|
318
422
|
}
|
|
@@ -337,7 +441,7 @@ export class Pool extends EventEmitter {
|
|
|
337
441
|
if (!o) {
|
|
338
442
|
return handleCallback(new AbortError('Factory returned no resource'));
|
|
339
443
|
}
|
|
340
|
-
|
|
444
|
+
awaitResult(o, handleCallback);
|
|
341
445
|
}
|
|
342
446
|
catch (e) {
|
|
343
447
|
handleCallback(e);
|
|
@@ -345,6 +449,12 @@ export class Pool extends EventEmitter {
|
|
|
345
449
|
};
|
|
346
450
|
tryCreate();
|
|
347
451
|
}
|
|
452
|
+
/**
|
|
453
|
+
* (Re)starts the housekeeper timer at interval `ms`, or leaves it stopped
|
|
454
|
+
* if the pool isn't `STARTED`/`CLOSING`. Called on `start()`, whenever
|
|
455
|
+
* `houseKeepInterval` changes, and (with a short fixed interval) while
|
|
456
|
+
* closing.
|
|
457
|
+
*/
|
|
348
458
|
_setHouseKeep(ms) {
|
|
349
459
|
if (this._houseKeepTimer)
|
|
350
460
|
clearInterval(this._houseKeepTimer);
|
|
@@ -354,6 +464,12 @@ export class Pool extends EventEmitter {
|
|
|
354
464
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
355
465
|
}
|
|
356
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* One housekeeping pass: destroys idle resources past `idleTimeoutMillis`
|
|
469
|
+
* (as long as doing so doesn't drop below `min`/`minIdle`), or - while
|
|
470
|
+
* `CLOSING` - destroys every idle resource unconditionally and, once no
|
|
471
|
+
* resources remain at all, finishes the transition to `CLOSED`.
|
|
472
|
+
*/
|
|
357
473
|
_houseKeep() {
|
|
358
474
|
const isClosing = this._state === PoolState.CLOSING;
|
|
359
475
|
const now = Date.now();
|
|
@@ -370,7 +486,7 @@ export class Pool extends EventEmitter {
|
|
|
370
486
|
}
|
|
371
487
|
if (isClosing) {
|
|
372
488
|
/* Check again 5 ms later */
|
|
373
|
-
if (this._allResources.size)
|
|
489
|
+
if (this._allResources.size || this._creating)
|
|
374
490
|
return;
|
|
375
491
|
clearInterval(this._houseKeepTimer);
|
|
376
492
|
this._state = PoolState.CLOSED;
|
|
@@ -378,13 +494,26 @@ export class Pool extends EventEmitter {
|
|
|
378
494
|
this.emit('close');
|
|
379
495
|
}
|
|
380
496
|
}
|
|
497
|
+
/**
|
|
498
|
+
* Schedules (via `process.nextTick`) creating however many resources are
|
|
499
|
+
* needed to satisfy `min`/`minIdle`, accounting for creations already in
|
|
500
|
+
* flight. Called after start, after every successful acquire, and
|
|
501
|
+
* whenever `min`/`minIdle` change.
|
|
502
|
+
*/
|
|
381
503
|
_ensureMin() {
|
|
504
|
+
// Common case (min/minIdle both unset): the scheduled tick below would
|
|
505
|
+
// always compute k <= 0 and do nothing, so skip the nextTick() and its
|
|
506
|
+
// closure allocation entirely rather than paying for a wasted microtask
|
|
507
|
+
// on every single acquire().
|
|
508
|
+
if (this.options.min <= 0 && this.options.minIdle <= 0)
|
|
509
|
+
return;
|
|
382
510
|
process.nextTick(() => {
|
|
383
511
|
let k = Math.max(this.options.min - this._allResources.size, this.options.minIdle - this._idleResources.length) - this.creating;
|
|
384
512
|
while (k-- > 0)
|
|
385
513
|
this._createResource();
|
|
386
514
|
});
|
|
387
515
|
}
|
|
516
|
+
/** Moves `item` into the acquired list, detaching it from wherever it was first. */
|
|
388
517
|
_itemSetAcquired(item) {
|
|
389
518
|
if (item.state !== ResourceState.ACQUIRED) {
|
|
390
519
|
this._itemDetach(item);
|
|
@@ -393,6 +522,7 @@ export class Pool extends EventEmitter {
|
|
|
393
522
|
item.acquiredNode = this._acquiredResources.tail;
|
|
394
523
|
}
|
|
395
524
|
}
|
|
525
|
+
/** Removes `item` from whichever tracked list (idle/acquired) its current `state` puts it in. */
|
|
396
526
|
_itemDetach(item) {
|
|
397
527
|
switch (item.state) {
|
|
398
528
|
case ResourceState.IDLE:
|
|
@@ -413,6 +543,12 @@ export class Pool extends EventEmitter {
|
|
|
413
543
|
break;
|
|
414
544
|
}
|
|
415
545
|
}
|
|
546
|
+
/**
|
|
547
|
+
* Runs `factory.reset()` (if the item was acquired and a `reset` is
|
|
548
|
+
* provided), then moves `item` to the idle list (FIFO or LIFO end per
|
|
549
|
+
* `fifo`) and processes the next queued request. Destroys the item
|
|
550
|
+
* instead if `reset()` fails.
|
|
551
|
+
*/
|
|
416
552
|
_itemSetIdle(item, callback) {
|
|
417
553
|
const isAcquired = item.state === ResourceState.ACQUIRED;
|
|
418
554
|
const handleCallback = (err) => {
|
|
@@ -439,7 +575,7 @@ export class Pool extends EventEmitter {
|
|
|
439
575
|
if (isAcquired && this._factory.reset) {
|
|
440
576
|
try {
|
|
441
577
|
const o = this._factory.reset(item.resource);
|
|
442
|
-
|
|
578
|
+
awaitResult(o, handleCallback);
|
|
443
579
|
}
|
|
444
580
|
catch (e) {
|
|
445
581
|
handleCallback(e);
|
|
@@ -448,6 +584,7 @@ export class Pool extends EventEmitter {
|
|
|
448
584
|
else
|
|
449
585
|
handleCallback();
|
|
450
586
|
}
|
|
587
|
+
/** Detaches `item`, runs `factory.destroy()`, and removes it from the pool entirely. */
|
|
451
588
|
_itemDestroy(item, callback) {
|
|
452
589
|
this._itemDetach(item);
|
|
453
590
|
const handleCallback = (err) => {
|
|
@@ -462,7 +599,7 @@ export class Pool extends EventEmitter {
|
|
|
462
599
|
};
|
|
463
600
|
try {
|
|
464
601
|
const o = this._factory.destroy(item.resource);
|
|
465
|
-
|
|
602
|
+
awaitResult(o, handleCallback);
|
|
466
603
|
}
|
|
467
604
|
catch (e) {
|
|
468
605
|
handleCallback(e);
|
|
@@ -471,12 +608,12 @@ export class Pool extends EventEmitter {
|
|
|
471
608
|
this._processNextRequest();
|
|
472
609
|
}
|
|
473
610
|
}
|
|
611
|
+
/** Runs `factory.validate()` on `item` before it's handed out to a caller. */
|
|
474
612
|
_itemValidate(item, callback) {
|
|
475
613
|
item.state = ResourceState.VALIDATION;
|
|
476
614
|
try {
|
|
477
615
|
const o = this._factory.validate?.(item.resource);
|
|
478
|
-
|
|
479
|
-
promisify.await(o, callback);
|
|
616
|
+
awaitResult(o, callback);
|
|
480
617
|
}
|
|
481
618
|
catch (e) {
|
|
482
619
|
callback?.(e);
|
package/resource-item.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import type { DoublyLinked } from 'doublylinked';
|
|
2
2
|
import { ResourceState } from './constants.js';
|
|
3
|
+
/**
|
|
4
|
+
* Internal wrapper `Pool` keeps around each resource returned by
|
|
5
|
+
* `factory.create()`, tracking its state and its node in whichever
|
|
6
|
+
* (idle/acquired) list it currently belongs to. Not part of the public API.
|
|
7
|
+
*/
|
|
3
8
|
export declare class ResourceItem<T> {
|
|
4
9
|
state: ResourceState;
|
|
10
|
+
/** This item's node in the acquired list, while `state` is `ACQUIRED`/`VALIDATION`. */
|
|
5
11
|
acquiredNode?: DoublyLinked.Node<ResourceItem<T>>;
|
|
12
|
+
/** This item's node in the idle list, while `state` is `IDLE`. */
|
|
6
13
|
idleNode?: DoublyLinked.Node<ResourceItem<T>>;
|
|
14
|
+
/** The actual resource, as returned by `factory.create()`. */
|
|
7
15
|
resource: T;
|
|
16
|
+
/** `Date.now()` when this item became idle; `0` while acquired/being validated. */
|
|
8
17
|
idleTime: number;
|
|
18
|
+
/** Set once `factory.destroy()` has been called for this item. */
|
|
9
19
|
destroyed: boolean;
|
|
10
20
|
constructor(resource: T);
|
|
11
21
|
}
|
package/resource-item.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { ResourceState } from './constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* Internal wrapper `Pool` keeps around each resource returned by
|
|
4
|
+
* `factory.create()`, tracking its state and its node in whichever
|
|
5
|
+
* (idle/acquired) list it currently belongs to. Not part of the public API.
|
|
6
|
+
*/
|
|
2
7
|
export class ResourceItem {
|
|
3
8
|
state = ResourceState.IDLE;
|
|
9
|
+
/** This item's node in the acquired list, while `state` is `ACQUIRED`/`VALIDATION`. */
|
|
4
10
|
acquiredNode;
|
|
11
|
+
/** This item's node in the idle list, while `state` is `IDLE`. */
|
|
5
12
|
idleNode;
|
|
13
|
+
/** The actual resource, as returned by `factory.create()`. */
|
|
6
14
|
resource;
|
|
15
|
+
/** `Date.now()` when this item became idle; `0` while acquired/being validated. */
|
|
7
16
|
idleTime = 0;
|
|
17
|
+
/** Set once `factory.destroy()` has been called for this item. */
|
|
8
18
|
destroyed = false;
|
|
9
19
|
constructor(resource) {
|
|
10
20
|
this.resource = resource;
|
package/types.d.ts
CHANGED
|
@@ -1,23 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback signature used throughout `Pool`'s non-Promise API (e.g.
|
|
3
|
+
* `acquire(callback)`, `release(resource, callback)`).
|
|
4
|
+
*
|
|
5
|
+
* @param e - The error, if the operation failed; `undefined` on success.
|
|
6
|
+
* @param args - Operation-specific result values (e.g. the acquired resource).
|
|
7
|
+
*/
|
|
1
8
|
export type Callback = (e?: unknown, ...args: any[]) => void;
|
|
9
|
+
/**
|
|
10
|
+
* The object a `Pool` uses to create, destroy, reset, and validate the
|
|
11
|
+
* resources it manages. Passed to `createPool()`/`new Pool()`.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T - The resource type this factory produces (e.g. a DB connection).
|
|
14
|
+
*/
|
|
2
15
|
export interface PoolFactory<T = any> {
|
|
16
|
+
/**
|
|
17
|
+
* Called when the `Pool` needs a new resource.
|
|
18
|
+
*
|
|
19
|
+
* @param info - Populated by the `Pool` itself on retry attempts (see
|
|
20
|
+
* `PoolConfiguration.acquireMaxRetries`) - not something a caller of
|
|
21
|
+
* `acquire()` passes in. `info.tries` is the number of attempts made so
|
|
22
|
+
* far for this resource; `info.maxRetries` mirrors `acquireMaxRetries`.
|
|
23
|
+
* @returns The new resource, or a `Promise` that resolves to it. Throwing
|
|
24
|
+
* (or rejecting with) an `AbortError` skips any remaining retries and
|
|
25
|
+
* fails the pending `acquire()` immediately.
|
|
26
|
+
*/
|
|
3
27
|
create(info?: {
|
|
4
28
|
tries: number;
|
|
5
29
|
maxRetries: number;
|
|
6
30
|
}): Promise<T> | T;
|
|
31
|
+
/**
|
|
32
|
+
* Called when the `Pool` wants to destroy a `resource` - on eviction
|
|
33
|
+
* (`idleTimeoutMillis`), on `pool.destroy()`/`destroyAsync()`, on
|
|
34
|
+
* `reset`/`validate` failure, and when the `Pool` itself closes.
|
|
35
|
+
*
|
|
36
|
+
* @param resource - The resource to destroy, as returned by `create()`.
|
|
37
|
+
*/
|
|
7
38
|
destroy(resource: T): Promise<void> | void;
|
|
39
|
+
/**
|
|
40
|
+
* Called before a `resource` is returned to the idle pool after being
|
|
41
|
+
* released (see `Pool.release()`/`releaseAsync()`). Optional - if
|
|
42
|
+
* omitted, released resources go straight back to idle.
|
|
43
|
+
*
|
|
44
|
+
* @param resource - The resource being released, as returned by `create()`.
|
|
45
|
+
* @remarks If this throws, rejects, or its returned `Promise` rejects,
|
|
46
|
+
* the `Pool` destroys and removes the resource instead of returning it
|
|
47
|
+
* to the idle pool.
|
|
48
|
+
*/
|
|
8
49
|
reset?(resource: T): Promise<void> | void;
|
|
50
|
+
/**
|
|
51
|
+
* Called to validate an idle `resource` before handing it out to an
|
|
52
|
+
* `acquire()` caller (see `PoolConfiguration.validation`). Optional - if
|
|
53
|
+
* omitted (or `validation` is `false`), resources are never validated.
|
|
54
|
+
*
|
|
55
|
+
* @param resource - The idle resource being considered for borrow.
|
|
56
|
+
* @remarks If this throws, rejects, or resolves to `false`, the `Pool`
|
|
57
|
+
* destroys and removes the resource and tries the next idle one (or
|
|
58
|
+
* creates a new one) instead.
|
|
59
|
+
*/
|
|
9
60
|
validate?(resource: T): Promise<void | boolean> | void;
|
|
10
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Options accepted by `createPool()`/`new Pool()`. Every option is also
|
|
64
|
+
* available as a live get/set property on `pool.options` after
|
|
65
|
+
* construction - see `PoolOptions`.
|
|
66
|
+
*/
|
|
11
67
|
export interface PoolConfiguration {
|
|
68
|
+
/**
|
|
69
|
+
* Maximum number of times the `Pool` will retry creating a resource
|
|
70
|
+
* before giving up and returning the error to the caller.
|
|
71
|
+
*
|
|
72
|
+
* @defaultValue `0` - fail on the first error, no retries.
|
|
73
|
+
*/
|
|
12
74
|
acquireMaxRetries?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Time in milliseconds the `Pool` waits between retry attempts (see
|
|
77
|
+
* `acquireMaxRetries`).
|
|
78
|
+
*
|
|
79
|
+
* @defaultValue `2000`
|
|
80
|
+
*/
|
|
13
81
|
acquireRetryWait?: number;
|
|
82
|
+
/**
|
|
83
|
+
* Time in milliseconds an `acquire()` call will wait for a resource
|
|
84
|
+
* before failing with a timeout error.
|
|
85
|
+
*
|
|
86
|
+
* @defaultValue `0` - no timeout.
|
|
87
|
+
*/
|
|
14
88
|
acquireTimeoutMillis?: number;
|
|
89
|
+
/**
|
|
90
|
+
* If `true`, idle resources are handed out in first-in-first-out order
|
|
91
|
+
* (the longest-idle resource first). If `false`, last-in-first-out (the
|
|
92
|
+
* most recently released resource first).
|
|
93
|
+
*
|
|
94
|
+
* @defaultValue `true`
|
|
95
|
+
*/
|
|
15
96
|
fifo?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* The minimum amount of time in milliseconds a resource may sit idle in
|
|
99
|
+
* the `Pool` before the housekeeper is allowed to destroy it (subject to
|
|
100
|
+
* `min`/`minIdle`).
|
|
101
|
+
*
|
|
102
|
+
* @defaultValue `30000`
|
|
103
|
+
*/
|
|
16
104
|
idleTimeoutMillis?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Time in milliseconds between housekeeping passes, which enforce
|
|
107
|
+
* `idleTimeoutMillis` and `min`/`minIdle`.
|
|
108
|
+
*
|
|
109
|
+
* @defaultValue `1000`
|
|
110
|
+
*/
|
|
17
111
|
houseKeepInterval?: number;
|
|
112
|
+
/**
|
|
113
|
+
* Minimum number of resources the `Pool` tries to keep alive in total.
|
|
114
|
+
*
|
|
115
|
+
* @defaultValue `0`
|
|
116
|
+
*/
|
|
18
117
|
min?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Minimum number of resources the `Pool` tries to keep idle (immediately
|
|
120
|
+
* available).
|
|
121
|
+
*
|
|
122
|
+
* @defaultValue `0`
|
|
123
|
+
*/
|
|
19
124
|
minIdle?: number;
|
|
125
|
+
/**
|
|
126
|
+
* Maximum number of resources the `Pool` will create.
|
|
127
|
+
*
|
|
128
|
+
* @defaultValue `10`
|
|
129
|
+
*/
|
|
20
130
|
max?: number;
|
|
131
|
+
/**
|
|
132
|
+
* Maximum number of `acquire()` requests that may be queued/pending at
|
|
133
|
+
* once; further requests fail immediately with an error instead of
|
|
134
|
+
* waiting.
|
|
135
|
+
*
|
|
136
|
+
* @defaultValue `1000`
|
|
137
|
+
*/
|
|
21
138
|
maxQueue?: number;
|
|
139
|
+
/**
|
|
140
|
+
* If `true`, the `Pool` calls `factory.validate()` on a resource before
|
|
141
|
+
* handing it out (when the factory provides one). If `false`,
|
|
142
|
+
* `validate()` is never called.
|
|
143
|
+
*
|
|
144
|
+
* @defaultValue `true`
|
|
145
|
+
*/
|
|
22
146
|
validation?: boolean;
|
|
23
147
|
}
|