lightning-pool 4.6.0 → 4.7.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/cjs/abort-error.js +6 -0
- package/cjs/definitions.js +16 -0
- package/cjs/index.js +11 -0
- package/cjs/pool-options.js +128 -0
- package/cjs/pool-request.js +26 -0
- package/cjs/pool.js +476 -0
- package/cjs/resource-item.js +13 -0
- package/esm/abort-error.js +2 -0
- package/esm/definitions.js +13 -0
- package/esm/index.js +7 -0
- package/esm/package.json +3 -0
- package/esm/pool-options.js +124 -0
- package/esm/pool-request.js +22 -0
- package/esm/pool.js +471 -0
- package/esm/resource-item.js +9 -0
- package/package.json +2 -38
- package/types/abort-error.d.ts +2 -0
- package/types/definitions.d.ts +34 -0
- package/types/index.d.ts +6 -0
- package/types/pool-options.d.ts +41 -0
- package/types/pool-request.d.ts +11 -0
- package/types/pool.d.ts +103 -0
- package/types/resource-item.d.ts +11 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export var PoolState;
|
|
2
|
+
(function (PoolState) {
|
|
3
|
+
PoolState[PoolState["IDLE"] = 0] = "IDLE";
|
|
4
|
+
PoolState[PoolState["STARTED"] = 1] = "STARTED";
|
|
5
|
+
PoolState[PoolState["CLOSING"] = 2] = "CLOSING";
|
|
6
|
+
PoolState[PoolState["CLOSED"] = 3] = "CLOSED";
|
|
7
|
+
})(PoolState || (PoolState = {}));
|
|
8
|
+
export var ResourceState;
|
|
9
|
+
(function (ResourceState) {
|
|
10
|
+
ResourceState[ResourceState["IDLE"] = 0] = "IDLE";
|
|
11
|
+
ResourceState[ResourceState["ACQUIRED"] = 1] = "ACQUIRED";
|
|
12
|
+
ResourceState[ResourceState["VALIDATION"] = 2] = "VALIDATION";
|
|
13
|
+
})(ResourceState || (ResourceState = {}));
|
package/esm/index.js
ADDED
package/esm/package.json
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
const defaultValues = {
|
|
3
|
+
acquireMaxRetries: 0,
|
|
4
|
+
acquireRetryWait: 2000,
|
|
5
|
+
acquireTimeoutMillis: 0,
|
|
6
|
+
fifo: true,
|
|
7
|
+
idleTimeoutMillis: 30000,
|
|
8
|
+
houseKeepInterval: 1000,
|
|
9
|
+
min: 0,
|
|
10
|
+
minIdle: 0,
|
|
11
|
+
max: 10,
|
|
12
|
+
maxQueue: 1000,
|
|
13
|
+
validation: true,
|
|
14
|
+
};
|
|
15
|
+
export class PoolOptions extends EventEmitter {
|
|
16
|
+
constructor(pool) {
|
|
17
|
+
super();
|
|
18
|
+
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
|
+
this.pool = pool;
|
|
31
|
+
}
|
|
32
|
+
get acquireMaxRetries() {
|
|
33
|
+
return this._acquireMaxRetries;
|
|
34
|
+
}
|
|
35
|
+
set acquireMaxRetries(val) {
|
|
36
|
+
this._acquireMaxRetries = val >= 0 ? val : defaultValues.acquireMaxRetries;
|
|
37
|
+
this.emit('change', 'acquireMaxRetries', this._acquireMaxRetries);
|
|
38
|
+
}
|
|
39
|
+
get acquireRetryWait() {
|
|
40
|
+
return this._acquireRetryWait;
|
|
41
|
+
}
|
|
42
|
+
set acquireRetryWait(val) {
|
|
43
|
+
this._acquireRetryWait = val >= 0 ? val : defaultValues.acquireRetryWait;
|
|
44
|
+
this.emit('change', 'acquireRetryWait', this._acquireRetryWait);
|
|
45
|
+
}
|
|
46
|
+
get acquireTimeoutMillis() {
|
|
47
|
+
return this._acquireTimeoutMillis;
|
|
48
|
+
}
|
|
49
|
+
set acquireTimeoutMillis(val) {
|
|
50
|
+
this._acquireTimeoutMillis =
|
|
51
|
+
val >= 0 ? val : defaultValues.acquireTimeoutMillis;
|
|
52
|
+
this.emit('change', 'acquireTimeoutMillis', this._acquireTimeoutMillis);
|
|
53
|
+
}
|
|
54
|
+
get fifo() {
|
|
55
|
+
return this._fifo;
|
|
56
|
+
}
|
|
57
|
+
set fifo(val) {
|
|
58
|
+
// noinspection PointlessBooleanExpressionJS
|
|
59
|
+
this._fifo = !!val;
|
|
60
|
+
this.emit('change', 'fifo', this.fifo);
|
|
61
|
+
}
|
|
62
|
+
get idleTimeoutMillis() {
|
|
63
|
+
return this._idleTimeoutMillis;
|
|
64
|
+
}
|
|
65
|
+
set idleTimeoutMillis(val) {
|
|
66
|
+
this._idleTimeoutMillis = val >= 0 ? val : defaultValues.idleTimeoutMillis;
|
|
67
|
+
this.emit('change', 'idleTimeoutMillis', this._idleTimeoutMillis);
|
|
68
|
+
}
|
|
69
|
+
get houseKeepInterval() {
|
|
70
|
+
return this._houseKeepInterval;
|
|
71
|
+
}
|
|
72
|
+
set houseKeepInterval(val) {
|
|
73
|
+
this._houseKeepInterval = val >= 0 ? val : defaultValues.houseKeepInterval;
|
|
74
|
+
this.emit('change', 'houseKeepInterval', this._houseKeepInterval);
|
|
75
|
+
}
|
|
76
|
+
get min() {
|
|
77
|
+
return this._min;
|
|
78
|
+
}
|
|
79
|
+
set min(val) {
|
|
80
|
+
this._min = val >= 0 ? val : defaultValues.min;
|
|
81
|
+
if (this._min > this._max)
|
|
82
|
+
this._max = this._min;
|
|
83
|
+
this.emit('change', 'min', this._min);
|
|
84
|
+
}
|
|
85
|
+
get minIdle() {
|
|
86
|
+
return this._minIdle;
|
|
87
|
+
}
|
|
88
|
+
set minIdle(val) {
|
|
89
|
+
this._minIdle = val >= 0 ? val : defaultValues.minIdle;
|
|
90
|
+
this.emit('change', 'minIdle', this.minIdle);
|
|
91
|
+
}
|
|
92
|
+
get max() {
|
|
93
|
+
return this._max;
|
|
94
|
+
}
|
|
95
|
+
set max(val) {
|
|
96
|
+
this._max = val >= 0 ? Math.max(val, 1) : defaultValues.max;
|
|
97
|
+
if (this._min > this._max)
|
|
98
|
+
this._min = this._max;
|
|
99
|
+
this.emit('change', 'max', this._max);
|
|
100
|
+
}
|
|
101
|
+
get maxQueue() {
|
|
102
|
+
return this._maxQueue;
|
|
103
|
+
}
|
|
104
|
+
set maxQueue(val) {
|
|
105
|
+
this._maxQueue = val >= 0 ? Math.max(val, 1) : defaultValues.maxQueue;
|
|
106
|
+
this.emit('change', 'maxQueue', this._maxQueue);
|
|
107
|
+
}
|
|
108
|
+
get validation() {
|
|
109
|
+
return this._validation;
|
|
110
|
+
}
|
|
111
|
+
set validation(val) {
|
|
112
|
+
// noinspection PointlessBooleanExpressionJS
|
|
113
|
+
this._validation = !!val;
|
|
114
|
+
this.emit('change', 'validation', this._validation);
|
|
115
|
+
}
|
|
116
|
+
assign(values) {
|
|
117
|
+
const proto = Object.getPrototypeOf(this);
|
|
118
|
+
for (const k of Object.keys(values)) {
|
|
119
|
+
const desc = Object.getOwnPropertyDescriptor(proto, k);
|
|
120
|
+
if (desc && desc.set)
|
|
121
|
+
this[k] = values[k];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
2
|
+
function noop() { }
|
|
3
|
+
export class PoolRequest {
|
|
4
|
+
constructor(pool, callback, options) {
|
|
5
|
+
this.timedOut = false;
|
|
6
|
+
this.created = Date.now();
|
|
7
|
+
this.callback = callback || noop;
|
|
8
|
+
this.options = options;
|
|
9
|
+
if (pool.options.acquireTimeoutMillis) {
|
|
10
|
+
this.timeoutHandle = setTimeout(() => {
|
|
11
|
+
this.timedOut = true;
|
|
12
|
+
this.stopTimout();
|
|
13
|
+
pool.emit('request-timeout');
|
|
14
|
+
this.callback(new Error('Request timed out'));
|
|
15
|
+
}, pool.options.acquireTimeoutMillis);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
stopTimout() {
|
|
19
|
+
if (this.timeoutHandle)
|
|
20
|
+
clearTimeout(this.timeoutHandle);
|
|
21
|
+
}
|
|
22
|
+
}
|
package/esm/pool.js
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import DoublyLinked from 'doublylinked';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import promisify from 'putil-promisify';
|
|
4
|
+
import { AbortError } from './abort-error.js';
|
|
5
|
+
import { PoolState, ResourceState, } from './definitions.js';
|
|
6
|
+
import { PoolOptions } from './pool-options.js';
|
|
7
|
+
import { PoolRequest } from './pool-request.js';
|
|
8
|
+
import { ResourceItem } from './resource-item.js';
|
|
9
|
+
export class Pool extends EventEmitter {
|
|
10
|
+
constructor(factory, config) {
|
|
11
|
+
super();
|
|
12
|
+
this._requestQueue = new DoublyLinked();
|
|
13
|
+
this._allResources = new Map();
|
|
14
|
+
this._acquiredResources = new DoublyLinked();
|
|
15
|
+
this._idleResources = new DoublyLinked();
|
|
16
|
+
this._creating = 0;
|
|
17
|
+
this._requestsProcessing = 0;
|
|
18
|
+
this._state = PoolState.IDLE;
|
|
19
|
+
if (typeof factory !== 'object') {
|
|
20
|
+
throw new TypeError('You must provide `factory` object');
|
|
21
|
+
}
|
|
22
|
+
if (typeof factory.create !== 'function') {
|
|
23
|
+
throw new TypeError('factory.create must be a function');
|
|
24
|
+
}
|
|
25
|
+
if (typeof factory.destroy !== 'function') {
|
|
26
|
+
throw new TypeError('factory.destroy must be a function');
|
|
27
|
+
}
|
|
28
|
+
if (factory.validate && typeof factory.validate !== 'function') {
|
|
29
|
+
throw new TypeError('factory.validate can be a function');
|
|
30
|
+
}
|
|
31
|
+
if (factory.reset && typeof factory.reset !== 'function') {
|
|
32
|
+
throw new TypeError('factory.reset can be a function');
|
|
33
|
+
}
|
|
34
|
+
const opts = (this._options = new PoolOptions(this));
|
|
35
|
+
opts.on('change', (prop, val) => {
|
|
36
|
+
if (prop === 'houseKeepInterval')
|
|
37
|
+
this._setHouseKeep(val);
|
|
38
|
+
if (prop === 'min' || prop === 'minIdle')
|
|
39
|
+
this._ensureMin();
|
|
40
|
+
});
|
|
41
|
+
if (config)
|
|
42
|
+
this.options.assign(config);
|
|
43
|
+
this._factory = factory;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns Pool options
|
|
47
|
+
*/
|
|
48
|
+
get options() {
|
|
49
|
+
return this._options;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns number of resources that are currently acquired
|
|
53
|
+
*/
|
|
54
|
+
get acquired() {
|
|
55
|
+
return this._acquiredResources.length;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns number of unused resources in the pool
|
|
59
|
+
*/
|
|
60
|
+
get available() {
|
|
61
|
+
return this._idleResources.length;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns number of resources currently creating
|
|
65
|
+
*/
|
|
66
|
+
get creating() {
|
|
67
|
+
return this._creating;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Returns number of callers waiting to acquire a resource
|
|
71
|
+
*/
|
|
72
|
+
get pending() {
|
|
73
|
+
return this._requestQueue.length + this._requestsProcessing;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Returns number of resources in the pool
|
|
77
|
+
* regardless of whether they are idle or in use
|
|
78
|
+
*/
|
|
79
|
+
get size() {
|
|
80
|
+
return this._allResources.size;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns state of the pool
|
|
84
|
+
*/
|
|
85
|
+
get state() {
|
|
86
|
+
return this._state;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Starts the pool and begins creating of resources, starts house keeping and any other internal logic.
|
|
90
|
+
* Note: This method is not need to be called. Pool instance will automatically be started when acquire() method is called
|
|
91
|
+
*/
|
|
92
|
+
start() {
|
|
93
|
+
if (this._state === PoolState.STARTED)
|
|
94
|
+
return;
|
|
95
|
+
if (this._state >= PoolState.CLOSING) {
|
|
96
|
+
throw new Error('Closed pool can not be started again');
|
|
97
|
+
}
|
|
98
|
+
this._state = PoolState.STARTED;
|
|
99
|
+
this._setHouseKeep(this.options.houseKeepInterval);
|
|
100
|
+
this._ensureMin();
|
|
101
|
+
this.emit('start');
|
|
102
|
+
}
|
|
103
|
+
close(arg0, arg1) {
|
|
104
|
+
let terminateWait = Infinity;
|
|
105
|
+
let callback;
|
|
106
|
+
if (typeof arg0 === 'function')
|
|
107
|
+
callback = arg0;
|
|
108
|
+
else {
|
|
109
|
+
terminateWait = typeof arg0 === 'number' ? arg0 : arg0 ? 0 : Infinity;
|
|
110
|
+
callback = arg1;
|
|
111
|
+
}
|
|
112
|
+
if (!callback) {
|
|
113
|
+
return promisify.fromCallback(cb => this.close(terminateWait, cb));
|
|
114
|
+
}
|
|
115
|
+
if (this._state === PoolState.CLOSED || this._state === PoolState.IDLE) {
|
|
116
|
+
return callback();
|
|
117
|
+
}
|
|
118
|
+
if (this._state === PoolState.CLOSING) {
|
|
119
|
+
this.once('close', callback);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
this.emit('closing');
|
|
123
|
+
if (this._houseKeepTimer)
|
|
124
|
+
clearTimeout(this._houseKeepTimer);
|
|
125
|
+
this._state = PoolState.CLOSING;
|
|
126
|
+
this._requestQueue.forEach(t => t.stopTimout());
|
|
127
|
+
this._requestQueue = new DoublyLinked();
|
|
128
|
+
this._requestsProcessing = 0;
|
|
129
|
+
if (terminateWait <= 0) {
|
|
130
|
+
this._acquiredResources.forEach(t => this.destroy(t.resource));
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
const startTime = Date.now();
|
|
134
|
+
this._closeWaitTimer = setInterval(() => {
|
|
135
|
+
if (!this._allResources.size) {
|
|
136
|
+
clearInterval(this._closeWaitTimer);
|
|
137
|
+
this._closeWaitTimer = undefined;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (Date.now() > startTime + terminateWait) {
|
|
141
|
+
clearInterval(this._closeWaitTimer);
|
|
142
|
+
this._closeWaitTimer = undefined;
|
|
143
|
+
this._acquiredResources.forEach(t => this.release(t.resource));
|
|
144
|
+
this.emit('terminate');
|
|
145
|
+
}
|
|
146
|
+
}, 50);
|
|
147
|
+
}
|
|
148
|
+
this._setHouseKeep(5);
|
|
149
|
+
this.once('close', callback);
|
|
150
|
+
}
|
|
151
|
+
closeAsync(arg0) {
|
|
152
|
+
return promisify.fromCallback(cb => this.close(arg0, cb));
|
|
153
|
+
}
|
|
154
|
+
acquire(callback) {
|
|
155
|
+
if (!callback)
|
|
156
|
+
return promisify.fromCallback(cb => this.acquire(cb));
|
|
157
|
+
try {
|
|
158
|
+
this.start();
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
return callback(e);
|
|
162
|
+
}
|
|
163
|
+
if (this.options.maxQueue && this.pending >= this.options.maxQueue) {
|
|
164
|
+
return callback(new Error('Pool queue is full'));
|
|
165
|
+
}
|
|
166
|
+
this._requestQueue.push(new PoolRequest(this, callback));
|
|
167
|
+
this._processNextRequest();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Releases an allocated `resource` and let it back to pool.
|
|
171
|
+
*/
|
|
172
|
+
release(resource, callback) {
|
|
173
|
+
const item = this._allResources.get(resource);
|
|
174
|
+
if (item && item.state !== ResourceState.IDLE) {
|
|
175
|
+
this._itemSetIdle(item, callback);
|
|
176
|
+
}
|
|
177
|
+
this._processNextRequest();
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Async version of release().
|
|
181
|
+
*/
|
|
182
|
+
releaseAsync(resource) {
|
|
183
|
+
return promisify.fromCallback(cb => this.release(resource, cb));
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Releases, destroys and removes any `resource` from `Pool`.
|
|
187
|
+
*/
|
|
188
|
+
destroy(resource, callback) {
|
|
189
|
+
try {
|
|
190
|
+
const item = this._allResources.get(resource);
|
|
191
|
+
if (item)
|
|
192
|
+
this._itemDestroy(item, callback);
|
|
193
|
+
else if (callback)
|
|
194
|
+
callback();
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
this._processNextRequest();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Async version of destroy().
|
|
202
|
+
*/
|
|
203
|
+
destroyAsync(resource) {
|
|
204
|
+
return promisify.fromCallback(cb => this.destroy(resource, cb));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Returns if a `resource` has been acquired from the pool and not yet released or destroyed.
|
|
208
|
+
*/
|
|
209
|
+
isAcquired(resource) {
|
|
210
|
+
const item = this._allResources.get(resource);
|
|
211
|
+
return !!(item && item.acquiredNode);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns if the pool contains a `resource`
|
|
215
|
+
*/
|
|
216
|
+
includes(resource) {
|
|
217
|
+
return this._allResources.has(resource);
|
|
218
|
+
}
|
|
219
|
+
_processNextRequest() {
|
|
220
|
+
if (this._state !== PoolState.STARTED ||
|
|
221
|
+
this._requestsProcessing >= this.options.max - this.acquired) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const request = this._requestQueue.shift();
|
|
225
|
+
if (!request)
|
|
226
|
+
return;
|
|
227
|
+
this._requestsProcessing++;
|
|
228
|
+
const handleCallback = (err, item) => {
|
|
229
|
+
this._requestsProcessing--;
|
|
230
|
+
request.stopTimout();
|
|
231
|
+
try {
|
|
232
|
+
if (item) {
|
|
233
|
+
/* istanbul ignore next : Hard to simulate */
|
|
234
|
+
if (this._state !== PoolState.STARTED) {
|
|
235
|
+
this._itemDestroy(item);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
this._itemSetAcquired(item);
|
|
239
|
+
this._ensureMin();
|
|
240
|
+
request.callback(undefined, item.resource);
|
|
241
|
+
this.emit('acquire', item.resource);
|
|
242
|
+
}
|
|
243
|
+
else
|
|
244
|
+
request.callback(err);
|
|
245
|
+
}
|
|
246
|
+
catch (ignored) {
|
|
247
|
+
// ignored
|
|
248
|
+
}
|
|
249
|
+
this._processNextRequest();
|
|
250
|
+
};
|
|
251
|
+
const item = this._idleResources.shift();
|
|
252
|
+
if (item) {
|
|
253
|
+
/* Validate resource */
|
|
254
|
+
if (this.options.validation && this._factory.validate) {
|
|
255
|
+
this._itemValidate(item, (err) => {
|
|
256
|
+
/* Destroy resource on validation error */
|
|
257
|
+
if (err) {
|
|
258
|
+
this._itemDestroy(item);
|
|
259
|
+
this.emit('validate-error', err, item.resource);
|
|
260
|
+
this._requestsProcessing--;
|
|
261
|
+
this._requestQueue.unshift(request);
|
|
262
|
+
this._processNextRequest();
|
|
263
|
+
}
|
|
264
|
+
else
|
|
265
|
+
handleCallback(undefined, item);
|
|
266
|
+
});
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
return handleCallback(undefined, item);
|
|
270
|
+
}
|
|
271
|
+
/** There is no idle resource. We need to create new one **/
|
|
272
|
+
this._createResource(request, handleCallback);
|
|
273
|
+
}
|
|
274
|
+
emit(event, ...args) {
|
|
275
|
+
// Prevents errors while calling emit()
|
|
276
|
+
try {
|
|
277
|
+
return super.emit(event, ...args);
|
|
278
|
+
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Creates new resource
|
|
285
|
+
*/
|
|
286
|
+
_createResource(request, callback) {
|
|
287
|
+
const maxRetries = this.options.acquireMaxRetries;
|
|
288
|
+
let tries = 0;
|
|
289
|
+
this._creating++;
|
|
290
|
+
const handleCallback = (err, obj) => {
|
|
291
|
+
if (request && request.timedOut)
|
|
292
|
+
return;
|
|
293
|
+
if (err || !obj) {
|
|
294
|
+
tries++;
|
|
295
|
+
this.emit('error', err, {
|
|
296
|
+
requestTime: request ? request.created : Date.now(),
|
|
297
|
+
tries,
|
|
298
|
+
maxRetries: this.options.acquireMaxRetries,
|
|
299
|
+
});
|
|
300
|
+
if (err instanceof AbortError || tries >= maxRetries) {
|
|
301
|
+
this._creating--;
|
|
302
|
+
return callback && callback(err);
|
|
303
|
+
}
|
|
304
|
+
return setTimeout(() => tryCreate(), this.options.acquireRetryWait);
|
|
305
|
+
}
|
|
306
|
+
this._creating--;
|
|
307
|
+
if (this._allResources.has(obj)) {
|
|
308
|
+
return (callback &&
|
|
309
|
+
callback(new Error('Factory error. Resource already in pool')));
|
|
310
|
+
}
|
|
311
|
+
const item = new ResourceItem(obj);
|
|
312
|
+
this._itemSetIdle(item);
|
|
313
|
+
this._allResources.set(obj, item);
|
|
314
|
+
if (callback)
|
|
315
|
+
callback(undefined, item);
|
|
316
|
+
this.emit('create', obj);
|
|
317
|
+
};
|
|
318
|
+
const tryCreate = () => {
|
|
319
|
+
try {
|
|
320
|
+
const o = this._factory.create({ tries, maxRetries });
|
|
321
|
+
/* istanbul ignore next */
|
|
322
|
+
if (!o) {
|
|
323
|
+
return handleCallback(new AbortError('Factory returned no resource'));
|
|
324
|
+
}
|
|
325
|
+
promisify.await(o, handleCallback);
|
|
326
|
+
}
|
|
327
|
+
catch (e) {
|
|
328
|
+
handleCallback(e);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
tryCreate();
|
|
332
|
+
}
|
|
333
|
+
_setHouseKeep(ms) {
|
|
334
|
+
if (this._houseKeepTimer)
|
|
335
|
+
clearInterval(this._houseKeepTimer);
|
|
336
|
+
this._houseKeepTimer = undefined;
|
|
337
|
+
if ((ms > 0 && this.state === PoolState.STARTED) ||
|
|
338
|
+
this.state === PoolState.CLOSING) {
|
|
339
|
+
this._houseKeepTimer = setInterval(() => this._houseKeep(), ms);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
_houseKeep() {
|
|
343
|
+
const isClosing = this._state === PoolState.CLOSING;
|
|
344
|
+
const now = Date.now();
|
|
345
|
+
let m = this._allResources.size - this.options.min;
|
|
346
|
+
let n = this._idleResources.length - this.options.minIdle;
|
|
347
|
+
if (isClosing || (m > 0 && n > 0)) {
|
|
348
|
+
this._idleResources.every((item) => {
|
|
349
|
+
if (isClosing || item.idleTime + this.options.idleTimeoutMillis < now) {
|
|
350
|
+
this._itemDestroy(item);
|
|
351
|
+
return isClosing || !!(--n && --m);
|
|
352
|
+
}
|
|
353
|
+
return false;
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (isClosing) {
|
|
357
|
+
/* Check again 5 ms later */
|
|
358
|
+
if (this._allResources.size)
|
|
359
|
+
return;
|
|
360
|
+
clearInterval(this._houseKeepTimer);
|
|
361
|
+
this._state = PoolState.CLOSED;
|
|
362
|
+
this._requestsProcessing = 0;
|
|
363
|
+
this.emit('close');
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
_ensureMin() {
|
|
367
|
+
process.nextTick(() => {
|
|
368
|
+
let k = Math.max(this.options.min - this._allResources.size, this.options.minIdle - this._idleResources.length) - this.creating;
|
|
369
|
+
while (k-- > 0)
|
|
370
|
+
this._createResource();
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
_itemSetAcquired(item) {
|
|
374
|
+
if (item.state !== ResourceState.ACQUIRED) {
|
|
375
|
+
this._itemDetach(item);
|
|
376
|
+
item.state = ResourceState.ACQUIRED;
|
|
377
|
+
this._acquiredResources.push(item);
|
|
378
|
+
item.acquiredNode = this._acquiredResources.tail;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
_itemDetach(item) {
|
|
382
|
+
switch (item.state) {
|
|
383
|
+
case ResourceState.IDLE:
|
|
384
|
+
item.idleTime = 0;
|
|
385
|
+
/* istanbul ignore next*/
|
|
386
|
+
if (item.idleNode)
|
|
387
|
+
item.idleNode.remove();
|
|
388
|
+
item.idleNode = undefined;
|
|
389
|
+
break;
|
|
390
|
+
case ResourceState.ACQUIRED:
|
|
391
|
+
case ResourceState.VALIDATION:
|
|
392
|
+
/* istanbul ignore next*/
|
|
393
|
+
if (item.acquiredNode)
|
|
394
|
+
item.acquiredNode.remove();
|
|
395
|
+
item.acquiredNode = undefined;
|
|
396
|
+
break;
|
|
397
|
+
default:
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
_itemSetIdle(item, callback) {
|
|
402
|
+
const isAcquired = item.state === ResourceState.ACQUIRED;
|
|
403
|
+
const handleCallback = (err) => {
|
|
404
|
+
if (err)
|
|
405
|
+
return this._itemDestroy(item, callback);
|
|
406
|
+
this._itemDetach(item);
|
|
407
|
+
item.idleTime = Date.now();
|
|
408
|
+
item.state = ResourceState.IDLE;
|
|
409
|
+
if (this.options.fifo) {
|
|
410
|
+
this._idleResources.push(item);
|
|
411
|
+
item.idleNode = this._idleResources.tail;
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
this._idleResources.unshift(item);
|
|
415
|
+
item.idleNode = this._idleResources.head;
|
|
416
|
+
}
|
|
417
|
+
if (isAcquired)
|
|
418
|
+
this.emit('return', item.resource);
|
|
419
|
+
if (callback)
|
|
420
|
+
callback();
|
|
421
|
+
// noinspection JSAccessibilityCheck
|
|
422
|
+
this._processNextRequest();
|
|
423
|
+
};
|
|
424
|
+
if (isAcquired && this._factory.reset) {
|
|
425
|
+
try {
|
|
426
|
+
const o = this._factory.reset(item.resource);
|
|
427
|
+
promisify.await(o, handleCallback);
|
|
428
|
+
}
|
|
429
|
+
catch (e) {
|
|
430
|
+
handleCallback(e);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
else
|
|
434
|
+
handleCallback();
|
|
435
|
+
}
|
|
436
|
+
_itemDestroy(item, callback) {
|
|
437
|
+
this._itemDetach(item);
|
|
438
|
+
const handleCallback = (err) => {
|
|
439
|
+
if (err) {
|
|
440
|
+
this.emit('destroy-error', err, item.resource);
|
|
441
|
+
/* istanbul ignore next */
|
|
442
|
+
return callback && callback(err);
|
|
443
|
+
}
|
|
444
|
+
this.emit('destroy', item.resource);
|
|
445
|
+
item.destroyed = true;
|
|
446
|
+
if (callback)
|
|
447
|
+
callback();
|
|
448
|
+
};
|
|
449
|
+
try {
|
|
450
|
+
this._allResources.delete(item.resource);
|
|
451
|
+
this._processNextRequest();
|
|
452
|
+
const o = this._factory.destroy(item.resource);
|
|
453
|
+
promisify.await(o, handleCallback);
|
|
454
|
+
}
|
|
455
|
+
catch (e) {
|
|
456
|
+
handleCallback(e);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
_itemValidate(item, callback) {
|
|
460
|
+
item.state = ResourceState.VALIDATION;
|
|
461
|
+
try {
|
|
462
|
+
const o = this._factory.validate?.(item.resource);
|
|
463
|
+
// @ts-ignore
|
|
464
|
+
promisify.await(o, callback);
|
|
465
|
+
}
|
|
466
|
+
catch (e) {
|
|
467
|
+
if (callback)
|
|
468
|
+
callback(e);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|