Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-worker_thr...
[poolifier.git] / src / pools / pool.ts
1 import { EventEmitter } from 'node:events'
2 import { type TransferListItem } from 'node:worker_threads'
3 import type {
4 ErrorHandler,
5 ExitHandler,
6 IWorker,
7 IWorkerNode,
8 MessageHandler,
9 OnlineHandler,
10 WorkerType
11 } from './worker'
12 import type {
13 WorkerChoiceStrategy,
14 WorkerChoiceStrategyOptions
15 } from './selection-strategies/selection-strategies-types'
16
17 /**
18 * Enumeration of pool types.
19 */
20 export const PoolTypes = Object.freeze({
21 /**
22 * Fixed pool type.
23 */
24 fixed: 'fixed',
25 /**
26 * Dynamic pool type.
27 */
28 dynamic: 'dynamic'
29 } as const)
30
31 /**
32 * Pool type.
33 */
34 export type PoolType = keyof typeof PoolTypes
35
36 /**
37 * Pool events emitter.
38 */
39 export class PoolEmitter extends EventEmitter {}
40
41 /**
42 * Enumeration of pool events.
43 */
44 export const PoolEvents = Object.freeze({
45 ready: 'ready',
46 busy: 'busy',
47 full: 'full',
48 destroy: 'destroy',
49 error: 'error',
50 taskError: 'taskError',
51 backPressure: 'backPressure'
52 } as const)
53
54 /**
55 * Pool event.
56 */
57 export type PoolEvent = keyof typeof PoolEvents
58
59 /**
60 * Pool information.
61 */
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
86 readonly runTime?: {
87 readonly minimum: number
88 readonly maximum: number
89 readonly average?: number
90 readonly median?: number
91 }
92 readonly waitTime?: {
93 readonly minimum: number
94 readonly maximum: number
95 readonly average?: number
96 readonly median?: number
97 }
98 }
99
100 /**
101 * Worker node tasks queue options.
102 */
103 export interface TasksQueueOptions {
104 /**
105 * Maximum tasks queue size per worker node flagging it as back pressured.
106 *
107 * @defaultValue (pool maximum size)^2
108 */
109 readonly size?: number
110 /**
111 * Maximum number of tasks that can be executed concurrently on a worker node.
112 *
113 * @defaultValue 1
114 */
115 readonly concurrency?: number
116 /**
117 * Whether to enable task stealing.
118 *
119 * @defaultValue true
120 */
121 readonly taskStealing?: boolean
122 /**
123 * Whether to enable tasks stealing on back pressure.
124 *
125 * @defaultValue true
126 */
127 readonly tasksStealingOnBackPressure?: boolean
128 }
129
130 /**
131 * Options for a poolifier pool.
132 *
133 * @typeParam Worker - Type of worker.
134 */
135 export interface PoolOptions<Worker extends IWorker> {
136 /**
137 * A function that will listen for online event on each worker.
138 *
139 * @defaultValue `() => {}`
140 */
141 onlineHandler?: OnlineHandler<Worker>
142 /**
143 * A function that will listen for message event on each worker.
144 *
145 * @defaultValue `() => {}`
146 */
147 messageHandler?: MessageHandler<Worker>
148 /**
149 * A function that will listen for error event on each worker.
150 *
151 * @defaultValue `() => {}`
152 */
153 errorHandler?: ErrorHandler<Worker>
154 /**
155 * A function that will listen for exit event on each worker.
156 *
157 * @defaultValue `() => {}`
158 */
159 exitHandler?: ExitHandler<Worker>
160 /**
161 * Whether to start the minimum number of workers at pool initialization.
162 *
163 * @defaultValue true
164 */
165 startWorkers?: boolean
166 /**
167 * The worker choice strategy to use in this pool.
168 *
169 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
170 */
171 workerChoiceStrategy?: WorkerChoiceStrategy
172 /**
173 * The worker choice strategy options.
174 */
175 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
176 /**
177 * Restart worker on error.
178 */
179 restartWorkerOnError?: boolean
180 /**
181 * Pool events emission.
182 *
183 * @defaultValue true
184 */
185 enableEvents?: boolean
186 /**
187 * Pool worker node tasks queue.
188 *
189 * @defaultValue false
190 */
191 enableTasksQueue?: boolean
192 /**
193 * Pool worker node tasks queue options.
194 */
195 tasksQueueOptions?: TasksQueueOptions
196 }
197
198 /**
199 * Contract definition for a poolifier pool.
200 *
201 * @typeParam Worker - Type of worker which manages this pool.
202 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
203 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
204 */
205 export interface IPool<
206 Worker extends IWorker,
207 Data = unknown,
208 Response = unknown
209 > {
210 /**
211 * Pool information.
212 */
213 readonly info: PoolInfo
214 /**
215 * Pool worker nodes.
216 *
217 * @internal
218 */
219 readonly workerNodes: Array<IWorkerNode<Worker, Data>>
220 /**
221 * Whether the worker node has back pressure (i.e. its tasks queue is full).
222 *
223 * @param workerNodeKey - The worker node key.
224 * @returns `true` if the worker node has back pressure, `false` otherwise.
225 * @internal
226 */
227 readonly hasWorkerNodeBackPressure: (workerNodeKey: number) => boolean
228 /**
229 * Emitter on which events can be listened to.
230 *
231 * Events that can currently be listened to:
232 *
233 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
234 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
235 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
236 * - `'destroy'`: Emitted when the pool is destroyed.
237 * - `'error'`: Emitted when an uncaught error occurs.
238 * - `'taskError'`: Emitted when an error occurs while executing a task.
239 * - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
240 */
241 readonly emitter?: PoolEmitter
242 /**
243 * Executes the specified function in the worker constructor with the task data input parameter.
244 *
245 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
246 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
247 * @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.
248 * @returns Promise that will be fulfilled when the task is completed.
249 */
250 readonly execute: (
251 data?: Data,
252 name?: string,
253 transferList?: TransferListItem[]
254 ) => Promise<Response>
255 /**
256 * Starts the minimum number of workers in this pool.
257 */
258 readonly start: () => void
259 /**
260 * Terminates all workers in this pool.
261 */
262 readonly destroy: () => Promise<void>
263 /**
264 * Lists the names of task function available in this pool.
265 *
266 * @returns The names of task function available in this pool.
267 */
268 readonly listTaskFunctions: () => string[]
269 /**
270 * Sets the worker choice strategy in this pool.
271 *
272 * @param workerChoiceStrategy - The worker choice strategy.
273 * @param workerChoiceStrategyOptions - The worker choice strategy options.
274 */
275 readonly setWorkerChoiceStrategy: (
276 workerChoiceStrategy: WorkerChoiceStrategy,
277 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
278 ) => void
279 /**
280 * Sets the worker choice strategy options in this pool.
281 *
282 * @param workerChoiceStrategyOptions - The worker choice strategy options.
283 */
284 readonly setWorkerChoiceStrategyOptions: (
285 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
286 ) => void
287 /**
288 * Enables/disables the worker node tasks queue in this pool.
289 *
290 * @param enable - Whether to enable or disable the worker node tasks queue.
291 * @param tasksQueueOptions - The worker node tasks queue options.
292 */
293 readonly enableTasksQueue: (
294 enable: boolean,
295 tasksQueueOptions?: TasksQueueOptions
296 ) => void
297 /**
298 * Sets the worker node tasks queue options in this pool.
299 *
300 * @param tasksQueueOptions - The worker node tasks queue options.
301 */
302 readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void
303 }