lightning-pool 4.13.0 → 4.14.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/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  ## Changelog
2
2
 
3
- ### [v4.13.0](https://github.com/panates/lightning-pool/compare/v4.12.0...v4.13.0) -
3
+ ### [v4.14.0](https://github.com/panates/lightning-pool/compare/v4.13.0...v4.14.0) -
4
+
5
+ #### 🚀 New Features
6
+
7
+ - feat: Replace benchmark-tests with a proper benchmark suite @Eray Hanoğlu
8
+
9
+ #### 🪲 Fixes
10
+
11
+ - fix: Correct release/close/ensureMin bugs and cut hot-path allocations in Pool @Eray Hanoğlu
12
+
13
+ #### 📖 Documentation Changes
14
+
15
+ - docs: Split API reference into doc/API.md, add generated doc/BENCHMARKS.md @Eray Hanoğlu
16
+ - docs: Add CLAUDE.md with graphify usage guidelines @Eray Hanoğlu
17
+
18
+ ### [v4.13.0](https://github.com/panates/lightning-pool/compare/v4.12.0...v4.13.0) - 27 August 2026
4
19
 
5
20
  ### [v4.12.0](https://github.com/panates/lightning-pool/compare/v4.11.1...v4.12.0) - 4 December 2025
6
21
 
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  High performance resource pool written with TypeScript.
11
11
 
12
- - The fastest Resource Pool implementation for JavaScript ever! Check out [benchmark](BANCHMARK.md) results
12
+ - A fast Resource Pool implementation for JavaScript - see [doc/BENCHMARKS.md](doc/BENCHMARKS.md) for the numbers against [generic-pool](https://github.com/coopernurse/node-pool), or [benchmark/README.md](benchmark/README.md) to reproduce them yourself
13
13
  - Advanced configuration options, suits for enterprise level applications
14
14
  - Configuration can be changed while pool running
15
15
  - Promise based factory supported
@@ -65,7 +65,7 @@ const client = await pool.acquire();
65
65
  // Use resource
66
66
  await client.query("select * from foo");
67
67
  // return object back to pool
68
- await pool.release(client);
68
+ await pool.releaseAsync(client);
69
69
 
70
70
  /**
71
71
  * Step 4 - Shutdown pool (optional)
@@ -78,295 +78,8 @@ await pool.close(5000);
78
78
 
79
79
  ## Documentation
80
80
 
81
- ### Creating a `Pool` instance
82
-
83
- lightning-pool module exports createPool() method and Pool class. Both can be used to instantiate a Pool.
84
-
85
- ```ts
86
- import {createPool} from 'lightning-pool';
87
- const pool = createPool(factory, options);
88
- ```
89
-
90
- ```ts
91
- import {Pool} from 'lightning-pool';
92
- const pool = new Pool(factory, options);
93
- ```
94
-
95
- #### factory
96
-
97
- Can be any object/instance with the following properties:
98
-
99
- - `create` : The function that the `Pool` will call when it needs a new resource. It should return a `Promise<resouce>`.
100
- - `destroy` : The function that the `Pool` will call when it wants to destroy a `resource`. It should accept first argument as `resource`, where `resource` is whatever factory.create made. It should return a `Promise<>`.
101
- - `reset` (optional) : The function that the `Pool` will call before any `resource` back to the `Pool`. It should accept first argument as `resource`, where `resource` is whatever factory.create made. It should return a `Promise<>`. `Pool` will destroy and remove the resource from the `Pool` on any error.
102
- - `validate` (optional) : The function that the `Pool` will call when any resource needs to be validated. It should accept first argument as `resource`, where `resource` is whatever factory.create made. It should return a `Promise<>`. `Pool` will destroy and remove the resource from the `Pool` on any error.
103
-
104
- #### options
105
- - `acquireMaxRetries`: Maximum number that `Pool` will try to create a resource before returning the error. (Default 0)
106
- - `acquireRetryWait`: Time in millis that `Pool` will wait after each tries. (Default 2000)
107
- - `acquireTimeoutMillis`: Time in millis an acquire call will wait for a resource before timing out. (Default 0 - no limit)
108
- - `fifo`: If true resources will be allocated first-in-first-out order. resources will be allocated last-in-first-out order. (Default true)
109
- - `idleTimeoutMillis`: The minimum amount of time in millis that an `resource` may sit idle in the `Pool`. (Default 30000)
110
- - `houseKeepInterval`: Time period in millis that `Pool` will make a cleanup. (Default 1000)
111
- - `min`: Minimum number of resources that `Pool` will keep. (Default 0)
112
- - `minIdle`: Minimum number of resources that `Pool` will keep in idle state. (Default 0)
113
- - `max`: Maximum number of resources that `Pool` will create. (Default 10)
114
- - `maxQueue`: Maximum number of request that `Pool` will accept. (Default 1000)
115
- - `resetOnReturn`: If true `Pool` will call `reset()` function of factory before moving it idle state. (Default true)
116
- - `validation`: If true `Pool` will call `validation()` function of factory when it needs it. If false, `validation()` never been called. (Default true)
117
-
118
- ### Methods
119
-
120
- #### Pool.prototype.acquire()
121
-
122
- Acquires a `resource` from the `Pool` or create a new one.
123
-
124
- ##### Usage
125
-
126
- `pool.acquire(): Promise<any>`
127
-
128
- `pool.acquire(factoryCreateOptions: any): Promise<any>`
129
-
130
- `pool.acquire(callback:Callback): Promise<any>`
131
-
132
- `pool.acquire(factoryCreateOptions?: any, callback:Callback): Promise<any>`
133
-
134
- - *Returns*: A Promise
135
-
136
-
137
- ```js
138
- var promise = pool.acquire();
139
- promise.then(resource => {
140
- // Do what ever you want with resource
141
- }).catch(err =>{
142
- // Handle Error
143
- });
144
- ```
145
-
146
- #### Pool.prototype.isAcquired()
147
-
148
- Returns if a resource has been acquired from the `Pool` and not yet released or destroyed.
149
-
150
- ##### Usage
151
-
152
- `pool.isAcquired(resource)`
153
-
154
- - `resource`: A previously acquired resource
155
- - *Returns*: True if the resource is acquired, else False
156
-
157
- ```js
158
- if (pool.isAcquired(resource)) {
159
- // Do any thing
160
- }
161
- ```
162
-
163
-
164
-
165
- #### Pool.prototype.includes()
166
-
167
- Returns if the `Pool` contains a resource
168
-
169
- ##### Usage
170
-
171
- `pool.includes(resource)`
172
-
173
- - `resource`: A resource object
174
- - *Returns*: True if the resource is in the `Pool`, else False
175
-
176
- ```js
177
- if (pool.includes(resource)) {
178
- // Do any thing
179
- }
180
- ```
181
-
182
-
183
-
184
- #### Pool.prototype.release()
185
-
186
- Releases an allocated `resource` and let it back to pool.
187
-
188
- ##### Usage
189
-
190
- `pool.release(resource)`
191
-
192
- - `resource`: A previously acquired resource
193
- - *Returns*: undefined
194
-
195
- ```js
196
- pool.release(resource);
197
- ```
198
-
199
-
200
-
201
-
202
- #### Pool.prototype.destroy()
203
-
204
- Releases, destroys and removes any `resource` from `Pool`.
205
-
206
- ##### Usage
207
-
208
- `pool.destroy(resource)`
209
-
210
- - `resource`: A previously acquired resource
211
- - *Returns*: undefined
212
-
213
- ```js
214
- pool.destroy(resource);
215
- ```
216
-
217
-
218
-
219
-
220
- #### Pool.prototype.start()
221
-
222
- Starts the `Pool` and begins creating of resources, starts house keeping and any other internal logic.
223
-
224
- *Note: This method is not need to be called. `Pool` instance will automatically be started when acquire() method is called*
225
-
226
- ##### Usage
227
-
228
- `pool.start()`
229
-
230
- - *Returns*: undefined
231
-
232
-
233
- ```js
234
- pool.start();
235
- ```
236
-
237
-
238
-
239
- #### Pool.prototype.close()
240
-
241
- Shuts down the `Pool` and destroys all resources.
242
-
243
- ##### Usage
244
-
245
- `close(callback: Callback): void;`
246
- `close(terminateWait: number, callback?: Callback): Promise<void>;`
247
- `close(force: boolean, callback?: Callback): void;`
248
-
249
- - `force`: If true, `Pool` will immediately destroy resources instead of waiting to be released
250
- - `terminateWait`: If specified, `Pool` will wait for active resources to release
251
- - `callback`: If specified, callback will be called after close. If not specified a promise returns.
252
-
253
-
254
- ```js
255
- var promise = pool.close();
256
- promise.then(() => {
257
- console.log('Pool has been shut down')
258
- }).catch(err => {
259
- console.error(err);
260
- });
261
- ```
262
-
263
- ### Properties
264
-
265
-
266
- - `acquired` (Number): Returns number of acquired resources.
267
- - `available` (Number): Returns number of idle resources.
268
- - `creating` (Number): Returns number of resources currently been created.
269
- - `pending` (Number): Returns number of acquire request waits in the `Pool` queue.
270
- - `size` (Number): Returns number of total resources.
271
- - `state` (PoolState): Returns current state of the `Pool`.
272
- - `options` (PoolOptions): Returns object instance that holds configuration properties
273
- - `acquireMaxRetries` (Get/Set): Maximum number that `Pool` will try to create a resource before returning the error. (Default 0)
274
- - `acquireRetryWait` (Get/Set): Time in millis that `Pool` will wait after each tries. (Default 2000)
275
- - `acquireTimeoutMillis` (Get/Set): Time in millis an acquire call will wait for a resource before timing out. (Default 0 - no limit)
276
- - `fifo` (Get/Set): If true resources will be allocated first-in-first-out order. resources will be allocated last-in-first-out order. (Default true)
277
- - `idleTimeoutMillis` (Get/Set): The minimum amount of time in millis that an `resource` may sit idle in the `Pool`. (Default 30000)
278
- - `houseKeepInterval` (Get/Set): Time period in millis that `Pool` will make a cleanup. (Default 1000)
279
- - `min` (Get/Set): Minimum number of resources that `Pool` will keep. (Default 0)
280
- - `minIdle` (Get/Set): Minimum number of resources that `Pool` will keep in idle state. (Default 0)
281
- - `max` (Get/Set): Maximum number of resources that `Pool` will create. (Default 10)
282
- - `maxQueue` (Get/Set): Maximum number of request that `Pool` will acceps. (Default 1000)
283
- - `resetOnReturn` (Get/Set): If true `Pool` will call `reset()` function of factory before moving it idle state. (Default true)
284
- - `validation` (Get/Set): If true `Pool` will call `validation()` function of factory when it needs it. If false, `validation()` never been called. (Default true)
285
-
286
- ### Events
287
-
288
- Pool derives from EventEmitter and produce the following events:
289
-
290
- - `acquire`: Emitted when a resource acquired.
291
-
292
- ```js
293
- pool.on('acquire', function(resource){
294
- //....
295
- })
296
- ```
297
-
298
- - `create`: Emitted when a new resource is added to the `Pool`.
299
- ```js
300
- pool.on('create', function(resource){
301
- //....
302
- })
303
- ```
304
-
305
- - `create-error`: Emitted when a factory.create informs any error.
306
- ```js
307
- pool.on('create-error', function(error){
308
- //Log stuff maybe
309
- })
310
- ```
311
-
312
- - `destroy`: Emitted when a resource is destroyed and removed from the `Pool`.
313
- - `destroy-error`: Emitted when a factory.destroy informs any error.
314
- ```js
315
- pool.on('destroy-error', function(error, resource){
316
- //Log stuff maybe
317
- })
318
- ```
319
-
320
- - `return`: Emitted when an acquired resource returns to the `Pool`.
321
- ```js
322
- pool.on('start', function(resource){
323
- //...
324
- })
325
- ```
326
-
327
-
328
- - `start`: Emitted when the `Pool` started.
329
- ```js
330
- pool.on('start', function(){
331
- //...
332
- })
333
- ```
334
-
335
- - `closing`: Emitted when before closing the `Pool`.
336
- ```js
337
- pool.on('closing', function(){
338
- //...
339
- })
340
- ```
341
-
342
-
343
- - `close`: Emitted when after closing the `Pool`.
344
- ```js
345
- pool.on('close', function(){
346
- //...
347
- })
348
- ```
349
-
350
- - `validate-error`: Emitted when a factory.validate informs any error.
351
- ```js
352
- pool.on('validate-error', function(error, resource){
353
- //Log stuff maybe
354
- })
355
- ```
356
-
357
- ## PoolState enum
358
-
359
- Pool.PoolState (Number):
360
-
361
- - IDLE: 0, // Pool has not been started
362
-
363
- - STARTED: 1, // Pool has been started
364
-
365
- - CLOSING: 2, // Pool shutdown in progress
366
-
367
- - CLOSED: 3 // Pool has been shut down
368
-
369
-
81
+ See [doc/API.md](doc/API.md) for the full API reference - factory/options shape, every method and property,
82
+ emitted events, and the `PoolState`/`ResourceState` enums.
370
83
 
371
84
  ## Node Compatibility
372
85
 
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.13.0",
4
+ "version": "4.14.0",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/pool-options.js CHANGED
@@ -116,10 +116,16 @@ export class PoolOptions extends EventEmitter {
116
116
  }
117
117
  assign(values) {
118
118
  const proto = Object.getPrototypeOf(this);
119
- for (const k of Object.keys(values)) {
119
+ // A PoolOptions instance only owns underscored private fields, so its
120
+ // public getter/setter names must be read from the canonical key list.
121
+ const keys = values instanceof PoolOptions
122
+ ? Object.keys(defaultValues)
123
+ : Object.keys(values);
124
+ for (const k of keys) {
120
125
  const desc = Object.getOwnPropertyDescriptor(proto, k);
121
- if (desc && desc.set)
122
- this[k] = values[k];
126
+ const val = values[k];
127
+ if (desc && desc.set && val !== undefined)
128
+ this[k] = val;
123
129
  }
124
130
  }
125
131
  }
package/pool.js CHANGED
@@ -6,6 +6,21 @@ 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
+ }
9
24
  export class Pool extends EventEmitter {
10
25
  _options;
11
26
  _factory;
@@ -137,7 +152,16 @@ export class Pool extends EventEmitter {
137
152
  if (this._houseKeepTimer)
138
153
  clearTimeout(this._houseKeepTimer);
139
154
  this._state = PoolState.CLOSING;
140
- this._requestQueue.forEach(t => t.stopTimout());
155
+ const closingError = new Error('Pool is closing');
156
+ this._requestQueue.forEach(t => {
157
+ t.stopTimout();
158
+ try {
159
+ t.callback(closingError);
160
+ }
161
+ catch {
162
+ // ignored
163
+ }
164
+ });
141
165
  this._requestQueue = new DoublyLinked();
142
166
  this._requestsProcessing = 0;
143
167
  if (terminateWait <= 0) {
@@ -189,6 +213,8 @@ export class Pool extends EventEmitter {
189
213
  if (item && item.state !== ResourceState.IDLE) {
190
214
  this._itemSetIdle(item, callback);
191
215
  }
216
+ else if (callback)
217
+ callback();
192
218
  this._processNextRequest();
193
219
  }
194
220
  /**
@@ -250,12 +276,19 @@ export class Pool extends EventEmitter {
250
276
  this._itemDestroy(item);
251
277
  return;
252
278
  }
279
+ if (request.timedOut) {
280
+ /* Request already failed with a timeout error; return the
281
+ * resource to the idle pool instead of handing it to an
282
+ * abandoned caller. */
283
+ this._itemSetIdle(item);
284
+ return;
285
+ }
253
286
  this._itemSetAcquired(item);
254
287
  this._ensureMin();
255
288
  request.callback(undefined, item.resource);
256
289
  this.emit('acquire', item.resource);
257
290
  }
258
- else
291
+ else if (!request.timedOut)
259
292
  request.callback(err);
260
293
  }
261
294
  catch {
@@ -303,8 +336,6 @@ export class Pool extends EventEmitter {
303
336
  let tries = 0;
304
337
  this._creating++;
305
338
  const handleCallback = (err, obj) => {
306
- if (request && request.timedOut)
307
- return;
308
339
  if (err || !obj) {
309
340
  tries++;
310
341
  this.emit('error', err, {
@@ -312,7 +343,9 @@ export class Pool extends EventEmitter {
312
343
  tries,
313
344
  maxRetries: this.options.acquireMaxRetries,
314
345
  });
315
- if (err instanceof AbortError || tries >= maxRetries) {
346
+ /* Stop retrying for a request that already timed out */
347
+ const abandoned = !!(request && request.timedOut);
348
+ if (abandoned || err instanceof AbortError || tries >= maxRetries) {
316
349
  this._creating--;
317
350
  return callback && callback(err);
318
351
  }
@@ -337,7 +370,7 @@ export class Pool extends EventEmitter {
337
370
  if (!o) {
338
371
  return handleCallback(new AbortError('Factory returned no resource'));
339
372
  }
340
- promisify.await(o, handleCallback);
373
+ awaitResult(o, handleCallback);
341
374
  }
342
375
  catch (e) {
343
376
  handleCallback(e);
@@ -370,7 +403,7 @@ export class Pool extends EventEmitter {
370
403
  }
371
404
  if (isClosing) {
372
405
  /* Check again 5 ms later */
373
- if (this._allResources.size)
406
+ if (this._allResources.size || this._creating)
374
407
  return;
375
408
  clearInterval(this._houseKeepTimer);
376
409
  this._state = PoolState.CLOSED;
@@ -379,6 +412,12 @@ export class Pool extends EventEmitter {
379
412
  }
380
413
  }
381
414
  _ensureMin() {
415
+ // Common case (min/minIdle both unset): the scheduled tick below would
416
+ // always compute k <= 0 and do nothing, so skip the nextTick() and its
417
+ // closure allocation entirely rather than paying for a wasted microtask
418
+ // on every single acquire().
419
+ if (this.options.min <= 0 && this.options.minIdle <= 0)
420
+ return;
382
421
  process.nextTick(() => {
383
422
  let k = Math.max(this.options.min - this._allResources.size, this.options.minIdle - this._idleResources.length) - this.creating;
384
423
  while (k-- > 0)
@@ -439,7 +478,7 @@ export class Pool extends EventEmitter {
439
478
  if (isAcquired && this._factory.reset) {
440
479
  try {
441
480
  const o = this._factory.reset(item.resource);
442
- promisify.await(o, handleCallback);
481
+ awaitResult(o, handleCallback);
443
482
  }
444
483
  catch (e) {
445
484
  handleCallback(e);
@@ -462,7 +501,7 @@ export class Pool extends EventEmitter {
462
501
  };
463
502
  try {
464
503
  const o = this._factory.destroy(item.resource);
465
- promisify.await(o, handleCallback);
504
+ awaitResult(o, handleCallback);
466
505
  }
467
506
  catch (e) {
468
507
  handleCallback(e);
@@ -475,8 +514,7 @@ export class Pool extends EventEmitter {
475
514
  item.state = ResourceState.VALIDATION;
476
515
  try {
477
516
  const o = this._factory.validate?.(item.resource);
478
- // @ts-ignore
479
- promisify.await(o, callback);
517
+ awaitResult(o, callback);
480
518
  }
481
519
  catch (e) {
482
520
  callback?.(e);