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