poolifier 2.6.37 → 2.6.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/lib/index.d.ts +1881 -22
- package/lib/index.js +1 -1
- package/lib/index.mjs +1 -1
- package/package.json +4 -3
- package/lib/circular-array.d.ts +0 -22
- package/lib/deque.d.ts +0 -83
- package/lib/pools/abstract-pool.d.ts +0 -316
- package/lib/pools/cluster/dynamic.d.ts +0 -29
- package/lib/pools/cluster/fixed.d.ts +0 -62
- package/lib/pools/pool.d.ts +0 -243
- package/lib/pools/selection-strategies/abstract-worker-choice-strategy.d.ts +0 -104
- package/lib/pools/selection-strategies/fair-share-worker-choice-strategy.d.ts +0 -39
- package/lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.d.ts +0 -39
- package/lib/pools/selection-strategies/least-busy-worker-choice-strategy.d.ts +0 -26
- package/lib/pools/selection-strategies/least-elu-worker-choice-strategy.d.ts +0 -26
- package/lib/pools/selection-strategies/least-used-worker-choice-strategy.d.ts +0 -24
- package/lib/pools/selection-strategies/round-robin-worker-choice-strategy.d.ts +0 -24
- package/lib/pools/selection-strategies/selection-strategies-types.d.ts +0 -200
- package/lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.d.ts +0 -35
- package/lib/pools/selection-strategies/worker-choice-strategy-context.d.ts +0 -71
- package/lib/pools/thread/dynamic.d.ts +0 -29
- package/lib/pools/thread/fixed.d.ts +0 -54
- package/lib/pools/version.d.ts +0 -1
- package/lib/pools/worker-node.d.ts +0 -60
- package/lib/pools/worker.d.ts +0 -277
- package/lib/utility-types.d.ts +0 -152
- package/lib/utils.d.ts +0 -123
- package/lib/worker/abstract-worker.d.ts +0 -192
- package/lib/worker/cluster-worker.d.ts +0 -35
- package/lib/worker/task-functions.d.ts +0 -33
- package/lib/worker/thread-worker.d.ts +0 -43
- package/lib/worker/worker-options.d.ts +0 -61
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { type TransferListItem } from 'node:worker_threads';
|
|
3
|
-
import type { MessageValue, PromiseResponseWrapper, Task } from '../utility-types';
|
|
4
|
-
import { type IPool, PoolEmitter, type PoolInfo, type PoolOptions, type PoolType, type TasksQueueOptions } from './pool';
|
|
5
|
-
import type { IWorker, IWorkerNode, WorkerInfo, WorkerType } from './worker';
|
|
6
|
-
import { type WorkerChoiceStrategy, type WorkerChoiceStrategyOptions } from './selection-strategies/selection-strategies-types';
|
|
7
|
-
import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context';
|
|
8
|
-
/**
|
|
9
|
-
* Base class that implements some shared logic for all poolifier pools.
|
|
10
|
-
*
|
|
11
|
-
* @typeParam Worker - Type of worker which manages this pool.
|
|
12
|
-
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
|
|
13
|
-
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
|
|
14
|
-
*/
|
|
15
|
-
export declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Response = unknown> implements IPool<Worker, Data, Response> {
|
|
16
|
-
protected readonly numberOfWorkers: number;
|
|
17
|
-
protected readonly filePath: string;
|
|
18
|
-
protected readonly opts: PoolOptions<Worker>;
|
|
19
|
-
/** @inheritDoc */
|
|
20
|
-
readonly workerNodes: Array<IWorkerNode<Worker, Data>>;
|
|
21
|
-
/** @inheritDoc */
|
|
22
|
-
readonly emitter?: PoolEmitter;
|
|
23
|
-
/**
|
|
24
|
-
* The task execution response promise map.
|
|
25
|
-
*
|
|
26
|
-
* - `key`: The message id of each submitted task.
|
|
27
|
-
* - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
|
|
28
|
-
*
|
|
29
|
-
* When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
|
|
30
|
-
*/
|
|
31
|
-
protected promiseResponseMap: Map<string, PromiseResponseWrapper<Response>>;
|
|
32
|
-
/**
|
|
33
|
-
* Worker choice strategy context referencing a worker choice algorithm implementation.
|
|
34
|
-
*/
|
|
35
|
-
protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<Worker, Data, Response>;
|
|
36
|
-
/**
|
|
37
|
-
* Dynamic pool maximum size property placeholder.
|
|
38
|
-
*/
|
|
39
|
-
protected readonly max?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Whether the pool is starting or not.
|
|
42
|
-
*/
|
|
43
|
-
private readonly starting;
|
|
44
|
-
/**
|
|
45
|
-
* Whether the pool is started or not.
|
|
46
|
-
*/
|
|
47
|
-
private started;
|
|
48
|
-
/**
|
|
49
|
-
* The start timestamp of the pool.
|
|
50
|
-
*/
|
|
51
|
-
private readonly startTimestamp;
|
|
52
|
-
/**
|
|
53
|
-
* Constructs a new poolifier pool.
|
|
54
|
-
*
|
|
55
|
-
* @param numberOfWorkers - Number of workers that this pool should manage.
|
|
56
|
-
* @param filePath - Path to the worker file.
|
|
57
|
-
* @param opts - Options for the pool.
|
|
58
|
-
*/
|
|
59
|
-
constructor(numberOfWorkers: number, filePath: string, opts: PoolOptions<Worker>);
|
|
60
|
-
private checkFilePath;
|
|
61
|
-
private checkNumberOfWorkers;
|
|
62
|
-
protected checkDynamicPoolSize(min: number, max: number): void;
|
|
63
|
-
private checkPoolOptions;
|
|
64
|
-
private checkValidWorkerChoiceStrategy;
|
|
65
|
-
private checkValidWorkerChoiceStrategyOptions;
|
|
66
|
-
private checkValidTasksQueueOptions;
|
|
67
|
-
private startPool;
|
|
68
|
-
/** @inheritDoc */
|
|
69
|
-
get info(): PoolInfo;
|
|
70
|
-
/**
|
|
71
|
-
* The pool readiness boolean status.
|
|
72
|
-
*/
|
|
73
|
-
private get ready();
|
|
74
|
-
/**
|
|
75
|
-
* The approximate pool utilization.
|
|
76
|
-
*
|
|
77
|
-
* @returns The pool utilization.
|
|
78
|
-
*/
|
|
79
|
-
private get utilization();
|
|
80
|
-
/**
|
|
81
|
-
* The pool type.
|
|
82
|
-
*
|
|
83
|
-
* If it is `'dynamic'`, it provides the `max` property.
|
|
84
|
-
*/
|
|
85
|
-
protected abstract get type(): PoolType;
|
|
86
|
-
/**
|
|
87
|
-
* The worker type.
|
|
88
|
-
*/
|
|
89
|
-
protected abstract get worker(): WorkerType;
|
|
90
|
-
/**
|
|
91
|
-
* The pool minimum size.
|
|
92
|
-
*/
|
|
93
|
-
protected get minSize(): number;
|
|
94
|
-
/**
|
|
95
|
-
* The pool maximum size.
|
|
96
|
-
*/
|
|
97
|
-
protected get maxSize(): number;
|
|
98
|
-
/**
|
|
99
|
-
* Checks if the worker id sent in the received message from a worker is valid.
|
|
100
|
-
*
|
|
101
|
-
* @param message - The received message.
|
|
102
|
-
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
|
|
103
|
-
*/
|
|
104
|
-
private checkMessageWorkerId;
|
|
105
|
-
/**
|
|
106
|
-
* Gets the given worker its worker node key.
|
|
107
|
-
*
|
|
108
|
-
* @param worker - The worker.
|
|
109
|
-
* @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
|
|
110
|
-
*/
|
|
111
|
-
private getWorkerNodeKeyByWorker;
|
|
112
|
-
/**
|
|
113
|
-
* Gets the worker node key given its worker id.
|
|
114
|
-
*
|
|
115
|
-
* @param workerId - The worker id.
|
|
116
|
-
* @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
|
|
117
|
-
*/
|
|
118
|
-
private getWorkerNodeKeyByWorkerId;
|
|
119
|
-
/** @inheritDoc */
|
|
120
|
-
setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions): void;
|
|
121
|
-
/** @inheritDoc */
|
|
122
|
-
setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions: WorkerChoiceStrategyOptions): void;
|
|
123
|
-
/** @inheritDoc */
|
|
124
|
-
enableTasksQueue(enable: boolean, tasksQueueOptions?: TasksQueueOptions): void;
|
|
125
|
-
/** @inheritDoc */
|
|
126
|
-
setTasksQueueOptions(tasksQueueOptions: TasksQueueOptions): void;
|
|
127
|
-
private setTasksQueueMaxSize;
|
|
128
|
-
private buildTasksQueueOptions;
|
|
129
|
-
/**
|
|
130
|
-
* Whether the pool is full or not.
|
|
131
|
-
*
|
|
132
|
-
* The pool filling boolean status.
|
|
133
|
-
*/
|
|
134
|
-
protected get full(): boolean;
|
|
135
|
-
/**
|
|
136
|
-
* Whether the pool is busy or not.
|
|
137
|
-
*
|
|
138
|
-
* The pool busyness boolean status.
|
|
139
|
-
*/
|
|
140
|
-
protected abstract get busy(): boolean;
|
|
141
|
-
/**
|
|
142
|
-
* Whether worker nodes are executing concurrently their tasks quota or not.
|
|
143
|
-
*
|
|
144
|
-
* @returns Worker nodes busyness boolean status.
|
|
145
|
-
*/
|
|
146
|
-
protected internalBusy(): boolean;
|
|
147
|
-
/** @inheritDoc */
|
|
148
|
-
listTaskFunctions(): string[];
|
|
149
|
-
private shallExecuteTask;
|
|
150
|
-
/** @inheritDoc */
|
|
151
|
-
execute(data?: Data, name?: string, transferList?: TransferListItem[]): Promise<Response>;
|
|
152
|
-
/** @inheritDoc */
|
|
153
|
-
destroy(): Promise<void>;
|
|
154
|
-
protected sendKillMessageToWorker(workerNodeKey: number, workerId: number): Promise<void>;
|
|
155
|
-
/**
|
|
156
|
-
* Terminates the worker node given its worker node key.
|
|
157
|
-
*
|
|
158
|
-
* @param workerNodeKey - The worker node key.
|
|
159
|
-
*/
|
|
160
|
-
protected abstract destroyWorkerNode(workerNodeKey: number): Promise<void>;
|
|
161
|
-
/**
|
|
162
|
-
* Setup hook to execute code before worker nodes are created in the abstract constructor.
|
|
163
|
-
* Can be overridden.
|
|
164
|
-
*
|
|
165
|
-
* @virtual
|
|
166
|
-
*/
|
|
167
|
-
protected setupHook(): void;
|
|
168
|
-
/**
|
|
169
|
-
* Should return whether the worker is the main worker or not.
|
|
170
|
-
*/
|
|
171
|
-
protected abstract isMain(): boolean;
|
|
172
|
-
/**
|
|
173
|
-
* Hook executed before the worker task execution.
|
|
174
|
-
* Can be overridden.
|
|
175
|
-
*
|
|
176
|
-
* @param workerNodeKey - The worker node key.
|
|
177
|
-
* @param task - The task to execute.
|
|
178
|
-
*/
|
|
179
|
-
protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
|
|
180
|
-
/**
|
|
181
|
-
* Hook executed after the worker task execution.
|
|
182
|
-
* Can be overridden.
|
|
183
|
-
*
|
|
184
|
-
* @param workerNodeKey - The worker node key.
|
|
185
|
-
* @param message - The received message.
|
|
186
|
-
*/
|
|
187
|
-
protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
|
|
188
|
-
/**
|
|
189
|
-
* Whether the worker node shall update its task function worker usage or not.
|
|
190
|
-
*
|
|
191
|
-
* @param workerNodeKey - The worker node key.
|
|
192
|
-
* @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
|
|
193
|
-
*/
|
|
194
|
-
private shallUpdateTaskFunctionWorkerUsage;
|
|
195
|
-
private updateTaskStatisticsWorkerUsage;
|
|
196
|
-
private updateRunTimeWorkerUsage;
|
|
197
|
-
private updateWaitTimeWorkerUsage;
|
|
198
|
-
private updateEluWorkerUsage;
|
|
199
|
-
/**
|
|
200
|
-
* Chooses a worker node for the next task.
|
|
201
|
-
*
|
|
202
|
-
* The default worker choice strategy uses a round robin algorithm to distribute the tasks.
|
|
203
|
-
*
|
|
204
|
-
* @returns The chosen worker node key
|
|
205
|
-
*/
|
|
206
|
-
private chooseWorkerNode;
|
|
207
|
-
/**
|
|
208
|
-
* Conditions for dynamic worker creation.
|
|
209
|
-
*
|
|
210
|
-
* @returns Whether to create a dynamic worker or not.
|
|
211
|
-
*/
|
|
212
|
-
private shallCreateDynamicWorker;
|
|
213
|
-
/**
|
|
214
|
-
* Sends a message to worker given its worker node key.
|
|
215
|
-
*
|
|
216
|
-
* @param workerNodeKey - The worker node key.
|
|
217
|
-
* @param message - The message.
|
|
218
|
-
* @param transferList - The optional array of transferable objects.
|
|
219
|
-
*/
|
|
220
|
-
protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
|
|
221
|
-
/**
|
|
222
|
-
* Creates a new worker.
|
|
223
|
-
*
|
|
224
|
-
* @returns Newly created worker.
|
|
225
|
-
*/
|
|
226
|
-
protected abstract createWorker(): Worker;
|
|
227
|
-
/**
|
|
228
|
-
* Creates a new, completely set up worker node.
|
|
229
|
-
*
|
|
230
|
-
* @returns New, completely set up worker node key.
|
|
231
|
-
*/
|
|
232
|
-
protected createAndSetupWorkerNode(): number;
|
|
233
|
-
/**
|
|
234
|
-
* Creates a new, completely set up dynamic worker node.
|
|
235
|
-
*
|
|
236
|
-
* @returns New, completely set up dynamic worker node key.
|
|
237
|
-
*/
|
|
238
|
-
protected createAndSetupDynamicWorkerNode(): number;
|
|
239
|
-
/**
|
|
240
|
-
* Registers a listener callback on the worker given its worker node key.
|
|
241
|
-
*
|
|
242
|
-
* @param workerNodeKey - The worker node key.
|
|
243
|
-
* @param listener - The message listener callback.
|
|
244
|
-
*/
|
|
245
|
-
protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
|
|
246
|
-
/**
|
|
247
|
-
* Method hooked up after a worker node has been newly created.
|
|
248
|
-
* Can be overridden.
|
|
249
|
-
*
|
|
250
|
-
* @param workerNodeKey - The newly created worker node key.
|
|
251
|
-
*/
|
|
252
|
-
protected afterWorkerNodeSetup(workerNodeKey: number): void;
|
|
253
|
-
/**
|
|
254
|
-
* Sends the startup message to worker given its worker node key.
|
|
255
|
-
*
|
|
256
|
-
* @param workerNodeKey - The worker node key.
|
|
257
|
-
*/
|
|
258
|
-
protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
|
|
259
|
-
/**
|
|
260
|
-
* Sends the statistics message to worker given its worker node key.
|
|
261
|
-
*
|
|
262
|
-
* @param workerNodeKey - The worker node key.
|
|
263
|
-
*/
|
|
264
|
-
private sendStatisticsMessageToWorker;
|
|
265
|
-
private redistributeQueuedTasks;
|
|
266
|
-
private updateTaskStolenStatisticsWorkerUsage;
|
|
267
|
-
private taskStealingOnEmptyQueue;
|
|
268
|
-
private tasksStealingOnBackPressure;
|
|
269
|
-
/**
|
|
270
|
-
* This method is the listener registered for each worker message.
|
|
271
|
-
*
|
|
272
|
-
* @returns The listener function to execute when a message is received from a worker.
|
|
273
|
-
*/
|
|
274
|
-
protected workerListener(): (message: MessageValue<Response>) => void;
|
|
275
|
-
private handleWorkerReadyResponse;
|
|
276
|
-
private handleTaskExecutionResponse;
|
|
277
|
-
private checkAndEmitTaskExecutionEvents;
|
|
278
|
-
private checkAndEmitTaskQueuingEvents;
|
|
279
|
-
private checkAndEmitDynamicWorkerCreationEvents;
|
|
280
|
-
/**
|
|
281
|
-
* Gets the worker information given its worker node key.
|
|
282
|
-
*
|
|
283
|
-
* @param workerNodeKey - The worker node key.
|
|
284
|
-
* @returns The worker information.
|
|
285
|
-
*/
|
|
286
|
-
protected getWorkerInfo(workerNodeKey: number): WorkerInfo | undefined;
|
|
287
|
-
/**
|
|
288
|
-
* Adds the given worker in the pool worker nodes.
|
|
289
|
-
*
|
|
290
|
-
* @param worker - The worker.
|
|
291
|
-
* @returns The added worker node key.
|
|
292
|
-
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
|
|
293
|
-
*/
|
|
294
|
-
private addWorkerNode;
|
|
295
|
-
/**
|
|
296
|
-
* Removes the given worker from the pool worker nodes.
|
|
297
|
-
*
|
|
298
|
-
* @param worker - The worker.
|
|
299
|
-
*/
|
|
300
|
-
private removeWorkerNode;
|
|
301
|
-
/** @inheritDoc */
|
|
302
|
-
hasWorkerNodeBackPressure(workerNodeKey: number): boolean;
|
|
303
|
-
private hasBackPressure;
|
|
304
|
-
/**
|
|
305
|
-
* Executes the given task on the worker given its worker node key.
|
|
306
|
-
*
|
|
307
|
-
* @param workerNodeKey - The worker node key.
|
|
308
|
-
* @param task - The task to execute.
|
|
309
|
-
*/
|
|
310
|
-
private executeTask;
|
|
311
|
-
private enqueueTask;
|
|
312
|
-
private dequeueTask;
|
|
313
|
-
private tasksQueueSize;
|
|
314
|
-
protected flushTasksQueue(workerNodeKey: number): void;
|
|
315
|
-
private flushTasksQueues;
|
|
316
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { type PoolType } from '../pool';
|
|
2
|
-
import { type ClusterPoolOptions, FixedClusterPool } from './fixed';
|
|
3
|
-
/**
|
|
4
|
-
* A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
|
|
5
|
-
*
|
|
6
|
-
* This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
|
|
7
|
-
* When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
|
|
8
|
-
*
|
|
9
|
-
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
|
|
10
|
-
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
|
|
11
|
-
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
|
|
12
|
-
* @since 2.0.0
|
|
13
|
-
*/
|
|
14
|
-
export declare class DynamicClusterPool<Data = unknown, Response = unknown> extends FixedClusterPool<Data, Response> {
|
|
15
|
-
protected readonly max: number;
|
|
16
|
-
/**
|
|
17
|
-
* Constructs a new poolifier dynamic cluster pool.
|
|
18
|
-
*
|
|
19
|
-
* @param min - Minimum number of workers which are always active.
|
|
20
|
-
* @param max - Maximum number of workers that can be created by this pool.
|
|
21
|
-
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
|
|
22
|
-
* @param opts - Options for this dynamic cluster pool.
|
|
23
|
-
*/
|
|
24
|
-
constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
|
|
25
|
-
/** @inheritDoc */
|
|
26
|
-
protected get type(): PoolType;
|
|
27
|
-
/** @inheritDoc */
|
|
28
|
-
protected get busy(): boolean;
|
|
29
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { type ClusterSettings, type Worker } from 'node:cluster';
|
|
3
|
-
import type { MessageValue } from '../../utility-types';
|
|
4
|
-
import { AbstractPool } from '../abstract-pool';
|
|
5
|
-
import { type PoolOptions, type PoolType } from '../pool';
|
|
6
|
-
import { type WorkerType } from '../worker';
|
|
7
|
-
/**
|
|
8
|
-
* Options for a poolifier cluster pool.
|
|
9
|
-
*/
|
|
10
|
-
export interface ClusterPoolOptions extends PoolOptions<Worker> {
|
|
11
|
-
/**
|
|
12
|
-
* Key/value pairs to add to worker process environment.
|
|
13
|
-
*
|
|
14
|
-
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
|
|
15
|
-
*/
|
|
16
|
-
env?: Record<string, unknown>;
|
|
17
|
-
/**
|
|
18
|
-
* Cluster settings.
|
|
19
|
-
*
|
|
20
|
-
* @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
|
|
21
|
-
*/
|
|
22
|
-
settings?: ClusterSettings;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* A cluster pool with a fixed number of workers.
|
|
26
|
-
*
|
|
27
|
-
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
|
|
28
|
-
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
|
|
29
|
-
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
|
|
30
|
-
* @since 2.0.0
|
|
31
|
-
*/
|
|
32
|
-
export declare class FixedClusterPool<Data = unknown, Response = unknown> extends AbstractPool<Worker, Data, Response> {
|
|
33
|
-
protected readonly opts: ClusterPoolOptions;
|
|
34
|
-
/**
|
|
35
|
-
* Constructs a new poolifier fixed cluster pool.
|
|
36
|
-
*
|
|
37
|
-
* @param numberOfWorkers - Number of workers for this pool.
|
|
38
|
-
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
|
|
39
|
-
* @param opts - Options for this fixed cluster pool.
|
|
40
|
-
*/
|
|
41
|
-
constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions);
|
|
42
|
-
/** @inheritDoc */
|
|
43
|
-
protected setupHook(): void;
|
|
44
|
-
/** @inheritDoc */
|
|
45
|
-
protected isMain(): boolean;
|
|
46
|
-
/** @inheritDoc */
|
|
47
|
-
protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
|
|
48
|
-
/** @inheritDoc */
|
|
49
|
-
protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
|
|
50
|
-
/** @inheritDoc */
|
|
51
|
-
protected sendStartupMessageToWorker(workerNodeKey: number): void;
|
|
52
|
-
/** @inheritDoc */
|
|
53
|
-
protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
|
|
54
|
-
/** @inheritDoc */
|
|
55
|
-
protected createWorker(): Worker;
|
|
56
|
-
/** @inheritDoc */
|
|
57
|
-
protected get type(): PoolType;
|
|
58
|
-
/** @inheritDoc */
|
|
59
|
-
protected get worker(): WorkerType;
|
|
60
|
-
/** @inheritDoc */
|
|
61
|
-
protected get busy(): boolean;
|
|
62
|
-
}
|
package/lib/pools/pool.d.ts
DELETED
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { EventEmitter } from 'node:events';
|
|
4
|
-
import { type TransferListItem } from 'node:worker_threads';
|
|
5
|
-
import type { ErrorHandler, ExitHandler, IWorker, IWorkerNode, MessageHandler, OnlineHandler, WorkerType } from './worker';
|
|
6
|
-
import type { WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './selection-strategies/selection-strategies-types';
|
|
7
|
-
/**
|
|
8
|
-
* Enumeration of pool types.
|
|
9
|
-
*/
|
|
10
|
-
export declare const PoolTypes: Readonly<{
|
|
11
|
-
/**
|
|
12
|
-
* Fixed pool type.
|
|
13
|
-
*/
|
|
14
|
-
readonly fixed: "fixed";
|
|
15
|
-
/**
|
|
16
|
-
* Dynamic pool type.
|
|
17
|
-
*/
|
|
18
|
-
readonly dynamic: "dynamic";
|
|
19
|
-
}>;
|
|
20
|
-
/**
|
|
21
|
-
* Pool type.
|
|
22
|
-
*/
|
|
23
|
-
export type PoolType = keyof typeof PoolTypes;
|
|
24
|
-
/**
|
|
25
|
-
* Pool events emitter.
|
|
26
|
-
*/
|
|
27
|
-
export declare class PoolEmitter extends EventEmitter {
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Enumeration of pool events.
|
|
31
|
-
*/
|
|
32
|
-
export declare const PoolEvents: Readonly<{
|
|
33
|
-
readonly ready: "ready";
|
|
34
|
-
readonly busy: "busy";
|
|
35
|
-
readonly full: "full";
|
|
36
|
-
readonly destroy: "destroy";
|
|
37
|
-
readonly error: "error";
|
|
38
|
-
readonly taskError: "taskError";
|
|
39
|
-
readonly backPressure: "backPressure";
|
|
40
|
-
}>;
|
|
41
|
-
/**
|
|
42
|
-
* Pool event.
|
|
43
|
-
*/
|
|
44
|
-
export type PoolEvent = keyof typeof PoolEvents;
|
|
45
|
-
/**
|
|
46
|
-
* Pool information.
|
|
47
|
-
*/
|
|
48
|
-
export interface PoolInfo {
|
|
49
|
-
readonly version: string;
|
|
50
|
-
readonly type: PoolType;
|
|
51
|
-
readonly worker: WorkerType;
|
|
52
|
-
readonly ready: boolean;
|
|
53
|
-
readonly strategy: WorkerChoiceStrategy;
|
|
54
|
-
readonly minSize: number;
|
|
55
|
-
readonly maxSize: number;
|
|
56
|
-
/** Pool utilization. */
|
|
57
|
-
readonly utilization?: number;
|
|
58
|
-
/** Pool total worker nodes. */
|
|
59
|
-
readonly workerNodes: number;
|
|
60
|
-
/** Pool idle worker nodes. */
|
|
61
|
-
readonly idleWorkerNodes: number;
|
|
62
|
-
/** Pool busy worker nodes. */
|
|
63
|
-
readonly busyWorkerNodes: number;
|
|
64
|
-
readonly executedTasks: number;
|
|
65
|
-
readonly executingTasks: number;
|
|
66
|
-
readonly queuedTasks?: number;
|
|
67
|
-
readonly maxQueuedTasks?: number;
|
|
68
|
-
readonly backPressure?: boolean;
|
|
69
|
-
readonly stolenTasks?: number;
|
|
70
|
-
readonly failedTasks: number;
|
|
71
|
-
readonly runTime?: {
|
|
72
|
-
readonly minimum: number;
|
|
73
|
-
readonly maximum: number;
|
|
74
|
-
readonly average?: number;
|
|
75
|
-
readonly median?: number;
|
|
76
|
-
};
|
|
77
|
-
readonly waitTime?: {
|
|
78
|
-
readonly minimum: number;
|
|
79
|
-
readonly maximum: number;
|
|
80
|
-
readonly average?: number;
|
|
81
|
-
readonly median?: number;
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Worker node tasks queue options.
|
|
86
|
-
*/
|
|
87
|
-
export interface TasksQueueOptions {
|
|
88
|
-
/**
|
|
89
|
-
* Maximum tasks queue size per worker node flagging it as back pressured.
|
|
90
|
-
*
|
|
91
|
-
* @defaultValue (pool maximum size)^2
|
|
92
|
-
*/
|
|
93
|
-
readonly size?: number;
|
|
94
|
-
/**
|
|
95
|
-
* @deprecated Use `size` instead.
|
|
96
|
-
*/
|
|
97
|
-
readonly queueMaxSize?: number;
|
|
98
|
-
/**
|
|
99
|
-
* Maximum number of tasks that can be executed concurrently on a worker node.
|
|
100
|
-
*
|
|
101
|
-
* @defaultValue 1
|
|
102
|
-
*/
|
|
103
|
-
readonly concurrency?: number;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Options for a poolifier pool.
|
|
107
|
-
*
|
|
108
|
-
* @typeParam Worker - Type of worker.
|
|
109
|
-
*/
|
|
110
|
-
export interface PoolOptions<Worker extends IWorker> {
|
|
111
|
-
/**
|
|
112
|
-
* A function that will listen for online event on each worker.
|
|
113
|
-
*/
|
|
114
|
-
onlineHandler?: OnlineHandler<Worker>;
|
|
115
|
-
/**
|
|
116
|
-
* A function that will listen for message event on each worker.
|
|
117
|
-
*/
|
|
118
|
-
messageHandler?: MessageHandler<Worker>;
|
|
119
|
-
/**
|
|
120
|
-
* A function that will listen for error event on each worker.
|
|
121
|
-
*/
|
|
122
|
-
errorHandler?: ErrorHandler<Worker>;
|
|
123
|
-
/**
|
|
124
|
-
* A function that will listen for exit event on each worker.
|
|
125
|
-
*/
|
|
126
|
-
exitHandler?: ExitHandler<Worker>;
|
|
127
|
-
/**
|
|
128
|
-
* The worker choice strategy to use in this pool.
|
|
129
|
-
*
|
|
130
|
-
* @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
|
|
131
|
-
*/
|
|
132
|
-
workerChoiceStrategy?: WorkerChoiceStrategy;
|
|
133
|
-
/**
|
|
134
|
-
* The worker choice strategy options.
|
|
135
|
-
*/
|
|
136
|
-
workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
|
|
137
|
-
/**
|
|
138
|
-
* Restart worker on error.
|
|
139
|
-
*/
|
|
140
|
-
restartWorkerOnError?: boolean;
|
|
141
|
-
/**
|
|
142
|
-
* Pool events emission.
|
|
143
|
-
*
|
|
144
|
-
* @defaultValue true
|
|
145
|
-
*/
|
|
146
|
-
enableEvents?: boolean;
|
|
147
|
-
/**
|
|
148
|
-
* Pool worker node tasks queue.
|
|
149
|
-
*
|
|
150
|
-
* @defaultValue false
|
|
151
|
-
*/
|
|
152
|
-
enableTasksQueue?: boolean;
|
|
153
|
-
/**
|
|
154
|
-
* Pool worker node tasks queue options.
|
|
155
|
-
*/
|
|
156
|
-
tasksQueueOptions?: TasksQueueOptions;
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Contract definition for a poolifier pool.
|
|
160
|
-
*
|
|
161
|
-
* @typeParam Worker - Type of worker which manages this pool.
|
|
162
|
-
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
|
|
163
|
-
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
|
|
164
|
-
*/
|
|
165
|
-
export interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
|
|
166
|
-
/**
|
|
167
|
-
* Pool information.
|
|
168
|
-
*/
|
|
169
|
-
readonly info: PoolInfo;
|
|
170
|
-
/**
|
|
171
|
-
* Pool worker nodes.
|
|
172
|
-
*
|
|
173
|
-
* @internal
|
|
174
|
-
*/
|
|
175
|
-
readonly workerNodes: Array<IWorkerNode<Worker, Data>>;
|
|
176
|
-
/**
|
|
177
|
-
* Whether the worker node has back pressure (i.e. its tasks queue is full).
|
|
178
|
-
*
|
|
179
|
-
* @param workerNodeKey - The worker node key.
|
|
180
|
-
* @returns `true` if the worker node has back pressure, `false` otherwise.
|
|
181
|
-
* @internal
|
|
182
|
-
*/
|
|
183
|
-
readonly hasWorkerNodeBackPressure: (workerNodeKey: number) => boolean;
|
|
184
|
-
/**
|
|
185
|
-
* Emitter on which events can be listened to.
|
|
186
|
-
*
|
|
187
|
-
* Events that can currently be listened to:
|
|
188
|
-
*
|
|
189
|
-
* - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
|
|
190
|
-
* - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
|
|
191
|
-
* - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
|
|
192
|
-
* - `'destroy'`: Emitted when the pool is destroyed.
|
|
193
|
-
* - `'error'`: Emitted when an uncaught error occurs.
|
|
194
|
-
* - `'taskError'`: Emitted when an error occurs while executing a task.
|
|
195
|
-
* - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
|
|
196
|
-
*/
|
|
197
|
-
readonly emitter?: PoolEmitter;
|
|
198
|
-
/**
|
|
199
|
-
* Executes the specified function in the worker constructor with the task data input parameter.
|
|
200
|
-
*
|
|
201
|
-
* @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
|
|
202
|
-
* @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
|
|
203
|
-
* @param transferList - An optional array of transferable objects to transfer ownership of. Ownership of the transferred objects is given to the pool's worker_threads worker and they should not be used in the main thread afterwards.
|
|
204
|
-
* @returns Promise that will be fulfilled when the task is completed.
|
|
205
|
-
*/
|
|
206
|
-
readonly execute: (data?: Data, name?: string, transferList?: TransferListItem[]) => Promise<Response>;
|
|
207
|
-
/**
|
|
208
|
-
* Terminates all workers in this pool.
|
|
209
|
-
*/
|
|
210
|
-
readonly destroy: () => Promise<void>;
|
|
211
|
-
/**
|
|
212
|
-
* Lists the names of task function available in this pool.
|
|
213
|
-
*
|
|
214
|
-
* @returns The names of task function available in this pool.
|
|
215
|
-
*/
|
|
216
|
-
readonly listTaskFunctions: () => string[];
|
|
217
|
-
/**
|
|
218
|
-
* Sets the worker choice strategy in this pool.
|
|
219
|
-
*
|
|
220
|
-
* @param workerChoiceStrategy - The worker choice strategy.
|
|
221
|
-
* @param workerChoiceStrategyOptions - The worker choice strategy options.
|
|
222
|
-
*/
|
|
223
|
-
readonly setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions) => void;
|
|
224
|
-
/**
|
|
225
|
-
* Sets the worker choice strategy options in this pool.
|
|
226
|
-
*
|
|
227
|
-
* @param workerChoiceStrategyOptions - The worker choice strategy options.
|
|
228
|
-
*/
|
|
229
|
-
readonly setWorkerChoiceStrategyOptions: (workerChoiceStrategyOptions: WorkerChoiceStrategyOptions) => void;
|
|
230
|
-
/**
|
|
231
|
-
* Enables/disables the worker node tasks queue in this pool.
|
|
232
|
-
*
|
|
233
|
-
* @param enable - Whether to enable or disable the worker node tasks queue.
|
|
234
|
-
* @param tasksQueueOptions - The worker node tasks queue options.
|
|
235
|
-
*/
|
|
236
|
-
readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
|
|
237
|
-
/**
|
|
238
|
-
* Sets the worker node tasks queue options in this pool.
|
|
239
|
-
*
|
|
240
|
-
* @param tasksQueueOptions - The worker node tasks queue options.
|
|
241
|
-
*/
|
|
242
|
-
readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
|
|
243
|
-
}
|