1 import { EventEmitterAsyncResource
} from
'node:events'
2 import type { TransferListItem
} from
'node:worker_threads'
3 import type { TaskFunction
} from
'../worker/task-functions'
15 WorkerChoiceStrategyOptions
16 } from
'./selection-strategies/selection-strategies-types'
19 * Enumeration of pool types.
21 export const PoolTypes
= Object.freeze({
35 export type PoolType
= keyof
typeof PoolTypes
38 * Pool event emitter integrated with async resource.
40 export class PoolEmitter
extends EventEmitterAsyncResource
{}
43 * Enumeration of pool events.
45 export const PoolEvents
= Object.freeze({
51 taskError
: 'taskError',
52 backPressure
: 'backPressure'
58 export type PoolEvent
= keyof
typeof PoolEvents
63 export interface PoolInfo
{
64 readonly version
: string
65 readonly type: PoolType
66 readonly worker
: WorkerType
67 readonly started
: boolean
68 readonly ready
: boolean
69 readonly strategy
: WorkerChoiceStrategy
70 readonly minSize
: number
71 readonly maxSize
: number
72 /** Pool utilization. */
73 readonly utilization
?: number
74 /** Pool total worker nodes. */
75 readonly workerNodes
: number
76 /** Pool idle worker nodes. */
77 readonly idleWorkerNodes
: number
78 /** Pool busy worker nodes. */
79 readonly busyWorkerNodes
: number
80 readonly executedTasks
: number
81 readonly executingTasks
: number
82 readonly queuedTasks
?: number
83 readonly maxQueuedTasks
?: number
84 readonly backPressure
?: boolean
85 readonly stolenTasks
?: number
86 readonly failedTasks
: number
88 readonly minimum
: number
89 readonly maximum
: number
90 readonly average
?: number
91 readonly median
?: number
94 readonly minimum
: number
95 readonly maximum
: number
96 readonly average
?: number
97 readonly median
?: number
102 * Worker node tasks queue options.
104 export interface TasksQueueOptions
{
106 * Maximum tasks queue size per worker node flagging it as back pressured.
108 * @defaultValue (pool maximum size)^2
110 readonly size
?: number
112 * Maximum number of tasks that can be executed concurrently on a worker node.
116 readonly concurrency
?: number
118 * Whether to enable task stealing.
122 readonly taskStealing
?: boolean
124 * Whether to enable tasks stealing on back pressure.
128 readonly tasksStealingOnBackPressure
?: boolean
132 * Options for a poolifier pool.
134 * @typeParam Worker - Type of worker.
136 export interface PoolOptions
<Worker
extends IWorker
> {
138 * A function that will listen for online event on each worker.
140 * @defaultValue `() => {}`
142 onlineHandler
?: OnlineHandler
<Worker
>
144 * A function that will listen for message event on each worker.
146 * @defaultValue `() => {}`
148 messageHandler
?: MessageHandler
<Worker
>
150 * A function that will listen for error event on each worker.
152 * @defaultValue `() => {}`
154 errorHandler
?: ErrorHandler
<Worker
>
156 * A function that will listen for exit event on each worker.
158 * @defaultValue `() => {}`
160 exitHandler
?: ExitHandler
<Worker
>
162 * Whether to start the minimum number of workers at pool initialization.
166 startWorkers
?: boolean
168 * The worker choice strategy to use in this pool.
170 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
172 workerChoiceStrategy
?: WorkerChoiceStrategy
174 * The worker choice strategy options.
176 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
178 * Restart worker on error.
180 restartWorkerOnError
?: boolean
182 * Pool events integrated with async resource emission.
186 enableEvents
?: boolean
188 * Pool worker node tasks queue.
190 * @defaultValue false
192 enableTasksQueue
?: boolean
194 * Pool worker node tasks queue options.
196 tasksQueueOptions
?: TasksQueueOptions
200 * Contract definition for a poolifier pool.
202 * @typeParam Worker - Type of worker which manages this pool.
203 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
204 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
206 export interface IPool
<
207 Worker
extends IWorker
,
214 readonly info
: PoolInfo
220 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
222 * Whether the worker node has back pressure (i.e. its tasks queue is full).
224 * @param workerNodeKey - The worker node key.
225 * @returns `true` if the worker node has back pressure, `false` otherwise.
228 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
230 * Event emitter integrated with async resource on which events can be listened to.
231 * The async tracking tooling identifier is `poolifier:<PoolType>-<WorkerType>-pool`.
233 * Events that can currently be listened to:
235 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
236 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
237 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
238 * - `'destroy'`: Emitted when the pool is destroyed.
239 * - `'error'`: Emitted when an uncaught error occurs.
240 * - `'taskError'`: Emitted when an error occurs while executing a task.
241 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
243 readonly emitter
?: PoolEmitter
245 * Executes the specified function in the worker constructor with the task data input parameter.
247 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
248 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
249 * @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.
250 * @returns Promise that will be fulfilled when the task is completed.
255 transferList
?: TransferListItem
[]
256 ) => Promise
<Response
>
258 * Starts the minimum number of workers in this pool.
260 readonly start
: () => void
262 * Terminates all workers in this pool.
264 readonly destroy
: () => Promise
<void>
266 * Whether the specified task function exists in this pool.
268 * @param name - The name of the task function.
269 * @returns `true` if the task function exists, `false` otherwise.
271 readonly hasTaskFunction
: (name
: string) => boolean
273 * Adds a task function to this pool.
274 * If a task function with the same name already exists, it will be overwritten.
276 * @param name - The name of the task function.
277 * @param fn - The task function.
278 * @returns `true` if the task function was added, `false` otherwise.
279 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
280 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
282 readonly addTaskFunction
: (
284 fn
: TaskFunction
<Data
, Response
>
285 ) => Promise
<boolean>
287 * Removes a task function from this pool.
289 * @param name - The name of the task function.
290 * @returns `true` if the task function was removed, `false` otherwise.
292 readonly removeTaskFunction
: (name
: string) => Promise
<boolean>
294 * Lists the names of task function available in this pool.
296 * @returns The names of task function available in this pool.
298 readonly listTaskFunctionNames
: () => string[]
300 * Sets the default task function in this pool.
302 * @param name - The name of the task function.
303 * @returns `true` if the default task function was set, `false` otherwise.
305 readonly setDefaultTaskFunction
: (name
: string) => Promise
<boolean>
307 * Sets the worker choice strategy in this pool.
309 * @param workerChoiceStrategy - The worker choice strategy.
310 * @param workerChoiceStrategyOptions - The worker choice strategy options.
312 readonly setWorkerChoiceStrategy
: (
313 workerChoiceStrategy
: WorkerChoiceStrategy
,
314 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
317 * Sets the worker choice strategy options in this pool.
319 * @param workerChoiceStrategyOptions - The worker choice strategy options.
321 readonly setWorkerChoiceStrategyOptions
: (
322 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
325 * Enables/disables the worker node tasks queue in this pool.
327 * @param enable - Whether to enable or disable the worker node tasks queue.
328 * @param tasksQueueOptions - The worker node tasks queue options.
330 readonly enableTasksQueue
: (
332 tasksQueueOptions
?: TasksQueueOptions
335 * Sets the worker node tasks queue options in this pool.
337 * @param tasksQueueOptions - The worker node tasks queue options.
339 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void