1 import { EventEmitter
} 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 events emitter.
40 export class PoolEmitter
extends EventEmitter
{}
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 ready
: boolean
68 readonly strategy
: WorkerChoiceStrategy
69 readonly minSize
: number
70 readonly maxSize
: number
71 /** Pool utilization. */
72 readonly utilization
?: number
73 /** Pool total worker nodes. */
74 readonly workerNodes
: number
75 /** Pool idle worker nodes. */
76 readonly idleWorkerNodes
: number
77 /** Pool busy worker nodes. */
78 readonly busyWorkerNodes
: number
79 readonly executedTasks
: number
80 readonly executingTasks
: number
81 readonly queuedTasks
?: number
82 readonly maxQueuedTasks
?: number
83 readonly backPressure
?: boolean
84 readonly stolenTasks
?: number
85 readonly failedTasks
: number
87 readonly minimum
: number
88 readonly maximum
: number
89 readonly average
?: number
90 readonly median
?: number
93 readonly minimum
: number
94 readonly maximum
: number
95 readonly average
?: number
96 readonly median
?: number
101 * Worker node tasks queue options.
103 export interface TasksQueueOptions
{
105 * Maximum tasks queue size per worker node flagging it as back pressured.
107 * @defaultValue (pool maximum size)^2
109 readonly size
?: number
111 * @deprecated Use `size` instead.
113 readonly queueMaxSize
?: number
115 * Maximum number of tasks that can be executed concurrently on a worker node.
119 readonly concurrency
?: number
123 * Options for a poolifier pool.
125 * @typeParam Worker - Type of worker.
127 export interface PoolOptions
<Worker
extends IWorker
> {
129 * A function that will listen for online event on each worker.
131 onlineHandler
?: OnlineHandler
<Worker
>
133 * A function that will listen for message event on each worker.
135 messageHandler
?: MessageHandler
<Worker
>
137 * A function that will listen for error event on each worker.
139 errorHandler
?: ErrorHandler
<Worker
>
141 * A function that will listen for exit event on each worker.
143 exitHandler
?: ExitHandler
<Worker
>
145 * The worker choice strategy to use in this pool.
147 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
149 workerChoiceStrategy
?: WorkerChoiceStrategy
151 * The worker choice strategy options.
153 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
155 * Restart worker on error.
157 restartWorkerOnError
?: boolean
159 * Pool events emission.
163 enableEvents
?: boolean
165 * Pool worker node tasks queue.
167 * @defaultValue false
169 enableTasksQueue
?: boolean
171 * Pool worker node tasks queue options.
173 tasksQueueOptions
?: TasksQueueOptions
177 * Contract definition for a poolifier pool.
179 * @typeParam Worker - Type of worker which manages this pool.
180 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
181 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
183 export interface IPool
<
184 Worker
extends IWorker
,
191 readonly info
: PoolInfo
197 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
199 * Whether the worker node has back pressure (i.e. its tasks queue is full).
201 * @param workerNodeKey - The worker node key.
202 * @returns `true` if the worker node has back pressure, `false` otherwise.
205 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
207 * Emitter on which events can be listened to.
209 * Events that can currently be listened to:
211 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
212 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
213 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
214 * - `'destroy'`: Emitted when the pool is destroyed.
215 * - `'error'`: Emitted when an uncaught error occurs.
216 * - `'taskError'`: Emitted when an error occurs while executing a task.
217 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
219 readonly emitter
?: PoolEmitter
221 * Executes the specified function in the worker constructor with the task data input parameter.
223 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
224 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
225 * @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.
226 * @returns Promise that will be fulfilled when the task is completed.
231 transferList
?: TransferListItem
[]
232 ) => Promise
<Response
>
234 * Terminates all workers in this pool.
236 readonly destroy
: () => Promise
<void>
238 * Whether the specified task function exists in this pool.
240 * @param name - The name of the task function.
241 * @returns `true` if the task function exists, `false` otherwise.
243 readonly hasTaskFunction
: (name
: string) => boolean
245 * Adds a task function to this pool.
246 * If a task function with the same name already exists, it will be overwritten.
248 * @param name - The name of the task function.
249 * @param taskFunction - The task function.
250 * @returns `true` if the task function was added, `false` otherwise.
252 readonly addTaskFunction
: (
254 taskFunction
: TaskFunction
<Data
, Response
>
255 ) => Promise
<boolean>
257 * Removes a task function from this pool.
259 * @param name - The name of the task function.
260 * @returns `true` if the task function was removed, `false` otherwise.
262 readonly removeTaskFunction
: (name
: string) => Promise
<boolean>
264 * Lists the names of task function available in this pool.
266 * @returns The names of task function available in this pool.
268 readonly listTaskFunctionNames
: () => string[]
270 * Sets the default task function in this pool.
272 * @param name - The name of the task function.
273 * @returns `true` if the default task function was set, `false` otherwise.
275 readonly setDefaultTaskFunction
: (name
: string) => Promise
<boolean>
277 * Sets the worker choice strategy in this pool.
279 * @param workerChoiceStrategy - The worker choice strategy.
280 * @param workerChoiceStrategyOptions - The worker choice strategy options.
282 readonly setWorkerChoiceStrategy
: (
283 workerChoiceStrategy
: WorkerChoiceStrategy
,
284 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
287 * Sets the worker choice strategy options in this pool.
289 * @param workerChoiceStrategyOptions - The worker choice strategy options.
291 readonly setWorkerChoiceStrategyOptions
: (
292 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
295 * Enables/disables the worker node tasks queue in this pool.
297 * @param enable - Whether to enable or disable the worker node tasks queue.
298 * @param tasksQueueOptions - The worker node tasks queue options.
300 readonly enableTasksQueue
: (
302 tasksQueueOptions
?: TasksQueueOptions
305 * Sets the worker node tasks queue options in this pool.
307 * @param tasksQueueOptions - The worker node tasks queue options.
309 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void