lightning-pool 4.14.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 +4 -4
- 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 +72 -0
- package/pool-request.d.ts +15 -0
- package/pool-request.js +17 -0
- package/pool.d.ts +125 -10
- package/pool.js +107 -8
- package/resource-item.d.ts +10 -0
- package/resource-item.js +10 -0
- package/types.d.ts +124 -0
- package/CHANGELOG.md +0 -451
package/pool.d.ts
CHANGED
|
@@ -2,6 +2,12 @@ import { EventEmitter } from 'events';
|
|
|
2
2
|
import { PoolState } from './constants.js';
|
|
3
3
|
import { PoolOptions } from './pool-options.js';
|
|
4
4
|
import type { Callback, PoolConfiguration, PoolFactory } from './types.js';
|
|
5
|
+
/**
|
|
6
|
+
* A generic resource pool: acquires, reuses, and (when idle too long or on
|
|
7
|
+
* validation failure) discards resources created by a `PoolFactory`.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The resource type this pool manages (e.g. a DB connection).
|
|
10
|
+
*/
|
|
5
11
|
export declare class Pool<T = any> extends EventEmitter {
|
|
6
12
|
private readonly _options;
|
|
7
13
|
private readonly _factory;
|
|
@@ -14,6 +20,12 @@ export declare class Pool<T = any> extends EventEmitter {
|
|
|
14
20
|
private _state;
|
|
15
21
|
private _houseKeepTimer;
|
|
16
22
|
private _closeWaitTimer;
|
|
23
|
+
/**
|
|
24
|
+
* @param factory - Creates, destroys, and (optionally) resets/validates resources.
|
|
25
|
+
* @param config - Initial options; every option can also be changed later via `pool.options`.
|
|
26
|
+
* @throws \{@link TypeError\} If `factory` isn't an object, or `create`/`destroy` aren't
|
|
27
|
+
* functions, or `reset`/`validate` are present but aren't functions.
|
|
28
|
+
*/
|
|
17
29
|
constructor(factory: PoolFactory<T>, config?: PoolConfiguration);
|
|
18
30
|
/**
|
|
19
31
|
* Returns Pool options
|
|
@@ -47,59 +59,162 @@ export declare class Pool<T = any> extends EventEmitter {
|
|
|
47
59
|
/**
|
|
48
60
|
* Starts the pool and begins creating of resources, starts the housekeeper and any other internal logic.
|
|
49
61
|
* Note: This method is not needed to be called. Pool instance will automatically be started when acquire() method is called
|
|
62
|
+
*
|
|
63
|
+
* @throws \{@link Error\} If the pool is currently `CLOSING`.
|
|
50
64
|
*/
|
|
51
65
|
start(): void;
|
|
52
66
|
/**
|
|
53
|
-
* Shuts down the pool and destroys all resources.
|
|
67
|
+
* Shuts down the pool and destroys all resources. Any `acquire()` call
|
|
68
|
+
* still queued at the time this is invoked is rejected with an error.
|
|
69
|
+
*
|
|
70
|
+
* @returns A `Promise` that resolves once fully closed (only when no `callback` is given).
|
|
71
|
+
*/
|
|
72
|
+
close(): Promise<void>;
|
|
73
|
+
/** @param callback - Called once the pool has fully closed. */
|
|
74
|
+
close(callback: Callback): void;
|
|
75
|
+
/**
|
|
76
|
+
* @param terminateWait - How long, in milliseconds, to wait for acquired
|
|
77
|
+
* resources to be released before forcibly destroying them anyway. Waits
|
|
78
|
+
* indefinitely if omitted.
|
|
79
|
+
* @param callback - Called once the pool has fully closed; if omitted, returns a `Promise` instead.
|
|
54
80
|
*/
|
|
55
|
-
close(): void;
|
|
56
|
-
close(callback?: Callback): void;
|
|
57
81
|
close(terminateWait?: number, callback?: Callback): void;
|
|
82
|
+
/**
|
|
83
|
+
* @param force - `true` is shorthand for `terminateWait: 0` (destroy
|
|
84
|
+
* acquired resources immediately, without waiting); `false` is shorthand
|
|
85
|
+
* for waiting indefinitely.
|
|
86
|
+
* @param callback - Called once the pool has fully closed; if omitted, returns a `Promise` instead.
|
|
87
|
+
*/
|
|
58
88
|
close(force?: boolean, callback?: Callback): void;
|
|
89
|
+
/** Promise-returning equivalent of `close()` - see it for what each overload does. */
|
|
59
90
|
closeAsync(): Promise<void>;
|
|
60
91
|
closeAsync(terminateWait?: number): Promise<void>;
|
|
61
92
|
closeAsync(force?: boolean): Promise<void>;
|
|
62
93
|
/**
|
|
63
|
-
* Acquires
|
|
94
|
+
* Acquires a resource from the pool, or creates a new one if none is idle.
|
|
95
|
+
* Starts the pool (see `start()`) if it hasn't been already.
|
|
96
|
+
*
|
|
97
|
+
* @returns A `Promise` resolving to the resource (only when no `callback` is given).
|
|
98
|
+
* @throws \{@link Error\} (via the `Promise` rejection or `callback`) If the pool is
|
|
99
|
+
* `CLOSING`, if `maxQueue` is exceeded, or if resource creation ultimately fails.
|
|
64
100
|
*/
|
|
65
101
|
acquire(): Promise<T>;
|
|
102
|
+
/** @param callback - Called with the acquired resource, or an error. */
|
|
66
103
|
acquire(callback: Callback): void;
|
|
67
104
|
/**
|
|
68
|
-
* Releases
|
|
105
|
+
* Releases a previously-acquired `resource` back to the pool so it can be
|
|
106
|
+
* reused (running `factory.reset()` first, if provided).
|
|
107
|
+
*
|
|
108
|
+
* @param resource - A previously acquired resource. A no-op (aside from invoking
|
|
109
|
+
* `callback`) if it isn't currently acquired or isn't in this pool.
|
|
110
|
+
* @param callback - Called once released (and `reset()`, if any, has finished).
|
|
111
|
+
* @remarks Returns immediately (`undefined`) regardless of `callback` -
|
|
112
|
+
* it does not wait for `reset()` to finish. Use `releaseAsync()` (or pass
|
|
113
|
+
* `callback`) to know when the release has actually completed.
|
|
69
114
|
*/
|
|
70
115
|
release(resource: T, callback?: Callback): void;
|
|
71
116
|
/**
|
|
72
|
-
*
|
|
117
|
+
* Promise-returning equivalent of `release()`.
|
|
118
|
+
*
|
|
119
|
+
* @param resource - A previously acquired resource.
|
|
120
|
+
* @returns A `Promise` that resolves once released (and `reset()`, if any, has finished).
|
|
73
121
|
*/
|
|
74
122
|
releaseAsync(resource: T): Promise<void>;
|
|
75
123
|
/**
|
|
76
|
-
* Releases, destroys and removes
|
|
124
|
+
* Releases, destroys, and removes a `resource` from the pool entirely -
|
|
125
|
+
* it will not be reused (running `factory.destroy()`).
|
|
126
|
+
*
|
|
127
|
+
* @param resource - A previously acquired (or idle) resource. A no-op
|
|
128
|
+
* (aside from invoking `callback`) if it isn't in this pool.
|
|
129
|
+
* @param callback - Called once destroyed (or on `factory.destroy()` failure).
|
|
77
130
|
*/
|
|
78
131
|
destroy(resource: T, callback?: Callback): any;
|
|
79
132
|
/**
|
|
80
|
-
*
|
|
133
|
+
* Promise-returning equivalent of `destroy()`.
|
|
134
|
+
*
|
|
135
|
+
* @param resource - A previously acquired (or idle) resource.
|
|
136
|
+
* @returns A `Promise` that resolves once destroyed.
|
|
81
137
|
*/
|
|
82
138
|
destroyAsync(resource: T): Promise<void>;
|
|
83
139
|
/**
|
|
84
140
|
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
141
|
+
*
|
|
142
|
+
* @param resource - The resource to check.
|
|
85
143
|
*/
|
|
86
144
|
isAcquired(resource: T): boolean;
|
|
87
145
|
/**
|
|
88
|
-
* Returns if the pool contains a `resource`
|
|
146
|
+
* Returns if the pool contains a `resource` - acquired, idle, or being
|
|
147
|
+
* validated (not yet destroyed).
|
|
148
|
+
*
|
|
149
|
+
* @param resource - The resource to check.
|
|
89
150
|
*/
|
|
90
151
|
includes(resource: T): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Serves the next queued `acquire()` request, if any and if capacity
|
|
154
|
+
* allows: from an idle resource (validating it first if enabled), or by
|
|
155
|
+
* creating a new one. Called after anything that could free up capacity
|
|
156
|
+
* or add a request (release, destroy, acquire, options change).
|
|
157
|
+
*/
|
|
91
158
|
private _processNextRequest;
|
|
159
|
+
/**
|
|
160
|
+
* Overrides `EventEmitter.emit()`: identical, except an error thrown by a
|
|
161
|
+
* listener is swallowed instead of propagating out of `emit()` - a
|
|
162
|
+
* misbehaving listener on, say, `'acquire'` can't take down the internal
|
|
163
|
+
* code path that just emitted it.
|
|
164
|
+
*
|
|
165
|
+
* @param event - Event name. See the class-level events list in `docs/API.md`
|
|
166
|
+
* (`start`, `closing`, `close`, `terminate`, `create`, `error`, `acquire`,
|
|
167
|
+
* `return`, `destroy`, `destroy-error`, `validate-error`, `request-timeout`).
|
|
168
|
+
* @param args - Arguments passed to each listener.
|
|
169
|
+
* @returns `true` if the event had listeners, `false` otherwise (same as
|
|
170
|
+
* `EventEmitter.emit()`); also `true` if a listener threw.
|
|
171
|
+
*/
|
|
92
172
|
emit(event: string | symbol, ...args: any[]): boolean;
|
|
93
173
|
/**
|
|
94
|
-
* Creates new resource
|
|
174
|
+
* Creates a new resource via `factory.create()`, retrying on failure up
|
|
175
|
+
* to `acquireMaxRetries` times (waiting `acquireRetryWait` between
|
|
176
|
+
* attempts), unless `request` has already timed out or the factory threw
|
|
177
|
+
* an `AbortError`. On success, adds the resource to the pool as idle.
|
|
178
|
+
*
|
|
179
|
+
* @param request - The acquire request this creation is for, if any (`_ensureMin()`
|
|
180
|
+
* creates resources with no associated request).
|
|
181
|
+
* @param callback - Called with the new `ResourceItem`, or the final error.
|
|
95
182
|
*/
|
|
96
183
|
private _createResource;
|
|
184
|
+
/**
|
|
185
|
+
* (Re)starts the housekeeper timer at interval `ms`, or leaves it stopped
|
|
186
|
+
* if the pool isn't `STARTED`/`CLOSING`. Called on `start()`, whenever
|
|
187
|
+
* `houseKeepInterval` changes, and (with a short fixed interval) while
|
|
188
|
+
* closing.
|
|
189
|
+
*/
|
|
97
190
|
private _setHouseKeep;
|
|
191
|
+
/**
|
|
192
|
+
* One housekeeping pass: destroys idle resources past `idleTimeoutMillis`
|
|
193
|
+
* (as long as doing so doesn't drop below `min`/`minIdle`), or - while
|
|
194
|
+
* `CLOSING` - destroys every idle resource unconditionally and, once no
|
|
195
|
+
* resources remain at all, finishes the transition to `CLOSED`.
|
|
196
|
+
*/
|
|
98
197
|
private _houseKeep;
|
|
198
|
+
/**
|
|
199
|
+
* Schedules (via `process.nextTick`) creating however many resources are
|
|
200
|
+
* needed to satisfy `min`/`minIdle`, accounting for creations already in
|
|
201
|
+
* flight. Called after start, after every successful acquire, and
|
|
202
|
+
* whenever `min`/`minIdle` change.
|
|
203
|
+
*/
|
|
99
204
|
private _ensureMin;
|
|
205
|
+
/** Moves `item` into the acquired list, detaching it from wherever it was first. */
|
|
100
206
|
private _itemSetAcquired;
|
|
207
|
+
/** Removes `item` from whichever tracked list (idle/acquired) its current `state` puts it in. */
|
|
101
208
|
private _itemDetach;
|
|
209
|
+
/**
|
|
210
|
+
* Runs `factory.reset()` (if the item was acquired and a `reset` is
|
|
211
|
+
* provided), then moves `item` to the idle list (FIFO or LIFO end per
|
|
212
|
+
* `fifo`) and processes the next queued request. Destroys the item
|
|
213
|
+
* instead if `reset()` fails.
|
|
214
|
+
*/
|
|
102
215
|
private _itemSetIdle;
|
|
216
|
+
/** Detaches `item`, runs `factory.destroy()`, and removes it from the pool entirely. */
|
|
103
217
|
private _itemDestroy;
|
|
218
|
+
/** Runs `factory.validate()` on `item` before it's handed out to a caller. */
|
|
104
219
|
private _itemValidate;
|
|
105
220
|
}
|
package/pool.js
CHANGED
|
@@ -21,6 +21,12 @@ function awaitResult(value, callback) {
|
|
|
21
21
|
else if (callback)
|
|
22
22
|
callback(undefined, value);
|
|
23
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
|
+
*/
|
|
24
30
|
export class Pool extends EventEmitter {
|
|
25
31
|
_options;
|
|
26
32
|
_factory;
|
|
@@ -33,6 +39,12 @@ export class Pool extends EventEmitter {
|
|
|
33
39
|
_state = PoolState.IDLE;
|
|
34
40
|
_houseKeepTimer;
|
|
35
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
|
+
*/
|
|
36
48
|
constructor(factory, config) {
|
|
37
49
|
super();
|
|
38
50
|
if (typeof factory !== 'object') {
|
|
@@ -107,6 +119,8 @@ export class Pool extends EventEmitter {
|
|
|
107
119
|
/**
|
|
108
120
|
* Starts the pool and begins creating of resources, starts the housekeeper and any other internal logic.
|
|
109
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`.
|
|
110
124
|
*/
|
|
111
125
|
start() {
|
|
112
126
|
if (this._state === PoolState.STARTED)
|
|
@@ -202,11 +216,23 @@ export class Pool extends EventEmitter {
|
|
|
202
216
|
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
203
217
|
return callback(new Error('Pool queue is full'));
|
|
204
218
|
}
|
|
205
|
-
|
|
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);
|
|
206
224
|
this._processNextRequest();
|
|
207
225
|
}
|
|
208
226
|
/**
|
|
209
|
-
* 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.
|
|
210
236
|
*/
|
|
211
237
|
release(resource, callback) {
|
|
212
238
|
const item = this._allResources.get(resource);
|
|
@@ -218,13 +244,21 @@ export class Pool extends EventEmitter {
|
|
|
218
244
|
this._processNextRequest();
|
|
219
245
|
}
|
|
220
246
|
/**
|
|
221
|
-
*
|
|
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).
|
|
222
251
|
*/
|
|
223
252
|
releaseAsync(resource) {
|
|
224
253
|
return promisify.fromCallback(cb => this.release(resource, cb));
|
|
225
254
|
}
|
|
226
255
|
/**
|
|
227
|
-
* 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).
|
|
228
262
|
*/
|
|
229
263
|
destroy(resource, callback) {
|
|
230
264
|
try {
|
|
@@ -239,24 +273,38 @@ export class Pool extends EventEmitter {
|
|
|
239
273
|
}
|
|
240
274
|
}
|
|
241
275
|
/**
|
|
242
|
-
*
|
|
276
|
+
* Promise-returning equivalent of `destroy()`.
|
|
277
|
+
*
|
|
278
|
+
* @param resource - A previously acquired (or idle) resource.
|
|
279
|
+
* @returns A `Promise` that resolves once destroyed.
|
|
243
280
|
*/
|
|
244
281
|
destroyAsync(resource) {
|
|
245
282
|
return promisify.fromCallback(cb => this.destroy(resource, cb));
|
|
246
283
|
}
|
|
247
284
|
/**
|
|
248
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.
|
|
249
288
|
*/
|
|
250
289
|
isAcquired(resource) {
|
|
251
290
|
const item = this._allResources.get(resource);
|
|
252
291
|
return !!(item && item.acquiredNode);
|
|
253
292
|
}
|
|
254
293
|
/**
|
|
255
|
-
* 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.
|
|
256
298
|
*/
|
|
257
299
|
includes(resource) {
|
|
258
300
|
return this._allResources.has(resource);
|
|
259
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
|
+
*/
|
|
260
308
|
_processNextRequest() {
|
|
261
309
|
if (this._state !== PoolState.STARTED ||
|
|
262
310
|
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
@@ -306,7 +354,10 @@ export class Pool extends EventEmitter {
|
|
|
306
354
|
this._itemDestroy(item);
|
|
307
355
|
this.emit('validate-error', err, item.resource);
|
|
308
356
|
this._requestsProcessing--;
|
|
309
|
-
|
|
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);
|
|
310
361
|
this._processNextRequest();
|
|
311
362
|
}
|
|
312
363
|
else
|
|
@@ -319,6 +370,19 @@ export class Pool extends EventEmitter {
|
|
|
319
370
|
/** There is no idle resource. We need to create new one **/
|
|
320
371
|
this._createResource(request, handleCallback);
|
|
321
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
|
+
*/
|
|
322
386
|
emit(event, ...args) {
|
|
323
387
|
// Prevents errors while calling emit()
|
|
324
388
|
try {
|
|
@@ -329,7 +393,14 @@ export class Pool extends EventEmitter {
|
|
|
329
393
|
}
|
|
330
394
|
}
|
|
331
395
|
/**
|
|
332
|
-
* 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.
|
|
333
404
|
*/
|
|
334
405
|
_createResource(request, callback) {
|
|
335
406
|
const maxRetries = this.options.acquireMaxRetries;
|
|
@@ -378,6 +449,12 @@ export class Pool extends EventEmitter {
|
|
|
378
449
|
};
|
|
379
450
|
tryCreate();
|
|
380
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
|
+
*/
|
|
381
458
|
_setHouseKeep(ms) {
|
|
382
459
|
if (this._houseKeepTimer)
|
|
383
460
|
clearInterval(this._houseKeepTimer);
|
|
@@ -387,6 +464,12 @@ export class Pool extends EventEmitter {
|
|
|
387
464
|
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
388
465
|
}
|
|
389
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
|
+
*/
|
|
390
473
|
_houseKeep() {
|
|
391
474
|
const isClosing = this._state === PoolState.CLOSING;
|
|
392
475
|
const now = Date.now();
|
|
@@ -411,6 +494,12 @@ export class Pool extends EventEmitter {
|
|
|
411
494
|
this.emit('close');
|
|
412
495
|
}
|
|
413
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
|
+
*/
|
|
414
503
|
_ensureMin() {
|
|
415
504
|
// Common case (min/minIdle both unset): the scheduled tick below would
|
|
416
505
|
// always compute k <= 0 and do nothing, so skip the nextTick() and its
|
|
@@ -424,6 +513,7 @@ export class Pool extends EventEmitter {
|
|
|
424
513
|
this._createResource();
|
|
425
514
|
});
|
|
426
515
|
}
|
|
516
|
+
/** Moves `item` into the acquired list, detaching it from wherever it was first. */
|
|
427
517
|
_itemSetAcquired(item) {
|
|
428
518
|
if (item.state !== ResourceState.ACQUIRED) {
|
|
429
519
|
this._itemDetach(item);
|
|
@@ -432,6 +522,7 @@ export class Pool extends EventEmitter {
|
|
|
432
522
|
item.acquiredNode = this._acquiredResources.tail;
|
|
433
523
|
}
|
|
434
524
|
}
|
|
525
|
+
/** Removes `item` from whichever tracked list (idle/acquired) its current `state` puts it in. */
|
|
435
526
|
_itemDetach(item) {
|
|
436
527
|
switch (item.state) {
|
|
437
528
|
case ResourceState.IDLE:
|
|
@@ -452,6 +543,12 @@ export class Pool extends EventEmitter {
|
|
|
452
543
|
break;
|
|
453
544
|
}
|
|
454
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
|
+
*/
|
|
455
552
|
_itemSetIdle(item, callback) {
|
|
456
553
|
const isAcquired = item.state === ResourceState.ACQUIRED;
|
|
457
554
|
const handleCallback = (err) => {
|
|
@@ -487,6 +584,7 @@ export class Pool extends EventEmitter {
|
|
|
487
584
|
else
|
|
488
585
|
handleCallback();
|
|
489
586
|
}
|
|
587
|
+
/** Detaches `item`, runs `factory.destroy()`, and removes it from the pool entirely. */
|
|
490
588
|
_itemDestroy(item, callback) {
|
|
491
589
|
this._itemDetach(item);
|
|
492
590
|
const handleCallback = (err) => {
|
|
@@ -510,6 +608,7 @@ export class Pool extends EventEmitter {
|
|
|
510
608
|
this._processNextRequest();
|
|
511
609
|
}
|
|
512
610
|
}
|
|
611
|
+
/** Runs `factory.validate()` on `item` before it's handed out to a caller. */
|
|
513
612
|
_itemValidate(item, callback) {
|
|
514
613
|
item.state = ResourceState.VALIDATION;
|
|
515
614
|
try {
|
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
|
}
|