poolifier 2.6.37 → 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 +4 -3
  6. package/lib/circular-array.d.ts +0 -22
  7. package/lib/deque.d.ts +0 -83
  8. package/lib/pools/abstract-pool.d.ts +0 -316
  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 -60
  26. package/lib/pools/worker.d.ts +0 -277
  27. package/lib/utility-types.d.ts +0 -152
  28. package/lib/utils.d.ts +0 -123
  29. package/lib/worker/abstract-worker.d.ts +0 -192
  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
@@ -1,192 +0,0 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
- /// <reference types="node" />
5
- import { AsyncResource } from 'node:async_hooks';
6
- import type { Worker } from 'node:cluster';
7
- import type { MessagePort } from 'node:worker_threads';
8
- import type { MessageValue, Task, WorkerStatistics } from '../utility-types';
9
- import { type WorkerOptions } from './worker-options';
10
- import type { TaskAsyncFunction, TaskFunction, TaskFunctions, TaskSyncFunction } from './task-functions';
11
- /**
12
- * Base class that implements some shared logic for all poolifier workers.
13
- *
14
- * @typeParam MainWorker - Type of main worker.
15
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
16
- * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
17
- */
18
- export declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> extends AsyncResource {
19
- protected readonly isMain: boolean;
20
- private readonly mainWorker;
21
- protected opts: WorkerOptions;
22
- /**
23
- * Worker id.
24
- */
25
- protected abstract id: number;
26
- /**
27
- * Task function(s) processed by the worker when the pool's `execution` function is invoked.
28
- */
29
- protected taskFunctions: Map<string, TaskFunction<Data, Response>>;
30
- /**
31
- * Timestamp of the last task processed by this worker.
32
- */
33
- protected lastTaskTimestamp: number;
34
- /**
35
- * Performance statistics computation requirements.
36
- */
37
- protected statistics: WorkerStatistics;
38
- /**
39
- * Handler id of the `activeInterval` worker activity check.
40
- */
41
- protected activeInterval?: NodeJS.Timeout;
42
- /**
43
- * Constructs a new poolifier worker.
44
- *
45
- * @param type - The type of async event.
46
- * @param isMain - Whether this is the main worker or not.
47
- * @param mainWorker - Reference to main worker.
48
- * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
49
- * @param opts - Options for the worker.
50
- */
51
- constructor(type: string, isMain: boolean, mainWorker: MainWorker, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
52
- private checkWorkerOptions;
53
- private checkValidTaskFunction;
54
- /**
55
- * Checks if the `taskFunctions` parameter is passed to the constructor.
56
- *
57
- * @param taskFunctions - The task function(s) parameter that should be checked.
58
- */
59
- private checkTaskFunctions;
60
- /**
61
- * Checks if the worker has a task function with the given name.
62
- *
63
- * @param name - The name of the task function to check.
64
- * @returns Whether the worker has a task function with the given name or not.
65
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
66
- */
67
- hasTaskFunction(name: string): boolean;
68
- /**
69
- * Adds a task function to the worker.
70
- * If a task function with the same name already exists, it is replaced.
71
- *
72
- * @param name - The name of the task function to add.
73
- * @param fn - The task function to add.
74
- * @returns Whether the task function was added or not.
75
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
76
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
77
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
78
- */
79
- addTaskFunction(name: string, fn: TaskFunction<Data, Response>): boolean;
80
- /**
81
- * Removes a task function from the worker.
82
- *
83
- * @param name - The name of the task function to remove.
84
- * @returns Whether the task function existed and was removed or not.
85
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
86
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
87
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
88
- */
89
- removeTaskFunction(name: string): boolean;
90
- /**
91
- * Lists the names of the worker's task functions.
92
- *
93
- * @returns The names of the worker's task functions.
94
- */
95
- listTaskFunctions(): string[];
96
- /**
97
- * Sets the default task function to use in the worker.
98
- *
99
- * @param name - The name of the task function to use as default task function.
100
- * @returns Whether the default task function was set or not.
101
- * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
102
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
103
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
104
- */
105
- setDefaultTaskFunction(name: string): boolean;
106
- private checkTaskFunctionName;
107
- /**
108
- * Handles the ready message sent by the main worker.
109
- *
110
- * @param message - The ready message.
111
- */
112
- protected abstract handleReadyMessage(message: MessageValue<Data>): void;
113
- /**
114
- * Worker message listener.
115
- *
116
- * @param message - The received message.
117
- */
118
- protected messageListener(message: MessageValue<Data>): void;
119
- /**
120
- * Handles a kill message sent by the main worker.
121
- *
122
- * @param message - The kill message.
123
- */
124
- protected handleKillMessage(message: MessageValue<Data>): void;
125
- /**
126
- * Check if the message worker id is set and matches the worker id.
127
- *
128
- * @param message - The message to check.
129
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
130
- */
131
- private checkMessageWorkerId;
132
- /**
133
- * Starts the worker check active interval.
134
- */
135
- private startCheckActive;
136
- /**
137
- * Stops the worker check active interval.
138
- */
139
- private stopCheckActive;
140
- /**
141
- * Checks if the worker should be terminated, because its living too long.
142
- */
143
- private checkActive;
144
- /**
145
- * Returns the main worker.
146
- *
147
- * @returns Reference to the main worker.
148
- */
149
- protected getMainWorker(): MainWorker;
150
- /**
151
- * Sends a message to main worker.
152
- *
153
- * @param message - The response message.
154
- */
155
- protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
156
- /**
157
- * Sends the list of task function names to the main worker.
158
- */
159
- protected sendTaskFunctionsListToMainWorker(): void;
160
- /**
161
- * Handles an error and convert it to a string so it can be sent back to the main worker.
162
- *
163
- * @param e - The error raised by the worker.
164
- * @returns The error message.
165
- */
166
- protected handleError(e: Error | string): string;
167
- /**
168
- * Runs the given task.
169
- *
170
- * @param task - The task to execute.
171
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
172
- */
173
- protected run(task: Task<Data>): void;
174
- /**
175
- * Runs the given task function synchronously.
176
- *
177
- * @param fn - Task function that will be executed.
178
- * @param task - Input data for the task function.
179
- */
180
- protected runSync(fn: TaskSyncFunction<Data, Response>, task: Task<Data>): void;
181
- /**
182
- * Runs the given task function asynchronously.
183
- *
184
- * @param fn - Task function that will be executed.
185
- * @param task - Input data for the task function.
186
- */
187
- protected runAsync(fn: TaskAsyncFunction<Data, Response>, task: Task<Data>): void;
188
- private beginTaskPerformance;
189
- private endTaskPerformance;
190
- private checkStatistics;
191
- private updateLastTaskTimestamp;
192
- }
@@ -1,35 +0,0 @@
1
- /// <reference types="node" />
2
- import { type Worker } from 'node:cluster';
3
- import type { MessageValue } from '../utility-types';
4
- import { AbstractWorker } from './abstract-worker';
5
- import type { WorkerOptions } from './worker-options';
6
- import type { TaskFunction, TaskFunctions } from './task-functions';
7
- /**
8
- * A cluster worker used by a poolifier `ClusterPool`.
9
- *
10
- * When this worker is inactive for more than the given `maxInactiveTime`,
11
- * it will send a termination request to its main worker.
12
- *
13
- * If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
14
- * but the minimum number of workers will be guaranteed.
15
- *
16
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
17
- * @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
18
- * @author [Christopher Quadflieg](https://github.com/Shinigami92)
19
- * @since 2.0.0
20
- */
21
- export declare class ClusterWorker<Data = unknown, Response = unknown> extends AbstractWorker<Worker, Data, Response> {
22
- /**
23
- * Constructs a new poolifier cluster worker.
24
- *
25
- * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
26
- * @param opts - Options for the worker.
27
- */
28
- constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
29
- /** @inheritDoc */
30
- protected handleReadyMessage(message: MessageValue<Data>): void;
31
- /** @inheritDoc */
32
- protected get id(): number;
33
- /** @inheritDoc */
34
- protected sendToMainWorker(message: MessageValue<Response>): void;
35
- }
@@ -1,33 +0,0 @@
1
- /**
2
- * Task synchronous function that can be executed.
3
- *
4
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
5
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
6
- */
7
- export type TaskSyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Response;
8
- /**
9
- * Task asynchronous function that can be executed.
10
- * This function must return a promise.
11
- *
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 type TaskAsyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Promise<Response>;
16
- /**
17
- * Task function that can be executed.
18
- * This function can be synchronous or asynchronous.
19
- *
20
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
21
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
22
- */
23
- export type TaskFunction<Data = unknown, Response = unknown> = TaskSyncFunction<Data, Response> | TaskAsyncFunction<Data, Response>;
24
- /**
25
- * Tasks functions that can be executed.
26
- * This object can contain synchronous or asynchronous functions.
27
- * The key is the name of the function.
28
- * The value is the function itself.
29
- *
30
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
31
- * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
32
- */
33
- export type TaskFunctions<Data = unknown, Response = unknown> = Record<string, TaskFunction<Data, Response>>;
@@ -1,43 +0,0 @@
1
- /// <reference types="node" />
2
- import { type MessagePort } from 'node:worker_threads';
3
- import type { MessageValue } from '../utility-types';
4
- import { AbstractWorker } from './abstract-worker';
5
- import type { WorkerOptions } from './worker-options';
6
- import type { TaskFunction, TaskFunctions } from './task-functions';
7
- /**
8
- * A thread worker used by a poolifier `ThreadPool`.
9
- *
10
- * When this worker is inactive for more than the given `maxInactiveTime`,
11
- * it will send a termination request to its main thread.
12
- *
13
- * If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
14
- * but the minimum number of workers will be guaranteed.
15
- *
16
- * @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
17
- * @typeParam Response - Type of response the worker sends back to the main thread. This can only be structured-cloneable data.
18
- * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
19
- * @since 0.0.1
20
- */
21
- export declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractWorker<MessagePort, Data, Response> {
22
- /**
23
- * Message port used to communicate with the main worker.
24
- */
25
- private port;
26
- /**
27
- * Constructs a new poolifier thread worker.
28
- *
29
- * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
30
- * @param opts - Options for the worker.
31
- */
32
- constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
33
- /** @inheritDoc */
34
- protected handleReadyMessage(message: MessageValue<Data>): void;
35
- /** @inheritDoc */
36
- protected handleKillMessage(message: MessageValue<Data>): void;
37
- /** @inheritDoc */
38
- protected get id(): number;
39
- /** @inheritDoc */
40
- protected sendToMainWorker(message: MessageValue<Response>): void;
41
- /** @inheritDoc */
42
- protected handleError(e: Error | string): string;
43
- }
@@ -1,61 +0,0 @@
1
- /**
2
- * Enumeration of kill behaviors.
3
- */
4
- export declare const KillBehaviors: Readonly<{
5
- /**
6
- * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **wont** be deleted.
7
- */
8
- readonly SOFT: "SOFT";
9
- /**
10
- * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
11
- */
12
- readonly HARD: "HARD";
13
- }>;
14
- /**
15
- * Kill behavior.
16
- */
17
- export type KillBehavior = keyof typeof KillBehaviors;
18
- /**
19
- * Handler called when a worker is killed.
20
- */
21
- export type KillHandler = () => void | Promise<void>;
22
- /**
23
- * Options for workers.
24
- */
25
- export interface WorkerOptions {
26
- /**
27
- * `killBehavior` dictates if your worker will be deleted in case a task is active on it.
28
- *
29
- * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
30
- * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
31
- *
32
- * This option only apply to the newly created workers.
33
- *
34
- * @defaultValue KillBehaviors.SOFT
35
- */
36
- killBehavior?: KillBehavior;
37
- /**
38
- * Maximum waiting time in milliseconds for tasks on newly created workers.
39
- *
40
- * After this time, newly created workers will be terminated.
41
- * The last active time of your worker will be updated when it terminates a task.
42
- *
43
- * - If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool,
44
- * when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
45
- * - If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
46
- *
47
- * @defaultValue 60000
48
- */
49
- maxInactiveTime?: number;
50
- /**
51
- * The function to call when a worker is killed.
52
- */
53
- killHandler?: KillHandler;
54
- /**
55
- * Whether your worker will perform asynchronous or not.
56
- *
57
- * @defaultValue false
58
- * @deprecated This option will be removed in the next major version.
59
- */
60
- async?: boolean;
61
- }