lightning-pool 4.12.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 +451 -0
- package/README.md +4 -291
- package/{types/index.d.ts → index.d.ts} +1 -1
- package/package.json +8 -20
- package/{esm/pool-options.js → pool-options.js} +21 -14
- package/{esm/pool-request.js → pool-request.js} +5 -1
- package/{types/pool.d.ts → pool.d.ts} +5 -4
- package/{esm/pool.js → pool.js} +60 -18
- package/{esm/resource-item.js → resource-item.js} +6 -3
- package/cjs/abort-error.js +0 -6
- package/cjs/constants.js +0 -16
- package/cjs/index.js +0 -12
- package/cjs/package.json +0 -3
- package/cjs/pool-options.js +0 -128
- package/cjs/pool-request.js +0 -25
- package/cjs/pool.js +0 -486
- package/cjs/resource-item.js +0 -13
- package/cjs/tsconfig-build-cjs.tsbuildinfo +0 -1
- package/cjs/types.js +0 -2
- package/esm/package.json +0 -3
- package/esm/tsconfig-build-esm.tsbuildinfo +0 -1
- package/types/index.d.cts +0 -7
- /package/{types/abort-error.d.ts → abort-error.d.ts} +0 -0
- /package/{esm/abort-error.js → abort-error.js} +0 -0
- /package/{types/constants.d.ts → constants.d.ts} +0 -0
- /package/{esm/constants.js → constants.js} +0 -0
- /package/{esm/index.js → index.js} +0 -0
- /package/{types/pool-options.d.ts → pool-options.d.ts} +0 -0
- /package/{types/pool-request.d.ts → pool-request.d.ts} +0 -0
- /package/{types/resource-item.d.ts → resource-item.d.ts} +0 -0
- /package/{types/types.d.ts → types.d.ts} +0 -0
- /package/{esm/types.js → types.js} +0 -0
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
High performance resource pool written with TypeScript.
|
|
11
11
|
|
|
12
|
-
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
4
|
+
"version": "4.14.0",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
@@ -10,23 +10,15 @@
|
|
|
10
10
|
"tslib": "^2.8.1"
|
|
11
11
|
},
|
|
12
12
|
"type": "module",
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"types": "./index.d.ts",
|
|
13
15
|
"exports": {
|
|
14
16
|
".": {
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
"default": "./esm/index.js"
|
|
18
|
-
},
|
|
19
|
-
"require": {
|
|
20
|
-
"types": "./types/index.d.cts",
|
|
21
|
-
"default": "./cjs/index.js"
|
|
22
|
-
},
|
|
23
|
-
"default": "./esm/index.js"
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
24
19
|
},
|
|
25
20
|
"./package.json": "./package.json"
|
|
26
21
|
},
|
|
27
|
-
"main": "./cjs/index.js",
|
|
28
|
-
"module": "./esm/index.js",
|
|
29
|
-
"types": "./types/index.d.ts",
|
|
30
22
|
"repository": {
|
|
31
23
|
"type": "git",
|
|
32
24
|
"url": "git+https://github.com/panates/lightning-pool.git"
|
|
@@ -39,11 +31,7 @@
|
|
|
39
31
|
"generic",
|
|
40
32
|
"generic-pool"
|
|
41
33
|
],
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
"types/",
|
|
46
|
-
"LICENSE",
|
|
47
|
-
"README.md"
|
|
48
|
-
]
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
}
|
|
49
37
|
}
|
|
@@ -13,20 +13,21 @@ const defaultValues = {
|
|
|
13
13
|
validation: true,
|
|
14
14
|
};
|
|
15
15
|
export class PoolOptions extends EventEmitter {
|
|
16
|
+
pool;
|
|
17
|
+
_acquireMaxRetries = defaultValues.acquireMaxRetries;
|
|
18
|
+
_acquireRetryWait = defaultValues.acquireRetryWait;
|
|
19
|
+
_acquireTimeoutMillis = defaultValues.acquireTimeoutMillis;
|
|
20
|
+
_fifo = defaultValues.fifo;
|
|
21
|
+
_idleTimeoutMillis = defaultValues.idleTimeoutMillis;
|
|
22
|
+
_houseKeepInterval = defaultValues.houseKeepInterval;
|
|
23
|
+
_min = defaultValues.min;
|
|
24
|
+
_minIdle = defaultValues.minIdle;
|
|
25
|
+
_max = defaultValues.max;
|
|
26
|
+
_maxQueue = defaultValues.maxQueue;
|
|
27
|
+
_validation = defaultValues.validation;
|
|
16
28
|
constructor(pool) {
|
|
17
29
|
super();
|
|
18
30
|
this.pool = pool;
|
|
19
|
-
this._acquireMaxRetries = defaultValues.acquireMaxRetries;
|
|
20
|
-
this._acquireRetryWait = defaultValues.acquireRetryWait;
|
|
21
|
-
this._acquireTimeoutMillis = defaultValues.acquireTimeoutMillis;
|
|
22
|
-
this._fifo = defaultValues.fifo;
|
|
23
|
-
this._idleTimeoutMillis = defaultValues.idleTimeoutMillis;
|
|
24
|
-
this._houseKeepInterval = defaultValues.houseKeepInterval;
|
|
25
|
-
this._min = defaultValues.min;
|
|
26
|
-
this._minIdle = defaultValues.minIdle;
|
|
27
|
-
this._max = defaultValues.max;
|
|
28
|
-
this._maxQueue = defaultValues.maxQueue;
|
|
29
|
-
this._validation = defaultValues.validation;
|
|
30
31
|
this.pool = pool;
|
|
31
32
|
}
|
|
32
33
|
get acquireMaxRetries() {
|
|
@@ -115,10 +116,16 @@ export class PoolOptions extends EventEmitter {
|
|
|
115
116
|
}
|
|
116
117
|
assign(values) {
|
|
117
118
|
const proto = Object.getPrototypeOf(this);
|
|
118
|
-
|
|
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) {
|
|
119
125
|
const desc = Object.getOwnPropertyDescriptor(proto, k);
|
|
120
|
-
|
|
121
|
-
|
|
126
|
+
const val = values[k];
|
|
127
|
+
if (desc && desc.set && val !== undefined)
|
|
128
|
+
this[k] = val;
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
131
|
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
function noop() { }
|
|
2
2
|
export class PoolRequest {
|
|
3
|
+
created;
|
|
4
|
+
callback;
|
|
5
|
+
options;
|
|
6
|
+
timeoutHandle;
|
|
7
|
+
timedOut = false;
|
|
3
8
|
constructor(pool, callback, options) {
|
|
4
|
-
this.timedOut = false;
|
|
5
9
|
this.created = Date.now();
|
|
6
10
|
this.callback = callback || noop;
|
|
7
11
|
this.options = options;
|
|
@@ -52,12 +52,13 @@ export declare class Pool<T = any> extends EventEmitter {
|
|
|
52
52
|
/**
|
|
53
53
|
* Shuts down the pool and destroys all resources.
|
|
54
54
|
*/
|
|
55
|
+
close(): void;
|
|
55
56
|
close(callback?: Callback): void;
|
|
56
|
-
close(terminateWait
|
|
57
|
-
close(force
|
|
57
|
+
close(terminateWait?: number, callback?: Callback): void;
|
|
58
|
+
close(force?: boolean, callback?: Callback): void;
|
|
58
59
|
closeAsync(): Promise<void>;
|
|
59
|
-
closeAsync(terminateWait
|
|
60
|
-
closeAsync(force
|
|
60
|
+
closeAsync(terminateWait?: number): Promise<void>;
|
|
61
|
+
closeAsync(force?: boolean): Promise<void>;
|
|
61
62
|
/**
|
|
62
63
|
* Acquires `resource` from the pool or create a new one
|
|
63
64
|
*/
|
package/{esm/pool.js → pool.js}
RENAMED
|
@@ -6,16 +6,35 @@ 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 {
|
|
25
|
+
_options;
|
|
26
|
+
_factory;
|
|
27
|
+
_allResources = new Map();
|
|
28
|
+
_requestQueue = new DoublyLinked();
|
|
29
|
+
_acquiredResources = new DoublyLinked();
|
|
30
|
+
_idleResources = new DoublyLinked();
|
|
31
|
+
_creating = 0;
|
|
32
|
+
_requestsProcessing = 0;
|
|
33
|
+
_state = PoolState.IDLE;
|
|
34
|
+
_houseKeepTimer;
|
|
35
|
+
_closeWaitTimer;
|
|
10
36
|
constructor(factory, config) {
|
|
11
37
|
super();
|
|
12
|
-
this._allResources = new Map();
|
|
13
|
-
this._requestQueue = new DoublyLinked();
|
|
14
|
-
this._acquiredResources = new DoublyLinked();
|
|
15
|
-
this._idleResources = new DoublyLinked();
|
|
16
|
-
this._creating = 0;
|
|
17
|
-
this._requestsProcessing = 0;
|
|
18
|
-
this._state = PoolState.IDLE;
|
|
19
38
|
if (typeof factory !== 'object') {
|
|
20
39
|
throw new TypeError('You must provide `factory` object');
|
|
21
40
|
}
|
|
@@ -133,7 +152,16 @@ export class Pool extends EventEmitter {
|
|
|
133
152
|
if (this._houseKeepTimer)
|
|
134
153
|
clearTimeout(this._houseKeepTimer);
|
|
135
154
|
this._state = PoolState.CLOSING;
|
|
136
|
-
|
|
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
|
+
});
|
|
137
165
|
this._requestQueue = new DoublyLinked();
|
|
138
166
|
this._requestsProcessing = 0;
|
|
139
167
|
if (terminateWait <= 0) {
|
|
@@ -185,6 +213,8 @@ export class Pool extends EventEmitter {
|
|
|
185
213
|
if (item && item.state !== ResourceState.IDLE) {
|
|
186
214
|
this._itemSetIdle(item, callback);
|
|
187
215
|
}
|
|
216
|
+
else if (callback)
|
|
217
|
+
callback();
|
|
188
218
|
this._processNextRequest();
|
|
189
219
|
}
|
|
190
220
|
/**
|
|
@@ -246,12 +276,19 @@ export class Pool extends EventEmitter {
|
|
|
246
276
|
this._itemDestroy(item);
|
|
247
277
|
return;
|
|
248
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
|
+
}
|
|
249
286
|
this._itemSetAcquired(item);
|
|
250
287
|
this._ensureMin();
|
|
251
288
|
request.callback(undefined, item.resource);
|
|
252
289
|
this.emit('acquire', item.resource);
|
|
253
290
|
}
|
|
254
|
-
else
|
|
291
|
+
else if (!request.timedOut)
|
|
255
292
|
request.callback(err);
|
|
256
293
|
}
|
|
257
294
|
catch {
|
|
@@ -299,8 +336,6 @@ export class Pool extends EventEmitter {
|
|
|
299
336
|
let tries = 0;
|
|
300
337
|
this._creating++;
|
|
301
338
|
const handleCallback = (err, obj) => {
|
|
302
|
-
if (request && request.timedOut)
|
|
303
|
-
return;
|
|
304
339
|
if (err || !obj) {
|
|
305
340
|
tries++;
|
|
306
341
|
this.emit('error', err, {
|
|
@@ -308,7 +343,9 @@ export class Pool extends EventEmitter {
|
|
|
308
343
|
tries,
|
|
309
344
|
maxRetries: this.options.acquireMaxRetries,
|
|
310
345
|
});
|
|
311
|
-
|
|
346
|
+
/* Stop retrying for a request that already timed out */
|
|
347
|
+
const abandoned = !!(request && request.timedOut);
|
|
348
|
+
if (abandoned || err instanceof AbortError || tries >= maxRetries) {
|
|
312
349
|
this._creating--;
|
|
313
350
|
return callback && callback(err);
|
|
314
351
|
}
|
|
@@ -333,7 +370,7 @@ export class Pool extends EventEmitter {
|
|
|
333
370
|
if (!o) {
|
|
334
371
|
return handleCallback(new AbortError('Factory returned no resource'));
|
|
335
372
|
}
|
|
336
|
-
|
|
373
|
+
awaitResult(o, handleCallback);
|
|
337
374
|
}
|
|
338
375
|
catch (e) {
|
|
339
376
|
handleCallback(e);
|
|
@@ -366,7 +403,7 @@ export class Pool extends EventEmitter {
|
|
|
366
403
|
}
|
|
367
404
|
if (isClosing) {
|
|
368
405
|
/* Check again 5 ms later */
|
|
369
|
-
if (this._allResources.size)
|
|
406
|
+
if (this._allResources.size || this._creating)
|
|
370
407
|
return;
|
|
371
408
|
clearInterval(this._houseKeepTimer);
|
|
372
409
|
this._state = PoolState.CLOSED;
|
|
@@ -375,6 +412,12 @@ export class Pool extends EventEmitter {
|
|
|
375
412
|
}
|
|
376
413
|
}
|
|
377
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;
|
|
378
421
|
process.nextTick(() => {
|
|
379
422
|
let k = Math.max(this.options.min - this._allResources.size, this.options.minIdle - this._idleResources.length) - this.creating;
|
|
380
423
|
while (k-- > 0)
|
|
@@ -435,7 +478,7 @@ export class Pool extends EventEmitter {
|
|
|
435
478
|
if (isAcquired && this._factory.reset) {
|
|
436
479
|
try {
|
|
437
480
|
const o = this._factory.reset(item.resource);
|
|
438
|
-
|
|
481
|
+
awaitResult(o, handleCallback);
|
|
439
482
|
}
|
|
440
483
|
catch (e) {
|
|
441
484
|
handleCallback(e);
|
|
@@ -458,7 +501,7 @@ export class Pool extends EventEmitter {
|
|
|
458
501
|
};
|
|
459
502
|
try {
|
|
460
503
|
const o = this._factory.destroy(item.resource);
|
|
461
|
-
|
|
504
|
+
awaitResult(o, handleCallback);
|
|
462
505
|
}
|
|
463
506
|
catch (e) {
|
|
464
507
|
handleCallback(e);
|
|
@@ -471,8 +514,7 @@ export class Pool extends EventEmitter {
|
|
|
471
514
|
item.state = ResourceState.VALIDATION;
|
|
472
515
|
try {
|
|
473
516
|
const o = this._factory.validate?.(item.resource);
|
|
474
|
-
|
|
475
|
-
promisify.await(o, callback);
|
|
517
|
+
awaitResult(o, callback);
|
|
476
518
|
}
|
|
477
519
|
catch (e) {
|
|
478
520
|
callback?.(e);
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { ResourceState } from './constants.js';
|
|
2
2
|
export class ResourceItem {
|
|
3
|
+
state = ResourceState.IDLE;
|
|
4
|
+
acquiredNode;
|
|
5
|
+
idleNode;
|
|
6
|
+
resource;
|
|
7
|
+
idleTime = 0;
|
|
8
|
+
destroyed = false;
|
|
3
9
|
constructor(resource) {
|
|
4
|
-
this.state = ResourceState.IDLE;
|
|
5
|
-
this.idleTime = 0;
|
|
6
|
-
this.destroyed = false;
|
|
7
10
|
this.resource = resource;
|
|
8
11
|
}
|
|
9
12
|
}
|
package/cjs/abort-error.js
DELETED
package/cjs/constants.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ResourceState = exports.PoolState = void 0;
|
|
4
|
-
var PoolState;
|
|
5
|
-
(function (PoolState) {
|
|
6
|
-
PoolState[PoolState["IDLE"] = 0] = "IDLE";
|
|
7
|
-
PoolState[PoolState["STARTED"] = 1] = "STARTED";
|
|
8
|
-
PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
|
|
9
|
-
PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
|
|
10
|
-
})(PoolState || (exports.PoolState = PoolState = {}));
|
|
11
|
-
var ResourceState;
|
|
12
|
-
(function (ResourceState) {
|
|
13
|
-
ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
|
|
14
|
-
ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
|
|
15
|
-
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
|
|
16
|
-
})(ResourceState || (exports.ResourceState = ResourceState = {}));
|
package/cjs/index.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createPool = createPool;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const pool_js_1 = require("./pool.js");
|
|
6
|
-
tslib_1.__exportStar(require("./abort-error.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./pool.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./types.js"), exports);
|
|
10
|
-
function createPool(factory, config) {
|
|
11
|
-
return new pool_js_1.Pool(factory, config);
|
|
12
|
-
}
|
package/cjs/package.json
DELETED