1 import { EventEmitter
} from
'node:events'
2 import { type TransferListItem
} from
'node:worker_threads'
14 WorkerChoiceStrategyOptions
15 } from
'./selection-strategies/selection-strategies-types'
18 * Enumeration of pool types.
20 export const PoolTypes
= Object.freeze({
34 export type PoolType
= keyof
typeof PoolTypes
37 * Pool events emitter.
39 export class PoolEmitter
extends EventEmitter
{}
42 * Enumeration of pool events.
44 export const PoolEvents
= Object.freeze({
50 taskError
: 'taskError',
51 backPressure
: 'backPressure'
57 export type PoolEvent
= keyof
typeof PoolEvents
62 export interface PoolInfo
{
63 readonly version
: string
64 readonly type: PoolType
65 readonly worker
: WorkerType
66 readonly started
: boolean
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 * Maximum number of tasks that can be executed concurrently on a worker node.
115 readonly concurrency
?: number
117 * Whether to enable task stealing.
121 readonly taskStealing
?: boolean
123 * Whether to enable tasks stealing on back pressure.
127 readonly tasksStealingOnBackPressure
?: boolean
131 * Options for a poolifier pool.
133 * @typeParam Worker - Type of worker.
135 export interface PoolOptions
<Worker
extends IWorker
> {
137 * A function that will listen for online event on each worker.
139 onlineHandler
?: OnlineHandler
<Worker
>
141 * A function that will listen for message event on each worker.
143 messageHandler
?: MessageHandler
<Worker
>
145 * A function that will listen for error event on each worker.
147 errorHandler
?: ErrorHandler
<Worker
>
149 * A function that will listen for exit event on each worker.
151 exitHandler
?: ExitHandler
<Worker
>
153 * Whether to start the minimum number of workers at pool initialization.
157 startWorkers
?: boolean
159 * The worker choice strategy to use in this pool.
161 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
163 workerChoiceStrategy
?: WorkerChoiceStrategy
165 * The worker choice strategy options.
167 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
169 * Restart worker on error.
171 restartWorkerOnError
?: boolean
173 * Pool events emission.
177 enableEvents
?: boolean
179 * Pool worker node tasks queue.
181 * @defaultValue false
183 enableTasksQueue
?: boolean
185 * Pool worker node tasks queue options.
187 tasksQueueOptions
?: TasksQueueOptions
191 * Contract definition for a poolifier pool.
193 * @typeParam Worker - Type of worker which manages this pool.
194 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
195 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
197 export interface IPool
<
198 Worker
extends IWorker
,
205 readonly info
: PoolInfo
211 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
213 * Whether the worker node has back pressure (i.e. its tasks queue is full).
215 * @param workerNodeKey - The worker node key.
216 * @returns `true` if the worker node has back pressure, `false` otherwise.
219 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
221 * Emitter on which events can be listened to.
223 * Events that can currently be listened to:
225 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
226 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
227 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
228 * - `'destroy'`: Emitted when the pool is destroyed.
229 * - `'error'`: Emitted when an uncaught error occurs.
230 * - `'taskError'`: Emitted when an error occurs while executing a task.
231 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
233 readonly emitter
?: PoolEmitter
235 * Executes the specified function in the worker constructor with the task data input parameter.
237 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
238 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
239 * @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.
240 * @returns Promise that will be fulfilled when the task is completed.
245 transferList
?: TransferListItem
[]
246 ) => Promise
<Response
>
248 * Starts the minimum number of workers in this pool.
250 readonly start
: () => void
252 * Terminates all workers in this pool.
254 readonly destroy
: () => Promise
<void>
256 * Lists the names of task function available in this pool.
258 * @returns The names of task function available in this pool.
260 readonly listTaskFunctions
: () => string[]
262 * Sets the worker choice strategy in this pool.
264 * @param workerChoiceStrategy - The worker choice strategy.
265 * @param workerChoiceStrategyOptions - The worker choice strategy options.
267 readonly setWorkerChoiceStrategy
: (
268 workerChoiceStrategy
: WorkerChoiceStrategy
,
269 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
272 * Sets the worker choice strategy options in this pool.
274 * @param workerChoiceStrategyOptions - The worker choice strategy options.
276 readonly setWorkerChoiceStrategyOptions
: (
277 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
280 * Enables/disables the worker node tasks queue in this pool.
282 * @param enable - Whether to enable or disable the worker node tasks queue.
283 * @param tasksQueueOptions - The worker node tasks queue options.
285 readonly enableTasksQueue
: (
287 tasksQueueOptions
?: TasksQueueOptions
290 * Sets the worker node tasks queue options in this pool.
292 * @param tasksQueueOptions - The worker node tasks queue options.
294 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void