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 ready
: boolean
67 readonly strategy
: WorkerChoiceStrategy
68 readonly minSize
: number
69 readonly maxSize
: number
70 /** Pool utilization. */
71 readonly utilization
?: number
72 /** Pool total worker nodes. */
73 readonly workerNodes
: number
74 /** Pool idle worker nodes. */
75 readonly idleWorkerNodes
: number
76 /** Pool busy worker nodes. */
77 readonly busyWorkerNodes
: number
78 readonly executedTasks
: number
79 readonly executingTasks
: number
80 readonly queuedTasks
?: number
81 readonly maxQueuedTasks
?: number
82 readonly backPressure
?: boolean
83 readonly stolenTasks
?: number
84 readonly failedTasks
: number
86 readonly minimum
: number
87 readonly maximum
: number
88 readonly average
?: number
89 readonly median
?: number
92 readonly minimum
: number
93 readonly maximum
: number
94 readonly average
?: number
95 readonly median
?: number
100 * Worker node tasks queue options.
102 export interface TasksQueueOptions
{
104 * Maximum tasks queue size per worker node flagging it as back pressured.
106 * @defaultValue (pool maximum size)^2
108 readonly size
?: number
110 * @deprecated Use `size` instead.
112 readonly queueMaxSize
?: number
114 * Maximum number of tasks that can be executed concurrently on a worker node.
118 readonly concurrency
?: number
122 * Options for a poolifier pool.
124 * @typeParam Worker - Type of worker.
126 export interface PoolOptions
<Worker
extends IWorker
> {
128 * A function that will listen for online event on each worker.
130 onlineHandler
?: OnlineHandler
<Worker
>
132 * A function that will listen for message event on each worker.
134 messageHandler
?: MessageHandler
<Worker
>
136 * A function that will listen for error event on each worker.
138 errorHandler
?: ErrorHandler
<Worker
>
140 * A function that will listen for exit event on each worker.
142 exitHandler
?: ExitHandler
<Worker
>
144 * The worker choice strategy to use in this pool.
146 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
148 workerChoiceStrategy
?: WorkerChoiceStrategy
150 * The worker choice strategy options.
152 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
154 * Restart worker on error.
156 restartWorkerOnError
?: boolean
158 * Pool events emission.
162 enableEvents
?: boolean
164 * Pool worker node tasks queue.
166 * @defaultValue false
168 enableTasksQueue
?: boolean
170 * Pool worker node tasks queue options.
172 tasksQueueOptions
?: TasksQueueOptions
176 * Contract definition for a poolifier pool.
178 * @typeParam Worker - Type of worker which manages this pool.
179 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
180 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
182 export interface IPool
<
183 Worker
extends IWorker
,
190 readonly info
: PoolInfo
196 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
198 * Whether the worker node has back pressure (i.e. its tasks queue is full).
200 * @param workerNodeKey - The worker node key.
201 * @returns `true` if the worker node has back pressure, `false` otherwise.
204 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
206 * Emitter on which events can be listened to.
208 * Events that can currently be listened to:
210 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
211 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
212 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
213 * - `'destroy'`: Emitted when the pool is destroyed.
214 * - `'error'`: Emitted when an uncaught error occurs.
215 * - `'taskError'`: Emitted when an error occurs while executing a task.
216 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
218 readonly emitter
?: PoolEmitter
220 * Executes the specified function in the worker constructor with the task data input parameter.
222 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
223 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
224 * @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.
225 * @returns Promise that will be fulfilled when the task is completed.
230 transferList
?: TransferListItem
[]
231 ) => Promise
<Response
>
233 * Terminates all workers in this pool.
235 readonly destroy
: () => Promise
<void>
237 * Lists the names of task function available in this pool.
239 * @returns The names of task function available in this pool.
241 readonly listTaskFunctions
: () => string[]
243 * Sets the worker choice strategy in this pool.
245 * @param workerChoiceStrategy - The worker choice strategy.
246 * @param workerChoiceStrategyOptions - The worker choice strategy options.
248 readonly setWorkerChoiceStrategy
: (
249 workerChoiceStrategy
: WorkerChoiceStrategy
,
250 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
253 * Sets the worker choice strategy options in this pool.
255 * @param workerChoiceStrategyOptions - The worker choice strategy options.
257 readonly setWorkerChoiceStrategyOptions
: (
258 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
261 * Enables/disables the worker node tasks queue in this pool.
263 * @param enable - Whether to enable or disable the worker node tasks queue.
264 * @param tasksQueueOptions - The worker node tasks queue options.
266 readonly enableTasksQueue
: (
268 tasksQueueOptions
?: TasksQueueOptions
271 * Sets the worker node tasks queue options in this pool.
273 * @param tasksQueueOptions - The worker node tasks queue options.
275 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void