feat: change pool event emitter to event emitter async resource
[poolifier.git] / src / pools / pool.ts
CommitLineData
b5604034 1import { EventEmitterAsyncResource } from 'node:events'
ef083f7b 2import type { TransferListItem } from 'node:worker_threads'
6703b9f4 3import type { TaskFunction } from '../worker/task-functions'
bdaf31cd
JB
4import type {
5 ErrorHandler,
6 ExitHandler,
50e66724 7 IWorker,
4b628b48 8 IWorkerNode,
bdaf31cd 9 MessageHandler,
c4855468 10 OnlineHandler,
4b628b48 11 WorkerType
f06e48d8 12} from './worker'
da309861
JB
13import type {
14 WorkerChoiceStrategy,
15 WorkerChoiceStrategyOptions
16} from './selection-strategies/selection-strategies-types'
bdaf31cd 17
c4855468 18/**
6b27d407 19 * Enumeration of pool types.
c4855468 20 */
6b27d407 21export const PoolTypes = Object.freeze({
c4855468
JB
22 /**
23 * Fixed pool type.
24 */
6b27d407 25 fixed: 'fixed',
c4855468
JB
26 /**
27 * Dynamic pool type.
28 */
6b27d407
JB
29 dynamic: 'dynamic'
30} as const)
31
32/**
33 * Pool type.
34 */
35export type PoolType = keyof typeof PoolTypes
c4855468 36
b4904890 37/**
b5604034 38 * Pool event emitter integrated with async resource.
b4904890 39 */
b5604034 40export class PoolEmitter extends EventEmitterAsyncResource {}
b4904890 41
aee46736
JB
42/**
43 * Enumeration of pool events.
44 */
45export const PoolEvents = Object.freeze({
2431bdb4 46 ready: 'ready',
1f68cede 47 busy: 'busy',
ef3891a3
JB
48 full: 'full',
49 destroy: 'destroy',
91ee39ed 50 error: 'error',
671d5154
JB
51 taskError: 'taskError',
52 backPressure: 'backPressure'
aee46736
JB
53} as const)
54
55/**
56 * Pool event.
57 */
58export type PoolEvent = keyof typeof PoolEvents
59
6b27d407
JB
60/**
61 * Pool information.
62 */
63export interface PoolInfo {
4b628b48
JB
64 readonly version: string
65 readonly type: PoolType
66 readonly worker: WorkerType
47352846 67 readonly started: boolean
2431bdb4
JB
68 readonly ready: boolean
69 readonly strategy: WorkerChoiceStrategy
4b628b48
JB
70 readonly minSize: number
71 readonly maxSize: number
aa9eede8 72 /** Pool utilization. */
4b628b48 73 readonly utilization?: number
01a59f3c 74 /** Pool total worker nodes. */
4b628b48 75 readonly workerNodes: number
01a59f3c 76 /** Pool idle worker nodes. */
4b628b48 77 readonly idleWorkerNodes: number
01a59f3c 78 /** Pool busy worker nodes. */
4b628b48
JB
79 readonly busyWorkerNodes: number
80 readonly executedTasks: number
81 readonly executingTasks: number
daf86646
JB
82 readonly queuedTasks?: number
83 readonly maxQueuedTasks?: number
a1763c54 84 readonly backPressure?: boolean
68cbdc84 85 readonly stolenTasks?: number
4b628b48
JB
86 readonly failedTasks: number
87 readonly runTime?: {
88 readonly minimum: number
89 readonly maximum: number
3baa0837 90 readonly average?: number
4b628b48 91 readonly median?: number
1dcf8b7b 92 }
4b628b48
JB
93 readonly waitTime?: {
94 readonly minimum: number
95 readonly maximum: number
3baa0837 96 readonly average?: number
4b628b48 97 readonly median?: number
1dcf8b7b 98 }
6b27d407
JB
99}
100
7171d33f 101/**
20c6f652 102 * Worker node tasks queue options.
7171d33f
JB
103 */
104export interface TasksQueueOptions {
105 /**
20c6f652
JB
106 * Maximum tasks queue size per worker node flagging it as back pressured.
107 *
108 * @defaultValue (pool maximum size)^2
109 */
ff3f866a 110 readonly size?: number
20c6f652
JB
111 /**
112 * Maximum number of tasks that can be executed concurrently on a worker node.
7171d33f
JB
113 *
114 * @defaultValue 1
115 */
eb7bf744 116 readonly concurrency?: number
47352846 117 /**
dbd73092 118 * Whether to enable task stealing.
47352846
JB
119 *
120 * @defaultValue true
121 */
dbd73092 122 readonly taskStealing?: boolean
47352846
JB
123 /**
124 * Whether to enable tasks stealing on back pressure.
125 *
126 * @defaultValue true
127 */
128 readonly tasksStealingOnBackPressure?: boolean
7171d33f
JB
129}
130
bdaf31cd
JB
131/**
132 * Options for a poolifier pool.
c319c66b 133 *
d480d708 134 * @typeParam Worker - Type of worker.
bdaf31cd 135 */
50e66724 136export interface PoolOptions<Worker extends IWorker> {
fd04474e
JB
137 /**
138 * A function that will listen for online event on each worker.
68f1f531
JB
139 *
140 * @defaultValue `() => {}`
fd04474e
JB
141 */
142 onlineHandler?: OnlineHandler<Worker>
bdaf31cd
JB
143 /**
144 * A function that will listen for message event on each worker.
68f1f531
JB
145 *
146 * @defaultValue `() => {}`
bdaf31cd
JB
147 */
148 messageHandler?: MessageHandler<Worker>
149 /**
150 * A function that will listen for error event on each worker.
68f1f531
JB
151 *
152 * @defaultValue `() => {}`
bdaf31cd
JB
153 */
154 errorHandler?: ErrorHandler<Worker>
bdaf31cd
JB
155 /**
156 * A function that will listen for exit event on each worker.
68f1f531
JB
157 *
158 * @defaultValue `() => {}`
bdaf31cd
JB
159 */
160 exitHandler?: ExitHandler<Worker>
47352846
JB
161 /**
162 * Whether to start the minimum number of workers at pool initialization.
163 *
8ff61e33 164 * @defaultValue true
47352846
JB
165 */
166 startWorkers?: boolean
bdaf31cd 167 /**
46e857ca 168 * The worker choice strategy to use in this pool.
d29bce7c 169 *
95ec6006 170 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd
JB
171 */
172 workerChoiceStrategy?: WorkerChoiceStrategy
da309861
JB
173 /**
174 * The worker choice strategy options.
175 */
176 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
1f68cede
JB
177 /**
178 * Restart worker on error.
179 */
180 restartWorkerOnError?: boolean
bdaf31cd 181 /**
b5604034 182 * Pool events integrated with async resource emission.
bdaf31cd 183 *
38e795c1 184 * @defaultValue true
bdaf31cd
JB
185 */
186 enableEvents?: boolean
ff733df7 187 /**
20c6f652 188 * Pool worker node tasks queue.
ff733df7 189 *
ff733df7
JB
190 * @defaultValue false
191 */
192 enableTasksQueue?: boolean
7171d33f 193 /**
20c6f652 194 * Pool worker node tasks queue options.
7171d33f
JB
195 */
196 tasksQueueOptions?: TasksQueueOptions
bdaf31cd 197}
a35560ba 198
729c563d
S
199/**
200 * Contract definition for a poolifier pool.
201 *
c4855468 202 * @typeParam Worker - Type of worker which manages this pool.
e102732c
JB
203 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
204 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
729c563d 205 */
c4855468
JB
206export interface IPool<
207 Worker extends IWorker,
208 Data = unknown,
209 Response = unknown
210> {
08f3f44c 211 /**
6b27d407 212 * Pool information.
08f3f44c 213 */
6b27d407 214 readonly info: PoolInfo
c4855468
JB
215 /**
216 * Pool worker nodes.
9768f49f
JB
217 *
218 * @internal
c4855468 219 */
4b628b48 220 readonly workerNodes: Array<IWorkerNode<Worker, Data>>
e2b31e32
JB
221 /**
222 * Whether the worker node has back pressure (i.e. its tasks queue is full).
223 *
224 * @param workerNodeKey - The worker node key.
225 * @returns `true` if the worker node has back pressure, `false` otherwise.
9768f49f 226 * @internal
e2b31e32
JB
227 */
228 readonly hasWorkerNodeBackPressure: (workerNodeKey: number) => boolean
b4904890 229 /**
b5604034
JB
230 * Event emitter integrated with `AsyncResource` on which events can be listened to.
231 * The async tracking tooling identifier is `poolifier:<PoolType>-<WorkerType>-pool`.
b4904890
JB
232 *
233 * Events that can currently be listened to:
234 *
d5024c00 235 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
a9780ad2 236 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
ef3891a3 237 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
033f1776 238 * - `'destroy'`: Emitted when the pool is destroyed.
91ee39ed
JB
239 * - `'error'`: Emitted when an uncaught error occurs.
240 * - `'taskError'`: Emitted when an error occurs while executing a task.
d92f3ddf 241 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
b4904890
JB
242 */
243 readonly emitter?: PoolEmitter
729c563d 244 /**
61aa11a6 245 * Executes the specified function in the worker constructor with the task data input parameter.
729c563d 246 *
7d91a8cd
JB
247 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
248 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
249 * @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.
ef41a6e6 250 * @returns Promise that will be fulfilled when the task is completed.
729c563d 251 */
7d91a8cd
JB
252 readonly execute: (
253 data?: Data,
254 name?: string,
255 transferList?: TransferListItem[]
256 ) => Promise<Response>
47352846
JB
257 /**
258 * Starts the minimum number of workers in this pool.
259 */
260 readonly start: () => void
280c2a77 261 /**
aa9eede8 262 * Terminates all workers in this pool.
280c2a77 263 */
4b628b48 264 readonly destroy: () => Promise<void>
6703b9f4
JB
265 /**
266 * Whether the specified task function exists in this pool.
267 *
268 * @param name - The name of the task function.
269 * @returns `true` if the task function exists, `false` otherwise.
270 */
271 readonly hasTaskFunction: (name: string) => boolean
272 /**
273 * Adds a task function to this pool.
274 * If a task function with the same name already exists, it will be overwritten.
275 *
276 * @param name - The name of the task function.
3feeab69 277 * @param fn - The task function.
6703b9f4 278 * @returns `true` if the task function was added, `false` otherwise.
cf87987c
JB
279 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
280 * @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
6703b9f4
JB
281 */
282 readonly addTaskFunction: (
283 name: string,
3feeab69 284 fn: TaskFunction<Data, Response>
e81c38f2 285 ) => Promise<boolean>
6703b9f4
JB
286 /**
287 * Removes a task function from this pool.
288 *
289 * @param name - The name of the task function.
290 * @returns `true` if the task function was removed, `false` otherwise.
291 */
e81c38f2 292 readonly removeTaskFunction: (name: string) => Promise<boolean>
90d7d101
JB
293 /**
294 * Lists the names of task function available in this pool.
295 *
296 * @returns The names of task function available in this pool.
297 */
6703b9f4
JB
298 readonly listTaskFunctionNames: () => string[]
299 /**
300 * Sets the default task function in this pool.
301 *
302 * @param name - The name of the task function.
303 * @returns `true` if the default task function was set, `false` otherwise.
304 */
e81c38f2 305 readonly setDefaultTaskFunction: (name: string) => Promise<boolean>
a35560ba 306 /**
bdede008 307 * Sets the worker choice strategy in this pool.
a35560ba 308 *
38e795c1 309 * @param workerChoiceStrategy - The worker choice strategy.
59219cbb 310 * @param workerChoiceStrategyOptions - The worker choice strategy options.
a35560ba 311 */
4b628b48 312 readonly setWorkerChoiceStrategy: (
59219cbb
JB
313 workerChoiceStrategy: WorkerChoiceStrategy,
314 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
315 ) => void
a20f0ba5
JB
316 /**
317 * Sets the worker choice strategy options in this pool.
318 *
319 * @param workerChoiceStrategyOptions - The worker choice strategy options.
320 */
4b628b48 321 readonly setWorkerChoiceStrategyOptions: (
a20f0ba5
JB
322 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
323 ) => void
324 /**
20c6f652 325 * Enables/disables the worker node tasks queue in this pool.
a20f0ba5 326 *
20c6f652
JB
327 * @param enable - Whether to enable or disable the worker node tasks queue.
328 * @param tasksQueueOptions - The worker node tasks queue options.
a20f0ba5 329 */
4b628b48 330 readonly enableTasksQueue: (
8f52842f
JB
331 enable: boolean,
332 tasksQueueOptions?: TasksQueueOptions
333 ) => void
a20f0ba5 334 /**
20c6f652 335 * Sets the worker node tasks queue options in this pool.
a20f0ba5 336 *
20c6f652 337 * @param tasksQueueOptions - The worker node tasks queue options.
a20f0ba5 338 */
4b628b48 339 readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void
c97c7edb 340}