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 failedTasks
: number
85 readonly minimum
: number
86 readonly maximum
: number
87 readonly average
: number
88 readonly median
?: number
91 readonly minimum
: number
92 readonly maximum
: number
93 readonly average
: number
94 readonly median
?: number
99 * Worker node tasks queue options.
101 export interface TasksQueueOptions
{
103 * Maximum tasks queue size per worker node flagging it as back pressured.
105 * @defaultValue (pool maximum size)^2
107 readonly queueMaxSize
?: number
109 * Maximum number of tasks that can be executed concurrently on a worker node.
113 readonly concurrency
?: number
117 * Options for a poolifier pool.
119 * @typeParam Worker - Type of worker.
121 export interface PoolOptions
<Worker
extends IWorker
> {
123 * A function that will listen for online event on each worker.
125 onlineHandler
?: OnlineHandler
<Worker
>
127 * A function that will listen for message event on each worker.
129 messageHandler
?: MessageHandler
<Worker
>
131 * A function that will listen for error event on each worker.
133 errorHandler
?: ErrorHandler
<Worker
>
135 * A function that will listen for exit event on each worker.
137 exitHandler
?: ExitHandler
<Worker
>
139 * The worker choice strategy to use in this pool.
141 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
143 workerChoiceStrategy
?: WorkerChoiceStrategy
145 * The worker choice strategy options.
147 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
149 * Restart worker on error.
151 restartWorkerOnError
?: boolean
153 * Pool events emission.
157 enableEvents
?: boolean
159 * Pool worker node tasks queue.
161 * @defaultValue false
163 enableTasksQueue
?: boolean
165 * Pool worker node tasks queue options.
167 tasksQueueOptions
?: TasksQueueOptions
171 * Contract definition for a poolifier pool.
173 * @typeParam Worker - Type of worker which manages this pool.
174 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
175 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
177 export interface IPool
<
178 Worker
extends IWorker
,
185 readonly info
: PoolInfo
191 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
193 * Whether the worker node has back pressure (i.e. its tasks queue is full).
195 * @param workerNodeKey - The worker node key.
196 * @returns `true` if the worker node has back pressure, `false` otherwise.
199 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
201 * Emitter on which events can be listened to.
203 * Events that can currently be listened to:
205 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
206 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing at least one task.
207 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
208 * - '`destroy`': Emitted when the pool is destroyed.
209 * - `'error'`: Emitted when an uncaught error occurs.
210 * - `'taskError'`: Emitted when an error occurs while executing a task.
211 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
213 readonly emitter
?: PoolEmitter
215 * Executes the specified function in the worker constructor with the task data input parameter.
217 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
218 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
219 * @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.
220 * @returns Promise that will be fulfilled when the task is completed.
225 transferList
?: TransferListItem
[]
226 ) => Promise
<Response
>
228 * Terminates all workers in this pool.
230 readonly destroy
: () => Promise
<void>
232 * Lists the names of task function available in this pool.
234 * @returns The names of task function available in this pool.
236 readonly listTaskFunctions
: () => string[]
238 * Sets the worker choice strategy in this pool.
240 * @param workerChoiceStrategy - The worker choice strategy.
241 * @param workerChoiceStrategyOptions - The worker choice strategy options.
243 readonly setWorkerChoiceStrategy
: (
244 workerChoiceStrategy
: WorkerChoiceStrategy
,
245 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
248 * Sets the worker choice strategy options in this pool.
250 * @param workerChoiceStrategyOptions - The worker choice strategy options.
252 readonly setWorkerChoiceStrategyOptions
: (
253 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
256 * Enables/disables the worker node tasks queue in this pool.
258 * @param enable - Whether to enable or disable the worker node tasks queue.
259 * @param tasksQueueOptions - The worker node tasks queue options.
261 readonly enableTasksQueue
: (
263 tasksQueueOptions
?: TasksQueueOptions
266 * Sets the worker node tasks queue options in this pool.
268 * @param tasksQueueOptions - The worker node tasks queue options.
270 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void