poolifier 2.6.36 → 2.6.38

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.
Files changed (33) hide show
  1. package/README.md +6 -3
  2. package/lib/index.d.ts +1881 -22
  3. package/lib/index.js +1 -1
  4. package/lib/index.mjs +1 -1
  5. package/package.json +8 -7
  6. package/lib/circular-array.d.ts +0 -22
  7. package/lib/deque.d.ts +0 -80
  8. package/lib/pools/abstract-pool.d.ts +0 -314
  9. package/lib/pools/cluster/dynamic.d.ts +0 -29
  10. package/lib/pools/cluster/fixed.d.ts +0 -62
  11. package/lib/pools/pool.d.ts +0 -243
  12. package/lib/pools/selection-strategies/abstract-worker-choice-strategy.d.ts +0 -104
  13. package/lib/pools/selection-strategies/fair-share-worker-choice-strategy.d.ts +0 -39
  14. package/lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.d.ts +0 -39
  15. package/lib/pools/selection-strategies/least-busy-worker-choice-strategy.d.ts +0 -26
  16. package/lib/pools/selection-strategies/least-elu-worker-choice-strategy.d.ts +0 -26
  17. package/lib/pools/selection-strategies/least-used-worker-choice-strategy.d.ts +0 -24
  18. package/lib/pools/selection-strategies/round-robin-worker-choice-strategy.d.ts +0 -24
  19. package/lib/pools/selection-strategies/selection-strategies-types.d.ts +0 -200
  20. package/lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.d.ts +0 -35
  21. package/lib/pools/selection-strategies/worker-choice-strategy-context.d.ts +0 -71
  22. package/lib/pools/thread/dynamic.d.ts +0 -29
  23. package/lib/pools/thread/fixed.d.ts +0 -54
  24. package/lib/pools/version.d.ts +0 -1
  25. package/lib/pools/worker-node.d.ts +0 -69
  26. package/lib/pools/worker.d.ts +0 -276
  27. package/lib/utility-types.d.ts +0 -152
  28. package/lib/utils.d.ts +0 -115
  29. package/lib/worker/abstract-worker.d.ts +0 -191
  30. package/lib/worker/cluster-worker.d.ts +0 -35
  31. package/lib/worker/task-functions.d.ts +0 -33
  32. package/lib/worker/thread-worker.d.ts +0 -43
  33. package/lib/worker/worker-options.d.ts +0 -61
package/lib/deque.d.ts DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * @internal
3
- */
4
- export declare class Node<T> {
5
- value: T;
6
- next?: Node<T>;
7
- prev?: Node<T>;
8
- constructor(value: T);
9
- }
10
- /**
11
- * Deque.
12
- * Implemented with a doubly linked list.
13
- *
14
- * @typeParam T - Type of deque values.
15
- * @internal
16
- */
17
- export declare class Deque<T> {
18
- private head?;
19
- private tail?;
20
- /** The size of the deque. */
21
- size: number;
22
- /** The maximum size of the deque. */
23
- maxSize: number;
24
- constructor();
25
- /**
26
- * Appends a value to the deque.
27
- *
28
- * @param value - Value to append.
29
- * @returns The new size of the queue.
30
- */
31
- push(value: T): number;
32
- /**
33
- * Prepends a value to the deque.
34
- *
35
- * @param value - Value to prepend.
36
- * @returns The new size of the queue.
37
- */
38
- unshift(value: T): number;
39
- /**
40
- * Pops a value from the deque.
41
- *
42
- * @returns The popped value or `undefined` if the deque is empty.
43
- */
44
- pop(): T | undefined;
45
- /**
46
- * Shifts a value from the deque.
47
- *
48
- * @returns The shifted value or `undefined` if the deque is empty.
49
- */
50
- shift(): T | undefined;
51
- /**
52
- * Peeks at the first value.
53
- * @returns The first value or `undefined` if the deque is empty.
54
- */
55
- peekFirst(): T | undefined;
56
- /**
57
- * Peeks at the last value.
58
- * @returns The last value or `undefined` if the deque is empty.
59
- */
60
- peekLast(): T | undefined;
61
- /**
62
- * Clears the deque.
63
- */
64
- clear(): void;
65
- /**
66
- * Returns an iterator for the deque.
67
- *
68
- * @returns An iterator for the deque.
69
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
70
- */
71
- [Symbol.iterator](): Iterator<T>;
72
- /**
73
- * Returns an backward iterator for the deque.
74
- *
75
- * @returns An backward iterator for the deque.
76
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
77
- */
78
- backward(): Iterable<T>;
79
- private incrementSize;
80
- }
@@ -1,314 +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
- /** @inheritDoc */
150
- execute(data?: Data, name?: string, transferList?: TransferListItem[]): Promise<Response>;
151
- /** @inheritDoc */
152
- destroy(): Promise<void>;
153
- protected sendKillMessageToWorker(workerNodeKey: number, workerId: number): Promise<void>;
154
- /**
155
- * Terminates the worker node given its worker node key.
156
- *
157
- * @param workerNodeKey - The worker node key.
158
- */
159
- protected abstract destroyWorkerNode(workerNodeKey: number): Promise<void>;
160
- /**
161
- * Setup hook to execute code before worker nodes are created in the abstract constructor.
162
- * Can be overridden.
163
- *
164
- * @virtual
165
- */
166
- protected setupHook(): void;
167
- /**
168
- * Should return whether the worker is the main worker or not.
169
- */
170
- protected abstract isMain(): boolean;
171
- /**
172
- * Hook executed before the worker task execution.
173
- * Can be overridden.
174
- *
175
- * @param workerNodeKey - The worker node key.
176
- * @param task - The task to execute.
177
- */
178
- protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
179
- /**
180
- * Hook executed after the worker task execution.
181
- * Can be overridden.
182
- *
183
- * @param workerNodeKey - The worker node key.
184
- * @param message - The received message.
185
- */
186
- protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
187
- /**
188
- * Whether the worker node shall update its task function worker usage or not.
189
- *
190
- * @param workerNodeKey - The worker node key.
191
- * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
192
- */
193
- private shallUpdateTaskFunctionWorkerUsage;
194
- private updateTaskStatisticsWorkerUsage;
195
- private updateRunTimeWorkerUsage;
196
- private updateWaitTimeWorkerUsage;
197
- private updateEluWorkerUsage;
198
- /**
199
- * Chooses a worker node for the next task.
200
- *
201
- * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
202
- *
203
- * @returns The chosen worker node key
204
- */
205
- private chooseWorkerNode;
206
- /**
207
- * Conditions for dynamic worker creation.
208
- *
209
- * @returns Whether to create a dynamic worker or not.
210
- */
211
- private shallCreateDynamicWorker;
212
- /**
213
- * Sends a message to worker given its worker node key.
214
- *
215
- * @param workerNodeKey - The worker node key.
216
- * @param message - The message.
217
- * @param transferList - The optional array of transferable objects.
218
- */
219
- protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
220
- /**
221
- * Creates a new worker.
222
- *
223
- * @returns Newly created worker.
224
- */
225
- protected abstract createWorker(): Worker;
226
- /**
227
- * Creates a new, completely set up worker node.
228
- *
229
- * @returns New, completely set up worker node key.
230
- */
231
- protected createAndSetupWorkerNode(): number;
232
- /**
233
- * Creates a new, completely set up dynamic worker node.
234
- *
235
- * @returns New, completely set up dynamic worker node key.
236
- */
237
- protected createAndSetupDynamicWorkerNode(): number;
238
- /**
239
- * Registers a listener callback on the worker given its worker node key.
240
- *
241
- * @param workerNodeKey - The worker node key.
242
- * @param listener - The message listener callback.
243
- */
244
- protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
245
- /**
246
- * Method hooked up after a worker node has been newly created.
247
- * Can be overridden.
248
- *
249
- * @param workerNodeKey - The newly created worker node key.
250
- */
251
- protected afterWorkerNodeSetup(workerNodeKey: number): void;
252
- /**
253
- * Sends the startup message to worker given its worker node key.
254
- *
255
- * @param workerNodeKey - The worker node key.
256
- */
257
- protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
258
- /**
259
- * Sends the statistics message to worker given its worker node key.
260
- *
261
- * @param workerNodeKey - The worker node key.
262
- */
263
- private sendStatisticsMessageToWorker;
264
- private redistributeQueuedTasks;
265
- private taskStealingOnEmptyQueue;
266
- private tasksStealingOnBackPressure;
267
- /**
268
- * This method is the listener registered for each worker message.
269
- *
270
- * @returns The listener function to execute when a message is received from a worker.
271
- */
272
- protected workerListener(): (message: MessageValue<Response>) => void;
273
- private handleWorkerReadyResponse;
274
- private handleTaskExecutionResponse;
275
- private checkAndEmitTaskExecutionEvents;
276
- private checkAndEmitTaskQueuingEvents;
277
- private checkAndEmitDynamicWorkerCreationEvents;
278
- /**
279
- * Gets the worker information given its worker node key.
280
- *
281
- * @param workerNodeKey - The worker node key.
282
- * @returns The worker information.
283
- */
284
- protected getWorkerInfo(workerNodeKey: number): WorkerInfo | undefined;
285
- /**
286
- * Adds the given worker in the pool worker nodes.
287
- *
288
- * @param worker - The worker.
289
- * @returns The added worker node key.
290
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
291
- */
292
- private addWorkerNode;
293
- /**
294
- * Removes the given worker from the pool worker nodes.
295
- *
296
- * @param worker - The worker.
297
- */
298
- private removeWorkerNode;
299
- /** @inheritDoc */
300
- hasWorkerNodeBackPressure(workerNodeKey: number): boolean;
301
- private hasBackPressure;
302
- /**
303
- * Executes the given task on the worker given its worker node key.
304
- *
305
- * @param workerNodeKey - The worker node key.
306
- * @param task - The task to execute.
307
- */
308
- private executeTask;
309
- private enqueueTask;
310
- private dequeueTask;
311
- private tasksQueueSize;
312
- protected flushTasksQueue(workerNodeKey: number): void;
313
- private flushTasksQueues;
314
- }
@@ -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
- }