Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-worker_thr...
[poolifier.git] / src / pools / utils.ts
CommitLineData
e9ed6eee 1import cluster, { Worker as ClusterWorker } from 'node:cluster'
ded253e2 2import { existsSync } from 'node:fs'
ded253e2 3import { env } from 'node:process'
e9ed6eee
JB
4import {
5 SHARE_ENV,
6 Worker as ThreadWorker,
7 type WorkerOptions
8} from 'node:worker_threads'
ded253e2 9
d35e5717 10import type { MessageValue, Task } from '../utility-types.js'
ded253e2 11import { average, isPlainObject, max, median, min } from '../utils.js'
bcfb06ce 12import type { TasksQueueOptions } from './pool.js'
bde6b5d7 13import {
bfc75cca 14 type MeasurementStatisticsRequirements,
bde6b5d7 15 WorkerChoiceStrategies,
bcfb06ce 16 type WorkerChoiceStrategy
d35e5717 17} from './selection-strategies/selection-strategies-types.js'
bcfb06ce 18import type { WorkerChoiceStrategiesContext } from './selection-strategies/worker-choice-strategies-context.js'
c3719753
JB
19import {
20 type IWorker,
d41a44de 21 type IWorkerNode,
c3719753
JB
22 type MeasurementStatistics,
23 type WorkerNodeOptions,
24 type WorkerType,
c329fd41
JB
25 WorkerTypes,
26 type WorkerUsage
d35e5717 27} from './worker.js'
bde6b5d7 28
e9ed6eee
JB
29/**
30 * Default measurement statistics requirements.
31 */
32export const DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS: MeasurementStatisticsRequirements =
33 {
34 aggregate: false,
35 average: false,
36 median: false
37 }
38
32b141fd
JB
39export const getDefaultTasksQueueOptions = (
40 poolMaxSize: number
41): Required<TasksQueueOptions> => {
42 return {
43 size: Math.pow(poolMaxSize, 2),
44 concurrency: 1,
45 taskStealing: true,
2eee7220 46 tasksStealingOnBackPressure: false,
568d0075 47 tasksFinishedTimeout: 2000
32b141fd
JB
48 }
49}
50
c63a35a0 51export const checkFilePath = (filePath: string | undefined): void => {
c3719753
JB
52 if (filePath == null) {
53 throw new TypeError('The worker file path must be specified')
54 }
55 if (typeof filePath !== 'string') {
56 throw new TypeError('The worker file path must be a string')
57 }
bde6b5d7
JB
58 if (!existsSync(filePath)) {
59 throw new Error(`Cannot find the worker file '${filePath}'`)
60 }
61}
62
c63a35a0
JB
63export const checkDynamicPoolSize = (
64 min: number,
65 max: number | undefined
66): void => {
bde6b5d7
JB
67 if (max == null) {
68 throw new TypeError(
69 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
70 )
71 } else if (!Number.isSafeInteger(max)) {
72 throw new TypeError(
73 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
74 )
75 } else if (min > max) {
76 throw new RangeError(
77 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
78 )
79 } else if (max === 0) {
80 throw new RangeError(
81 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
82 )
83 } else if (min === max) {
84 throw new RangeError(
85 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
86 )
87 }
88}
89
d0bd5062
JB
90export const checkValidPriority = (priority: number | undefined): void => {
91 if (priority != null && !Number.isSafeInteger(priority)) {
85bbc7ab 92 throw new TypeError(`Invalid property 'priority': '${priority}'`)
d0bd5062
JB
93 }
94 if (
95 priority != null &&
96 Number.isSafeInteger(priority) &&
97 (priority < -20 || priority > 19)
98 ) {
85bbc7ab 99 throw new RangeError("Property 'priority' must be between -20 and 19")
d0bd5062
JB
100 }
101}
102
bde6b5d7 103export const checkValidWorkerChoiceStrategy = (
c63a35a0 104 workerChoiceStrategy: WorkerChoiceStrategy | undefined
bde6b5d7
JB
105): void => {
106 if (
107 workerChoiceStrategy != null &&
108 !Object.values(WorkerChoiceStrategies).includes(workerChoiceStrategy)
109 ) {
110 throw new Error(`Invalid worker choice strategy '${workerChoiceStrategy}'`)
111 }
112}
113
114export const checkValidTasksQueueOptions = (
c63a35a0 115 tasksQueueOptions: TasksQueueOptions | undefined
bde6b5d7
JB
116): void => {
117 if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
118 throw new TypeError('Invalid tasks queue options: must be a plain object')
119 }
120 if (
121 tasksQueueOptions?.concurrency != null &&
122 !Number.isSafeInteger(tasksQueueOptions.concurrency)
123 ) {
124 throw new TypeError(
125 'Invalid worker node tasks concurrency: must be an integer'
126 )
127 }
128 if (
129 tasksQueueOptions?.concurrency != null &&
130 tasksQueueOptions.concurrency <= 0
131 ) {
132 throw new RangeError(
133 `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero`
134 )
135 }
136 if (
137 tasksQueueOptions?.size != null &&
138 !Number.isSafeInteger(tasksQueueOptions.size)
139 ) {
140 throw new TypeError(
141 'Invalid worker node tasks queue size: must be an integer'
142 )
143 }
144 if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) {
145 throw new RangeError(
146 `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero`
147 )
148 }
149}
bfc75cca 150
c3719753 151export const checkWorkerNodeArguments = (
c63a35a0
JB
152 type: WorkerType | undefined,
153 filePath: string | undefined,
154 opts: WorkerNodeOptions | undefined
9a38f99e 155): void => {
c3719753
JB
156 if (type == null) {
157 throw new TypeError('Cannot construct a worker node without a worker type')
158 }
159 if (!Object.values(WorkerTypes).includes(type)) {
160 throw new TypeError(
161 `Cannot construct a worker node with an invalid worker type '${type}'`
162 )
9a38f99e 163 }
c3719753
JB
164 checkFilePath(filePath)
165 if (opts == null) {
9a38f99e 166 throw new TypeError(
c3719753 167 'Cannot construct a worker node without worker node options'
9a38f99e
JB
168 )
169 }
9974369e 170 if (!isPlainObject(opts)) {
9a38f99e 171 throw new TypeError(
fcfc3353 172 'Cannot construct a worker node with invalid worker node options: must be a plain object'
9a38f99e
JB
173 )
174 }
c3719753
JB
175 if (opts.tasksQueueBackPressureSize == null) {
176 throw new TypeError(
177 'Cannot construct a worker node without a tasks queue back pressure size option'
178 )
179 }
180 if (!Number.isSafeInteger(opts.tasksQueueBackPressureSize)) {
181 throw new TypeError(
182 'Cannot construct a worker node with a tasks queue back pressure size option that is not an integer'
183 )
184 }
185 if (opts.tasksQueueBackPressureSize <= 0) {
9a38f99e 186 throw new RangeError(
c3719753 187 'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer'
9a38f99e
JB
188 )
189 }
59ca7cff
JB
190 if (opts.tasksQueueBucketSize == null) {
191 throw new TypeError(
192 'Cannot construct a worker node without a tasks queue bucket size option'
193 )
194 }
195 if (!Number.isSafeInteger(opts.tasksQueueBucketSize)) {
196 throw new TypeError(
197 'Cannot construct a worker node with a tasks queue bucket size option that is not an integer'
198 )
199 }
200 if (opts.tasksQueueBucketSize <= 0) {
201 throw new RangeError(
202 'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
203 )
204 }
fcfc3353
JB
205 if (opts.tasksQueuePriority == null) {
206 throw new TypeError(
207 'Cannot construct a worker node without a tasks queue priority option'
208 )
209 }
210 if (typeof opts.tasksQueuePriority !== 'boolean') {
211 throw new TypeError(
212 'Cannot construct a worker node with a tasks queue priority option that is not a boolean'
213 )
214 }
9a38f99e 215}
bfc75cca
JB
216
217/**
218 * Updates the given measurement statistics.
219 *
220 * @param measurementStatistics - The measurement statistics to update.
221 * @param measurementRequirements - The measurement statistics requirements.
222 * @param measurementValue - The measurement value.
bfc75cca
JB
223 * @internal
224 */
c329fd41 225const updateMeasurementStatistics = (
bfc75cca 226 measurementStatistics: MeasurementStatistics,
c63a35a0
JB
227 measurementRequirements: MeasurementStatisticsRequirements | undefined,
228 measurementValue: number | undefined
bfc75cca 229): void => {
c63a35a0
JB
230 if (
231 measurementRequirements != null &&
232 measurementValue != null &&
233 measurementRequirements.aggregate
234 ) {
bfc75cca
JB
235 measurementStatistics.aggregate =
236 (measurementStatistics.aggregate ?? 0) + measurementValue
237 measurementStatistics.minimum = min(
238 measurementValue,
80115618 239 measurementStatistics.minimum ?? Number.POSITIVE_INFINITY
bfc75cca
JB
240 )
241 measurementStatistics.maximum = max(
242 measurementValue,
80115618 243 measurementStatistics.maximum ?? Number.NEGATIVE_INFINITY
bfc75cca 244 )
c63a35a0 245 if (measurementRequirements.average || measurementRequirements.median) {
f12182ad 246 measurementStatistics.history.put(measurementValue)
bfc75cca 247 if (measurementRequirements.average) {
f12182ad
JB
248 measurementStatistics.average = average(
249 measurementStatistics.history.toArray()
250 )
bfc75cca
JB
251 } else if (measurementStatistics.average != null) {
252 delete measurementStatistics.average
253 }
254 if (measurementRequirements.median) {
f12182ad
JB
255 measurementStatistics.median = median(
256 measurementStatistics.history.toArray()
257 )
bfc75cca
JB
258 } else if (measurementStatistics.median != null) {
259 delete measurementStatistics.median
260 }
261 }
262 }
263}
c329fd41
JB
264if (env.NODE_ENV === 'test') {
265 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
266 exports.updateMeasurementStatistics = updateMeasurementStatistics
267}
268
269export const updateWaitTimeWorkerUsage = <
270 Worker extends IWorker,
271 Data = unknown,
272 Response = unknown
273>(
5bdd0e9a 274 workerChoiceStrategiesContext:
bcfb06ce 275 | WorkerChoiceStrategiesContext<Worker, Data, Response>
c63a35a0 276 | undefined,
c329fd41
JB
277 workerUsage: WorkerUsage,
278 task: Task<Data>
279 ): void => {
280 const timestamp = performance.now()
281 const taskWaitTime = timestamp - (task.timestamp ?? timestamp)
282 updateMeasurementStatistics(
283 workerUsage.waitTime,
5bdd0e9a 284 workerChoiceStrategiesContext?.getTaskStatisticsRequirements().waitTime,
c329fd41
JB
285 taskWaitTime
286 )
287}
288
289export const updateTaskStatisticsWorkerUsage = <Response = unknown>(
290 workerUsage: WorkerUsage,
291 message: MessageValue<Response>
292): void => {
293 const workerTaskStatistics = workerUsage.tasks
294 if (
c63a35a0 295 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
c329fd41
JB
296 workerTaskStatistics.executing != null &&
297 workerTaskStatistics.executing > 0
298 ) {
299 --workerTaskStatistics.executing
300 }
301 if (message.workerError == null) {
302 ++workerTaskStatistics.executed
303 } else {
304 ++workerTaskStatistics.failed
305 }
306}
307
308export const updateRunTimeWorkerUsage = <
309 Worker extends IWorker,
310 Data = unknown,
311 Response = unknown
312>(
5bdd0e9a 313 workerChoiceStrategiesContext:
bcfb06ce 314 | WorkerChoiceStrategiesContext<Worker, Data, Response>
c63a35a0 315 | undefined,
c329fd41
JB
316 workerUsage: WorkerUsage,
317 message: MessageValue<Response>
318 ): void => {
319 if (message.workerError != null) {
320 return
321 }
322 updateMeasurementStatistics(
323 workerUsage.runTime,
5bdd0e9a 324 workerChoiceStrategiesContext?.getTaskStatisticsRequirements().runTime,
c329fd41
JB
325 message.taskPerformance?.runTime ?? 0
326 )
327}
328
329export const updateEluWorkerUsage = <
330 Worker extends IWorker,
331 Data = unknown,
332 Response = unknown
333>(
5bdd0e9a 334 workerChoiceStrategiesContext:
bcfb06ce 335 | WorkerChoiceStrategiesContext<Worker, Data, Response>
c63a35a0 336 | undefined,
c329fd41
JB
337 workerUsage: WorkerUsage,
338 message: MessageValue<Response>
339 ): void => {
340 if (message.workerError != null) {
341 return
342 }
c63a35a0 343 const eluTaskStatisticsRequirements =
5bdd0e9a 344 workerChoiceStrategiesContext?.getTaskStatisticsRequirements().elu
c329fd41
JB
345 updateMeasurementStatistics(
346 workerUsage.elu.active,
347 eluTaskStatisticsRequirements,
348 message.taskPerformance?.elu?.active ?? 0
349 )
350 updateMeasurementStatistics(
351 workerUsage.elu.idle,
352 eluTaskStatisticsRequirements,
353 message.taskPerformance?.elu?.idle ?? 0
354 )
c63a35a0 355 if (eluTaskStatisticsRequirements?.aggregate === true) {
c329fd41
JB
356 if (message.taskPerformance?.elu != null) {
357 if (workerUsage.elu.utilization != null) {
358 workerUsage.elu.utilization =
359 (workerUsage.elu.utilization +
360 message.taskPerformance.elu.utilization) /
361 2
362 } else {
363 workerUsage.elu.utilization = message.taskPerformance.elu.utilization
364 }
365 }
366 }
367}
c3719753
JB
368
369export const createWorker = <Worker extends IWorker>(
370 type: WorkerType,
371 filePath: string,
372 opts: { env?: Record<string, unknown>, workerOptions?: WorkerOptions }
373): Worker => {
374 switch (type) {
375 case WorkerTypes.thread:
e9ed6eee 376 return new ThreadWorker(filePath, {
c3719753 377 env: SHARE_ENV,
c63a35a0 378 ...opts.workerOptions
c3719753
JB
379 }) as unknown as Worker
380 case WorkerTypes.cluster:
c63a35a0 381 return cluster.fork(opts.env) as unknown as Worker
c3719753
JB
382 default:
383 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
384 throw new Error(`Unknown worker type '${type}'`)
385 }
386}
d41a44de 387
e9ed6eee
JB
388/**
389 * Returns the worker type of the given worker.
390 *
391 * @param worker - The worker to get the type of.
392 * @returns The worker type of the given worker.
393 * @internal
394 */
395export const getWorkerType = (worker: IWorker): WorkerType | undefined => {
396 if (worker instanceof ThreadWorker) {
397 return WorkerTypes.thread
398 } else if (worker instanceof ClusterWorker) {
399 return WorkerTypes.cluster
400 }
401}
402
403/**
404 * Returns the worker id of the given worker.
405 *
406 * @param worker - The worker to get the id of.
407 * @returns The worker id of the given worker.
408 * @internal
409 */
410export const getWorkerId = (worker: IWorker): number | undefined => {
411 if (worker instanceof ThreadWorker) {
412 return worker.threadId
413 } else if (worker instanceof ClusterWorker) {
414 return worker.id
415 }
416}
417
d41a44de
JB
418export const waitWorkerNodeEvents = async <
419 Worker extends IWorker,
420 Data = unknown
421>(
422 workerNode: IWorkerNode<Worker, Data>,
423 workerNodeEvent: string,
32b141fd
JB
424 numberOfEventsToWait: number,
425 timeout: number
d41a44de
JB
426): Promise<number> => {
427 return await new Promise<number>(resolve => {
428 let events = 0
429 if (numberOfEventsToWait === 0) {
430 resolve(events)
431 return
432 }
2ef26de4
JB
433 switch (workerNodeEvent) {
434 case 'idle':
435 case 'backPressure':
436 case 'taskFinished':
437 workerNode.on(workerNodeEvent, () => {
438 ++events
439 if (events === numberOfEventsToWait) {
440 resolve(events)
441 }
442 })
443 break
444 default:
445 throw new Error('Invalid worker node event')
446 }
6f3a391b 447 if (timeout >= 0) {
32b141fd
JB
448 setTimeout(() => {
449 resolve(events)
450 }, timeout)
451 }
d41a44de
JB
452 })
453}