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 tasks queue options.
101 export interface TasksQueueOptions
{
103 * Maximum number of tasks that can be executed concurrently on a worker.
107 readonly concurrency
?: number
111 * Options for a poolifier pool.
113 * @typeParam Worker - Type of worker.
115 export interface PoolOptions
<Worker
extends IWorker
> {
117 * A function that will listen for online event on each worker.
119 onlineHandler
?: OnlineHandler
<Worker
>
121 * A function that will listen for message event on each worker.
123 messageHandler
?: MessageHandler
<Worker
>
125 * A function that will listen for error event on each worker.
127 errorHandler
?: ErrorHandler
<Worker
>
129 * A function that will listen for exit event on each worker.
131 exitHandler
?: ExitHandler
<Worker
>
133 * The worker choice strategy to use in this pool.
135 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
137 workerChoiceStrategy
?: WorkerChoiceStrategy
139 * The worker choice strategy options.
141 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
143 * Restart worker on error.
145 restartWorkerOnError
?: boolean
147 * Pool events emission.
151 enableEvents
?: boolean
153 * Pool worker tasks queue.
155 * @defaultValue false
157 enableTasksQueue
?: boolean
159 * Pool worker tasks queue options.
161 tasksQueueOptions
?: TasksQueueOptions
165 * Contract definition for a poolifier pool.
167 * @typeParam Worker - Type of worker which manages this pool.
168 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
169 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
171 export interface IPool
<
172 Worker
extends IWorker
,
179 readonly info
: PoolInfo
185 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
187 * Whether the worker node has back pressure (i.e. its tasks queue is full).
189 * @param workerNodeKey - The worker node key.
190 * @returns `true` if the worker node has back pressure, `false` otherwise.
193 readonly hasWorkerNodeBackPressure
: (workerNodeKey
: number) => boolean
195 * Emitter on which events can be listened to.
197 * Events that can currently be listened to:
199 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
200 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing at least one task.
201 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
202 * - '`destroy`': Emitted when the pool is destroyed.
203 * - `'error'`: Emitted when an uncaught error occurs.
204 * - `'taskError'`: Emitted when an error occurs while executing a task.
205 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= pool maximum size^2).
207 readonly emitter
?: PoolEmitter
209 * Executes the specified function in the worker constructor with the task data input parameter.
211 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
212 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
213 * @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.
214 * @returns Promise that will be fulfilled when the task is completed.
219 transferList
?: TransferListItem
[]
220 ) => Promise
<Response
>
222 * Terminates all workers in this pool.
224 readonly destroy
: () => Promise
<void>
226 * Lists the names of task function available in this pool.
228 * @returns The names of task function available in this pool.
230 readonly listTaskFunctions
: () => string[]
232 * Sets the worker choice strategy in this pool.
234 * @param workerChoiceStrategy - The worker choice strategy.
235 * @param workerChoiceStrategyOptions - The worker choice strategy options.
237 readonly setWorkerChoiceStrategy
: (
238 workerChoiceStrategy
: WorkerChoiceStrategy
,
239 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
242 * Sets the worker choice strategy options in this pool.
244 * @param workerChoiceStrategyOptions - The worker choice strategy options.
246 readonly setWorkerChoiceStrategyOptions
: (
247 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
250 * Enables/disables the worker tasks queue in this pool.
252 * @param enable - Whether to enable or disable the worker tasks queue.
253 * @param tasksQueueOptions - The worker tasks queue options.
255 readonly enableTasksQueue
: (
257 tasksQueueOptions
?: TasksQueueOptions
260 * Sets the worker tasks queue options in this pool.
262 * @param tasksQueueOptions - The worker tasks queue options.
264 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void