1 import { randomUUID
} from
'node:crypto'
2 import { performance
} from
'node:perf_hooks'
3 import { existsSync
} from
'node:fs'
4 import { type TransferListItem
} from
'node:worker_threads'
7 PromiseResponseWrapper
,
9 } from
'../utility-types'
12 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
21 updateMeasurementStatistics
23 import { KillBehaviors
} from
'../worker/worker-options'
24 import type { TaskFunction
} from
'../worker/task-functions'
33 type TasksQueueOptions
43 type MeasurementStatisticsRequirements
,
45 WorkerChoiceStrategies
,
46 type WorkerChoiceStrategy
,
47 type WorkerChoiceStrategyOptions
48 } from
'./selection-strategies/selection-strategies-types'
49 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
50 import { version
} from
'./version'
51 import { WorkerNode
} from
'./worker-node'
54 * Base class that implements some shared logic for all poolifier pools.
56 * @typeParam Worker - Type of worker which manages this pool.
57 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
58 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
60 export abstract class AbstractPool
<
61 Worker
extends IWorker
,
64 > implements IPool
<Worker
, Data
, Response
> {
66 public readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>> = []
69 public readonly emitter
?: PoolEmitter
72 * The task execution response promise map.
74 * - `key`: The message id of each submitted task.
75 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
77 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
79 protected promiseResponseMap
: Map
<string, PromiseResponseWrapper
<Response
>> =
80 new Map
<string, PromiseResponseWrapper
<Response
>>()
83 * Worker choice strategy context referencing a worker choice algorithm implementation.
85 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
92 * Dynamic pool maximum size property placeholder.
94 protected readonly max
?: number
97 * Whether the pool is starting or not.
99 private readonly starting
: boolean
101 * Whether the pool is started or not.
103 private started
: boolean
105 * The start timestamp of the pool.
107 private readonly startTimestamp
110 * Constructs a new poolifier pool.
112 * @param numberOfWorkers - Number of workers that this pool should manage.
113 * @param filePath - Path to the worker file.
114 * @param opts - Options for the pool.
117 protected readonly numberOfWorkers
: number,
118 protected readonly filePath
: string,
119 protected readonly opts
: PoolOptions
<Worker
>
121 if (!this.isMain()) {
123 'Cannot start a pool from a worker with the same type as the pool'
126 this.checkNumberOfWorkers(this.numberOfWorkers
)
127 this.checkFilePath(this.filePath
)
128 this.checkPoolOptions(this.opts
)
130 this.chooseWorkerNode
= this.chooseWorkerNode
.bind(this)
131 this.executeTask
= this.executeTask
.bind(this)
132 this.enqueueTask
= this.enqueueTask
.bind(this)
134 if (this.opts
.enableEvents
=== true) {
135 this.emitter
= new PoolEmitter()
137 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
143 this.opts
.workerChoiceStrategy
,
144 this.opts
.workerChoiceStrategyOptions
151 this.starting
= false
154 this.startTimestamp
= performance
.now()
157 private checkFilePath (filePath
: string): void {
160 typeof filePath
!== 'string' ||
161 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
163 throw new Error('Please specify a file with a worker implementation')
165 if (!existsSync(filePath
)) {
166 throw new Error(`Cannot find the worker file '${filePath}'`)
170 private checkNumberOfWorkers (numberOfWorkers
: number): void {
171 if (numberOfWorkers
== null) {
173 'Cannot instantiate a pool without specifying the number of workers'
175 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
177 'Cannot instantiate a pool with a non safe integer number of workers'
179 } else if (numberOfWorkers
< 0) {
180 throw new RangeError(
181 'Cannot instantiate a pool with a negative number of workers'
183 } else if (this.type === PoolTypes
.fixed
&& numberOfWorkers
=== 0) {
184 throw new RangeError('Cannot instantiate a fixed pool with zero worker')
188 protected checkDynamicPoolSize (min
: number, max
: number): void {
189 if (this.type === PoolTypes
.dynamic
) {
192 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
194 } else if (!Number.isSafeInteger(max
)) {
196 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
198 } else if (min
> max
) {
199 throw new RangeError(
200 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
202 } else if (max
=== 0) {
203 throw new RangeError(
204 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
206 } else if (min
=== max
) {
207 throw new RangeError(
208 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
214 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
215 if (isPlainObject(opts
)) {
216 this.opts
.workerChoiceStrategy
=
217 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
218 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
219 this.opts
.workerChoiceStrategyOptions
= {
220 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
221 ...opts
.workerChoiceStrategyOptions
223 this.checkValidWorkerChoiceStrategyOptions(
224 this.opts
.workerChoiceStrategyOptions
226 this.opts
.restartWorkerOnError
= opts
.restartWorkerOnError
?? true
227 this.opts
.enableEvents
= opts
.enableEvents
?? true
228 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
229 if (this.opts
.enableTasksQueue
) {
230 this.checkValidTasksQueueOptions(
231 opts
.tasksQueueOptions
as TasksQueueOptions
233 this.opts
.tasksQueueOptions
= this.buildTasksQueueOptions(
234 opts
.tasksQueueOptions
as TasksQueueOptions
238 throw new TypeError('Invalid pool options: must be a plain object')
242 private checkValidWorkerChoiceStrategy (
243 workerChoiceStrategy
: WorkerChoiceStrategy
245 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
247 `Invalid worker choice strategy '${workerChoiceStrategy}'`
252 private checkValidWorkerChoiceStrategyOptions (
253 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
255 if (!isPlainObject(workerChoiceStrategyOptions
)) {
257 'Invalid worker choice strategy options: must be a plain object'
261 workerChoiceStrategyOptions
.retries
!= null &&
262 !Number.isSafeInteger(workerChoiceStrategyOptions
.retries
)
265 'Invalid worker choice strategy options: retries must be an integer'
269 workerChoiceStrategyOptions
.retries
!= null &&
270 workerChoiceStrategyOptions
.retries
< 0
272 throw new RangeError(
273 `Invalid worker choice strategy options: retries '${workerChoiceStrategyOptions.retries}' must be greater or equal than zero`
277 workerChoiceStrategyOptions
.weights
!= null &&
278 Object.keys(workerChoiceStrategyOptions
.weights
).length
!== this.maxSize
281 'Invalid worker choice strategy options: must have a weight for each worker node'
285 workerChoiceStrategyOptions
.measurement
!= null &&
286 !Object.values(Measurements
).includes(
287 workerChoiceStrategyOptions
.measurement
291 `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
296 private checkValidTasksQueueOptions (
297 tasksQueueOptions
: TasksQueueOptions
299 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
300 throw new TypeError('Invalid tasks queue options: must be a plain object')
303 tasksQueueOptions
?.concurrency
!= null &&
304 !Number.isSafeInteger(tasksQueueOptions
?.concurrency
)
307 'Invalid worker node tasks concurrency: must be an integer'
311 tasksQueueOptions
?.concurrency
!= null &&
312 tasksQueueOptions
?.concurrency
<= 0
314 throw new RangeError(
315 `Invalid worker node tasks concurrency: ${tasksQueueOptions?.concurrency} is a negative integer or zero`
318 if (tasksQueueOptions
?.queueMaxSize
!= null) {
320 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead'
324 tasksQueueOptions
?.size
!= null &&
325 !Number.isSafeInteger(tasksQueueOptions
?.size
)
328 'Invalid worker node tasks queue size: must be an integer'
331 if (tasksQueueOptions
?.size
!= null && tasksQueueOptions
?.size
<= 0) {
332 throw new RangeError(
333 `Invalid worker node tasks queue size: ${tasksQueueOptions?.size} is a negative integer or zero`
338 private startPool (): void {
340 this.workerNodes
.reduce(
341 (accumulator
, workerNode
) =>
342 !workerNode
.info
.dynamic
? accumulator
+ 1 : accumulator
,
344 ) < this.numberOfWorkers
346 this.createAndSetupWorkerNode()
351 public get
info (): PoolInfo
{
357 strategy
: this.opts
.workerChoiceStrategy
as WorkerChoiceStrategy
,
358 minSize
: this.minSize
,
359 maxSize
: this.maxSize
,
360 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
361 .runTime
.aggregate
&&
362 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
363 .waitTime
.aggregate
&& { utilization
: round(this.utilization
) }),
364 workerNodes
: this.workerNodes
.length
,
365 idleWorkerNodes
: this.workerNodes
.reduce(
366 (accumulator
, workerNode
) =>
367 workerNode
.usage
.tasks
.executing
=== 0
372 busyWorkerNodes
: this.workerNodes
.reduce(
373 (accumulator
, workerNode
) =>
374 workerNode
.usage
.tasks
.executing
> 0 ? accumulator
+ 1 : accumulator
,
377 executedTasks
: this.workerNodes
.reduce(
378 (accumulator
, workerNode
) =>
379 accumulator
+ workerNode
.usage
.tasks
.executed
,
382 executingTasks
: this.workerNodes
.reduce(
383 (accumulator
, workerNode
) =>
384 accumulator
+ workerNode
.usage
.tasks
.executing
,
387 ...(this.opts
.enableTasksQueue
=== true && {
388 queuedTasks
: this.workerNodes
.reduce(
389 (accumulator
, workerNode
) =>
390 accumulator
+ workerNode
.usage
.tasks
.queued
,
394 ...(this.opts
.enableTasksQueue
=== true && {
395 maxQueuedTasks
: this.workerNodes
.reduce(
396 (accumulator
, workerNode
) =>
397 accumulator
+ (workerNode
.usage
.tasks
?.maxQueued
?? 0),
401 ...(this.opts
.enableTasksQueue
=== true && {
402 backPressure
: this.hasBackPressure()
404 ...(this.opts
.enableTasksQueue
=== true && {
405 stolenTasks
: this.workerNodes
.reduce(
406 (accumulator
, workerNode
) =>
407 accumulator
+ workerNode
.usage
.tasks
.stolen
,
411 failedTasks
: this.workerNodes
.reduce(
412 (accumulator
, workerNode
) =>
413 accumulator
+ workerNode
.usage
.tasks
.failed
,
416 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
417 .runTime
.aggregate
&& {
421 ...this.workerNodes
.map(
422 workerNode
=> workerNode
.usage
.runTime
?.minimum
?? Infinity
428 ...this.workerNodes
.map(
429 workerNode
=> workerNode
.usage
.runTime
?.maximum
?? -Infinity
433 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
434 .runTime
.average
&& {
437 this.workerNodes
.reduce
<number[]>(
438 (accumulator
, workerNode
) =>
439 accumulator
.concat(workerNode
.usage
.runTime
.history
),
445 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
449 this.workerNodes
.reduce
<number[]>(
450 (accumulator
, workerNode
) =>
451 accumulator
.concat(workerNode
.usage
.runTime
.history
),
459 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
460 .waitTime
.aggregate
&& {
464 ...this.workerNodes
.map(
465 workerNode
=> workerNode
.usage
.waitTime
?.minimum
?? Infinity
471 ...this.workerNodes
.map(
472 workerNode
=> workerNode
.usage
.waitTime
?.maximum
?? -Infinity
476 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
477 .waitTime
.average
&& {
480 this.workerNodes
.reduce
<number[]>(
481 (accumulator
, workerNode
) =>
482 accumulator
.concat(workerNode
.usage
.waitTime
.history
),
488 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
489 .waitTime
.median
&& {
492 this.workerNodes
.reduce
<number[]>(
493 (accumulator
, workerNode
) =>
494 accumulator
.concat(workerNode
.usage
.waitTime
.history
),
506 * The pool readiness boolean status.
508 private get
ready (): boolean {
510 this.workerNodes
.reduce(
511 (accumulator
, workerNode
) =>
512 !workerNode
.info
.dynamic
&& workerNode
.info
.ready
521 * The approximate pool utilization.
523 * @returns The pool utilization.
525 private get
utilization (): number {
526 const poolTimeCapacity
=
527 (performance
.now() - this.startTimestamp
) * this.maxSize
528 const totalTasksRunTime
= this.workerNodes
.reduce(
529 (accumulator
, workerNode
) =>
530 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
533 const totalTasksWaitTime
= this.workerNodes
.reduce(
534 (accumulator
, workerNode
) =>
535 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
538 return (totalTasksRunTime
+ totalTasksWaitTime
) / poolTimeCapacity
544 * If it is `'dynamic'`, it provides the `max` property.
546 protected abstract get
type (): PoolType
551 protected abstract get
worker (): WorkerType
554 * The pool minimum size.
556 protected get
minSize (): number {
557 return this.numberOfWorkers
561 * The pool maximum size.
563 protected get
maxSize (): number {
564 return this.max
?? this.numberOfWorkers
568 * Checks if the worker id sent in the received message from a worker is valid.
570 * @param message - The received message.
571 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
573 private checkMessageWorkerId (message
: MessageValue
<Response
>): void {
574 if (message
.workerId
== null) {
575 throw new Error('Worker message received without worker id')
577 message
.workerId
!= null &&
578 this.getWorkerNodeKeyByWorkerId(message
.workerId
) === -1
581 `Worker message received from unknown worker '${message.workerId}'`
587 * Gets the given worker its worker node key.
589 * @param worker - The worker.
590 * @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
592 private getWorkerNodeKeyByWorker (worker
: Worker
): number {
593 return this.workerNodes
.findIndex(
594 workerNode
=> workerNode
.worker
=== worker
599 * Gets the worker node key given its worker id.
601 * @param workerId - The worker id.
602 * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
604 private getWorkerNodeKeyByWorkerId (workerId
: number): number {
605 return this.workerNodes
.findIndex(
606 workerNode
=> workerNode
.info
.id
=== workerId
611 public setWorkerChoiceStrategy (
612 workerChoiceStrategy
: WorkerChoiceStrategy
,
613 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
615 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
616 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
617 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
618 this.opts
.workerChoiceStrategy
620 if (workerChoiceStrategyOptions
!= null) {
621 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
623 for (const [workerNodeKey
, workerNode
] of this.workerNodes
.entries()) {
624 workerNode
.resetUsage()
625 this.sendStatisticsMessageToWorker(workerNodeKey
)
630 public setWorkerChoiceStrategyOptions (
631 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
633 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
634 this.opts
.workerChoiceStrategyOptions
= {
635 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
636 ...workerChoiceStrategyOptions
638 this.workerChoiceStrategyContext
.setOptions(
639 this.opts
.workerChoiceStrategyOptions
644 public enableTasksQueue (
646 tasksQueueOptions
?: TasksQueueOptions
648 if (this.opts
.enableTasksQueue
=== true && !enable
) {
649 this.flushTasksQueues()
651 this.opts
.enableTasksQueue
= enable
652 this.setTasksQueueOptions(tasksQueueOptions
as TasksQueueOptions
)
656 public setTasksQueueOptions (tasksQueueOptions
: TasksQueueOptions
): void {
657 if (this.opts
.enableTasksQueue
=== true) {
658 this.checkValidTasksQueueOptions(tasksQueueOptions
)
659 this.opts
.tasksQueueOptions
=
660 this.buildTasksQueueOptions(tasksQueueOptions
)
661 this.setTasksQueueSize(this.opts
.tasksQueueOptions
.size
as number)
662 } else if (this.opts
.tasksQueueOptions
!= null) {
663 delete this.opts
.tasksQueueOptions
667 private setTasksQueueSize (size
: number): void {
668 for (const workerNode
of this.workerNodes
) {
669 workerNode
.tasksQueueBackPressureSize
= size
673 private buildTasksQueueOptions (
674 tasksQueueOptions
: TasksQueueOptions
675 ): TasksQueueOptions
{
678 size
: Math.pow(this.maxSize
, 2),
686 * Whether the pool is full or not.
688 * The pool filling boolean status.
690 protected get
full (): boolean {
691 return this.workerNodes
.length
>= this.maxSize
695 * Whether the pool is busy or not.
697 * The pool busyness boolean status.
699 protected abstract get
busy (): boolean
702 * Whether worker nodes are executing concurrently their tasks quota or not.
704 * @returns Worker nodes busyness boolean status.
706 protected internalBusy (): boolean {
707 if (this.opts
.enableTasksQueue
=== true) {
709 this.workerNodes
.findIndex(
711 workerNode
.info
.ready
&&
712 workerNode
.usage
.tasks
.executing
<
713 (this.opts
.tasksQueueOptions
?.concurrency
as number)
718 this.workerNodes
.findIndex(
720 workerNode
.info
.ready
&& workerNode
.usage
.tasks
.executing
=== 0
726 private sendToWorkers (message
: Omit
<MessageValue
<Data
>, 'workerId'>): number {
727 let messagesCount
= 0
728 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
729 this.sendToWorker(workerNodeKey
, {
731 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number
739 public hasTaskFunction (name
: string): boolean {
740 for (const workerNode
of this.workerNodes
) {
742 Array.isArray(workerNode
.info
.taskFunctionNames
) &&
743 workerNode
.info
.taskFunctionNames
.includes(name
)
752 public addTaskFunction (name
: string, taskFunction
: TaskFunction
): boolean {
754 taskFunctionOperation
: 'add',
755 taskFunctionName
: name
,
756 taskFunction
: taskFunction
.toString()
762 public removeTaskFunction (name
: string): boolean {
764 taskFunctionOperation
: 'remove',
765 taskFunctionName
: name
771 public listTaskFunctionNames (): string[] {
772 for (const workerNode
of this.workerNodes
) {
774 Array.isArray(workerNode
.info
.taskFunctionNames
) &&
775 workerNode
.info
.taskFunctionNames
.length
> 0
777 return workerNode
.info
.taskFunctionNames
784 public setDefaultTaskFunction (name
: string): boolean {
786 taskFunctionOperation
: 'default',
787 taskFunctionName
: name
792 private shallExecuteTask (workerNodeKey
: number): boolean {
794 this.tasksQueueSize(workerNodeKey
) === 0 &&
795 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
796 (this.opts
.tasksQueueOptions
?.concurrency
as number)
801 public async execute (
804 transferList
?: TransferListItem
[]
805 ): Promise
<Response
> {
806 return await new Promise
<Response
>((resolve
, reject
) => {
808 reject(new Error('Cannot execute a task on destroyed pool'))
811 if (name
!= null && typeof name
!== 'string') {
812 reject(new TypeError('name argument must be a string'))
817 typeof name
=== 'string' &&
818 name
.trim().length
=== 0
820 reject(new TypeError('name argument must not be an empty string'))
823 if (transferList
!= null && !Array.isArray(transferList
)) {
824 reject(new TypeError('transferList argument must be an array'))
827 const timestamp
= performance
.now()
828 const workerNodeKey
= this.chooseWorkerNode()
829 const task
: Task
<Data
> = {
830 name
: name
?? DEFAULT_TASK_NAME
,
831 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
832 data
: data
?? ({} as Data
),
835 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number,
838 this.promiseResponseMap
.set(task
.taskId
as string, {
844 this.opts
.enableTasksQueue
=== false ||
845 (this.opts
.enableTasksQueue
=== true &&
846 this.shallExecuteTask(workerNodeKey
))
848 this.executeTask(workerNodeKey
, task
)
850 this.enqueueTask(workerNodeKey
, task
)
856 public async destroy (): Promise
<void> {
858 this.workerNodes
.map(async (_
, workerNodeKey
) => {
859 await this.destroyWorkerNode(workerNodeKey
)
862 this.emitter
?.emit(PoolEvents
.destroy
, this.info
)
866 protected async sendKillMessageToWorker (
867 workerNodeKey
: number,
870 await new Promise
<void>((resolve
, reject
) => {
871 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
872 if (message
.kill
=== 'success') {
874 } else if (message
.kill
=== 'failure') {
875 reject(new Error(`Worker ${workerId} kill message handling failed`))
878 this.sendToWorker(workerNodeKey
, { kill
: true, workerId
})
883 * Terminates the worker node given its worker node key.
885 * @param workerNodeKey - The worker node key.
887 protected abstract destroyWorkerNode (workerNodeKey
: number): Promise
<void>
890 * Setup hook to execute code before worker nodes are created in the abstract constructor.
895 protected setupHook (): void {
896 /* Intentionally empty */
900 * Should return whether the worker is the main worker or not.
902 protected abstract isMain (): boolean
905 * Hook executed before the worker task execution.
908 * @param workerNodeKey - The worker node key.
909 * @param task - The task to execute.
911 protected beforeTaskExecutionHook (
912 workerNodeKey
: number,
915 if (this.workerNodes
[workerNodeKey
]?.usage
!= null) {
916 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
917 ++workerUsage
.tasks
.executing
918 this.updateWaitTimeWorkerUsage(workerUsage
, task
)
921 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
922 this.workerNodes
[workerNodeKey
].getTaskFunctionWorkerUsage(
926 const taskFunctionWorkerUsage
= this.workerNodes
[
928 ].getTaskFunctionWorkerUsage(task
.name
as string) as WorkerUsage
929 ++taskFunctionWorkerUsage
.tasks
.executing
930 this.updateWaitTimeWorkerUsage(taskFunctionWorkerUsage
, task
)
935 * Hook executed after the worker task execution.
938 * @param workerNodeKey - The worker node key.
939 * @param message - The received message.
941 protected afterTaskExecutionHook (
942 workerNodeKey
: number,
943 message
: MessageValue
<Response
>
945 if (this.workerNodes
[workerNodeKey
]?.usage
!= null) {
946 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
947 this.updateTaskStatisticsWorkerUsage(workerUsage
, message
)
948 this.updateRunTimeWorkerUsage(workerUsage
, message
)
949 this.updateEluWorkerUsage(workerUsage
, message
)
952 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
953 this.workerNodes
[workerNodeKey
].getTaskFunctionWorkerUsage(
954 message
.taskPerformance
?.name
as string
957 const taskFunctionWorkerUsage
= this.workerNodes
[
959 ].getTaskFunctionWorkerUsage(
960 message
.taskPerformance
?.name
as string
962 this.updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage
, message
)
963 this.updateRunTimeWorkerUsage(taskFunctionWorkerUsage
, message
)
964 this.updateEluWorkerUsage(taskFunctionWorkerUsage
, message
)
969 * Whether the worker node shall update its task function worker usage or not.
971 * @param workerNodeKey - The worker node key.
972 * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
974 private shallUpdateTaskFunctionWorkerUsage (workerNodeKey
: number): boolean {
975 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
977 workerInfo
!= null &&
978 Array.isArray(workerInfo
.taskFunctionNames
) &&
979 workerInfo
.taskFunctionNames
.length
> 2
983 private updateTaskStatisticsWorkerUsage (
984 workerUsage
: WorkerUsage
,
985 message
: MessageValue
<Response
>
987 const workerTaskStatistics
= workerUsage
.tasks
989 workerTaskStatistics
.executing
!= null &&
990 workerTaskStatistics
.executing
> 0
992 --workerTaskStatistics
.executing
994 if (message
.workerError
== null) {
995 ++workerTaskStatistics
.executed
997 ++workerTaskStatistics
.failed
1001 private updateRunTimeWorkerUsage (
1002 workerUsage
: WorkerUsage
,
1003 message
: MessageValue
<Response
>
1005 if (message
.workerError
!= null) {
1008 updateMeasurementStatistics(
1009 workerUsage
.runTime
,
1010 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
,
1011 message
.taskPerformance
?.runTime
?? 0
1015 private updateWaitTimeWorkerUsage (
1016 workerUsage
: WorkerUsage
,
1019 const timestamp
= performance
.now()
1020 const taskWaitTime
= timestamp
- (task
.timestamp
?? timestamp
)
1021 updateMeasurementStatistics(
1022 workerUsage
.waitTime
,
1023 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().waitTime
,
1028 private updateEluWorkerUsage (
1029 workerUsage
: WorkerUsage
,
1030 message
: MessageValue
<Response
>
1032 if (message
.workerError
!= null) {
1035 const eluTaskStatisticsRequirements
: MeasurementStatisticsRequirements
=
1036 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().elu
1037 updateMeasurementStatistics(
1038 workerUsage
.elu
.active
,
1039 eluTaskStatisticsRequirements
,
1040 message
.taskPerformance
?.elu
?.active
?? 0
1042 updateMeasurementStatistics(
1043 workerUsage
.elu
.idle
,
1044 eluTaskStatisticsRequirements
,
1045 message
.taskPerformance
?.elu
?.idle
?? 0
1047 if (eluTaskStatisticsRequirements
.aggregate
) {
1048 if (message
.taskPerformance
?.elu
!= null) {
1049 if (workerUsage
.elu
.utilization
!= null) {
1050 workerUsage
.elu
.utilization
=
1051 (workerUsage
.elu
.utilization
+
1052 message
.taskPerformance
.elu
.utilization
) /
1055 workerUsage
.elu
.utilization
= message
.taskPerformance
.elu
.utilization
1062 * Chooses a worker node for the next task.
1064 * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
1066 * @returns The chosen worker node key
1068 private chooseWorkerNode (): number {
1069 if (this.shallCreateDynamicWorker()) {
1070 const workerNodeKey
= this.createAndSetupDynamicWorkerNode()
1072 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerUsage
1074 return workerNodeKey
1077 return this.workerChoiceStrategyContext
.execute()
1081 * Conditions for dynamic worker creation.
1083 * @returns Whether to create a dynamic worker or not.
1085 private shallCreateDynamicWorker (): boolean {
1086 return this.type === PoolTypes
.dynamic
&& !this.full
&& this.internalBusy()
1090 * Sends a message to worker given its worker node key.
1092 * @param workerNodeKey - The worker node key.
1093 * @param message - The message.
1094 * @param transferList - The optional array of transferable objects.
1096 protected abstract sendToWorker (
1097 workerNodeKey
: number,
1098 message
: MessageValue
<Data
>,
1099 transferList
?: TransferListItem
[]
1103 * Creates a new worker.
1105 * @returns Newly created worker.
1107 protected abstract createWorker (): Worker
1110 * Creates a new, completely set up worker node.
1112 * @returns New, completely set up worker node key.
1114 protected createAndSetupWorkerNode (): number {
1115 const worker
= this.createWorker()
1117 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
1118 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
1119 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
1120 worker
.on('error', error
=> {
1121 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1122 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
1123 workerInfo
.ready
= false
1124 this.workerNodes
[workerNodeKey
].closeChannel()
1125 this.emitter
?.emit(PoolEvents
.error
, error
)
1127 this.opts
.restartWorkerOnError
=== true &&
1131 if (workerInfo
.dynamic
) {
1132 this.createAndSetupDynamicWorkerNode()
1134 this.createAndSetupWorkerNode()
1137 if (this.opts
.enableTasksQueue
=== true) {
1138 this.redistributeQueuedTasks(workerNodeKey
)
1141 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
1142 worker
.once('exit', () => {
1143 this.removeWorkerNode(worker
)
1146 const workerNodeKey
= this.addWorkerNode(worker
)
1148 this.afterWorkerNodeSetup(workerNodeKey
)
1150 return workerNodeKey
1154 * Creates a new, completely set up dynamic worker node.
1156 * @returns New, completely set up dynamic worker node key.
1158 protected createAndSetupDynamicWorkerNode (): number {
1159 const workerNodeKey
= this.createAndSetupWorkerNode()
1160 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
1161 const localWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(
1164 const workerUsage
= this.workerNodes
[localWorkerNodeKey
].usage
1165 // Kill message received from worker
1167 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
1168 (isKillBehavior(KillBehaviors
.SOFT
, message
.kill
) &&
1169 ((this.opts
.enableTasksQueue
=== false &&
1170 workerUsage
.tasks
.executing
=== 0) ||
1171 (this.opts
.enableTasksQueue
=== true &&
1172 workerUsage
.tasks
.executing
=== 0 &&
1173 this.tasksQueueSize(localWorkerNodeKey
) === 0)))
1175 this.destroyWorkerNode(localWorkerNodeKey
).catch(error
=> {
1176 this.emitter
?.emit(PoolEvents
.error
, error
)
1180 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
1181 this.sendToWorker(workerNodeKey
, {
1183 workerId
: workerInfo
.id
as number
1185 workerInfo
.dynamic
= true
1187 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerReady
||
1188 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerUsage
1190 workerInfo
.ready
= true
1192 this.checkAndEmitDynamicWorkerCreationEvents()
1193 return workerNodeKey
1197 * Registers a listener callback on the worker given its worker node key.
1199 * @param workerNodeKey - The worker node key.
1200 * @param listener - The message listener callback.
1202 protected abstract registerWorkerMessageListener
<
1203 Message
extends Data
| Response
1205 workerNodeKey
: number,
1206 listener
: (message
: MessageValue
<Message
>) => void
1210 * Method hooked up after a worker node has been newly created.
1211 * Can be overridden.
1213 * @param workerNodeKey - The newly created worker node key.
1215 protected afterWorkerNodeSetup (workerNodeKey
: number): void {
1216 // Listen to worker messages.
1217 this.registerWorkerMessageListener(workerNodeKey
, this.workerListener())
1218 // Send the startup message to worker.
1219 this.sendStartupMessageToWorker(workerNodeKey
)
1220 // Send the statistics message to worker.
1221 this.sendStatisticsMessageToWorker(workerNodeKey
)
1222 if (this.opts
.enableTasksQueue
=== true) {
1223 this.workerNodes
[workerNodeKey
].onEmptyQueue
=
1224 this.taskStealingOnEmptyQueue
.bind(this)
1225 this.workerNodes
[workerNodeKey
].onBackPressure
=
1226 this.tasksStealingOnBackPressure
.bind(this)
1231 * Sends the startup message to worker given its worker node key.
1233 * @param workerNodeKey - The worker node key.
1235 protected abstract sendStartupMessageToWorker (workerNodeKey
: number): void
1238 * Sends the statistics message to worker given its worker node key.
1240 * @param workerNodeKey - The worker node key.
1242 private sendStatisticsMessageToWorker (workerNodeKey
: number): void {
1243 this.sendToWorker(workerNodeKey
, {
1246 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1248 elu
: this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1251 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number
1255 private redistributeQueuedTasks (workerNodeKey
: number): void {
1256 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1257 const destinationWorkerNodeKey
= this.workerNodes
.reduce(
1258 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
1259 return workerNode
.info
.ready
&&
1260 workerNode
.usage
.tasks
.queued
<
1261 workerNodes
[minWorkerNodeKey
].usage
.tasks
.queued
1267 const destinationWorkerNode
= this.workerNodes
[destinationWorkerNodeKey
]
1269 ...(this.dequeueTask(workerNodeKey
) as Task
<Data
>),
1270 workerId
: destinationWorkerNode
.info
.id
as number
1272 if (this.shallExecuteTask(destinationWorkerNodeKey
)) {
1273 this.executeTask(destinationWorkerNodeKey
, task
)
1275 this.enqueueTask(destinationWorkerNodeKey
, task
)
1280 private updateTaskStolenStatisticsWorkerUsage (
1281 workerNodeKey
: number,
1284 const workerNode
= this.workerNodes
[workerNodeKey
]
1285 if (workerNode
?.usage
!= null) {
1286 ++workerNode
.usage
.tasks
.stolen
1289 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
1290 workerNode
.getTaskFunctionWorkerUsage(taskName
) != null
1292 const taskFunctionWorkerUsage
= workerNode
.getTaskFunctionWorkerUsage(
1295 ++taskFunctionWorkerUsage
.tasks
.stolen
1299 private taskStealingOnEmptyQueue (workerId
: number): void {
1300 const destinationWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(workerId
)
1301 const destinationWorkerNode
= this.workerNodes
[destinationWorkerNodeKey
]
1302 const workerNodes
= this.workerNodes
1305 (workerNodeA
, workerNodeB
) =>
1306 workerNodeB
.usage
.tasks
.queued
- workerNodeA
.usage
.tasks
.queued
1308 const sourceWorkerNode
= workerNodes
.find(
1310 workerNode
.info
.ready
&&
1311 workerNode
.info
.id
!== workerId
&&
1312 workerNode
.usage
.tasks
.queued
> 0
1314 if (sourceWorkerNode
!= null) {
1316 ...(sourceWorkerNode
.popTask() as Task
<Data
>),
1317 workerId
: destinationWorkerNode
.info
.id
as number
1319 if (this.shallExecuteTask(destinationWorkerNodeKey
)) {
1320 this.executeTask(destinationWorkerNodeKey
, task
)
1322 this.enqueueTask(destinationWorkerNodeKey
, task
)
1324 this.updateTaskStolenStatisticsWorkerUsage(
1325 destinationWorkerNodeKey
,
1331 private tasksStealingOnBackPressure (workerId
: number): void {
1332 const sizeOffset
= 1
1333 if ((this.opts
.tasksQueueOptions
?.size
as number) <= sizeOffset
) {
1336 const sourceWorkerNode
=
1337 this.workerNodes
[this.getWorkerNodeKeyByWorkerId(workerId
)]
1338 const workerNodes
= this.workerNodes
1341 (workerNodeA
, workerNodeB
) =>
1342 workerNodeA
.usage
.tasks
.queued
- workerNodeB
.usage
.tasks
.queued
1344 for (const [workerNodeKey
, workerNode
] of workerNodes
.entries()) {
1346 sourceWorkerNode
.usage
.tasks
.queued
> 0 &&
1347 workerNode
.info
.ready
&&
1348 workerNode
.info
.id
!== workerId
&&
1349 workerNode
.usage
.tasks
.queued
<
1350 (this.opts
.tasksQueueOptions
?.size
as number) - sizeOffset
1353 ...(sourceWorkerNode
.popTask() as Task
<Data
>),
1354 workerId
: workerNode
.info
.id
as number
1356 if (this.shallExecuteTask(workerNodeKey
)) {
1357 this.executeTask(workerNodeKey
, task
)
1359 this.enqueueTask(workerNodeKey
, task
)
1361 this.updateTaskStolenStatisticsWorkerUsage(
1370 * This method is the listener registered for each worker message.
1372 * @returns The listener function to execute when a message is received from a worker.
1374 protected workerListener (): (message
: MessageValue
<Response
>) => void {
1376 this.checkMessageWorkerId(message
)
1377 if (message
.ready
!= null && message
.taskFunctionNames
!= null) {
1378 // Worker ready response received from worker
1379 this.handleWorkerReadyResponse(message
)
1380 } else if (message
.taskId
!= null) {
1381 // Task execution response received from worker
1382 this.handleTaskExecutionResponse(message
)
1383 } else if (message
.taskFunctionNames
!= null) {
1384 // Task function names message received from worker
1386 this.getWorkerNodeKeyByWorkerId(message
.workerId
)
1387 ).taskFunctionNames
= message
.taskFunctionNames
1388 } else if (message
.taskFunctionOperation
!= null) {
1389 // Task function operation response received from worker
1394 private handleWorkerReadyResponse (message
: MessageValue
<Response
>): void {
1395 if (message
.ready
=== false) {
1396 throw new Error(`Worker ${message.workerId} failed to initialize`)
1398 const workerInfo
= this.getWorkerInfo(
1399 this.getWorkerNodeKeyByWorkerId(message
.workerId
)
1401 workerInfo
.ready
= message
.ready
as boolean
1402 workerInfo
.taskFunctionNames
= message
.taskFunctionNames
1403 if (this.emitter
!= null && this.ready
) {
1404 this.emitter
.emit(PoolEvents
.ready
, this.info
)
1408 private handleTaskExecutionResponse (message
: MessageValue
<Response
>): void {
1409 const { taskId
, workerError
, data
} = message
1410 const promiseResponse
= this.promiseResponseMap
.get(taskId
as string)
1411 if (promiseResponse
!= null) {
1412 if (workerError
!= null) {
1413 this.emitter
?.emit(PoolEvents
.taskError
, workerError
)
1414 promiseResponse
.reject(workerError
.message
)
1416 promiseResponse
.resolve(data
as Response
)
1418 const workerNodeKey
= promiseResponse
.workerNodeKey
1419 this.afterTaskExecutionHook(workerNodeKey
, message
)
1420 this.workerChoiceStrategyContext
.update(workerNodeKey
)
1421 this.promiseResponseMap
.delete(taskId
as string)
1423 this.opts
.enableTasksQueue
=== true &&
1424 this.tasksQueueSize(workerNodeKey
) > 0 &&
1425 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
1426 (this.opts
.tasksQueueOptions
?.concurrency
as number)
1430 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1436 private checkAndEmitTaskExecutionEvents (): void {
1438 this.emitter
?.emit(PoolEvents
.busy
, this.info
)
1442 private checkAndEmitTaskQueuingEvents (): void {
1443 if (this.hasBackPressure()) {
1444 this.emitter
?.emit(PoolEvents
.backPressure
, this.info
)
1448 private checkAndEmitDynamicWorkerCreationEvents (): void {
1449 if (this.type === PoolTypes
.dynamic
) {
1451 this.emitter
?.emit(PoolEvents
.full
, this.info
)
1457 * Gets the worker information given its worker node key.
1459 * @param workerNodeKey - The worker node key.
1460 * @returns The worker information.
1462 protected getWorkerInfo (workerNodeKey
: number): WorkerInfo
{
1463 return this.workerNodes
[workerNodeKey
].info
1467 * Adds the given worker in the pool worker nodes.
1469 * @param worker - The worker.
1470 * @returns The added worker node key.
1471 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1473 private addWorkerNode (worker
: Worker
): number {
1474 const workerNode
= new WorkerNode
<Worker
, Data
>(
1476 this.opts
.tasksQueueOptions
?.size
?? Math.pow(this.maxSize
, 2)
1478 // Flag the worker node as ready at pool startup.
1479 if (this.starting
) {
1480 workerNode
.info
.ready
= true
1482 this.workerNodes
.push(workerNode
)
1483 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1484 if (workerNodeKey
=== -1) {
1485 throw new Error('Worker added not found in worker nodes')
1487 return workerNodeKey
1491 * Removes the given worker from the pool worker nodes.
1493 * @param worker - The worker.
1495 private removeWorkerNode (worker
: Worker
): void {
1496 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1497 if (workerNodeKey
!== -1) {
1498 this.workerNodes
.splice(workerNodeKey
, 1)
1499 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
1504 public hasWorkerNodeBackPressure (workerNodeKey
: number): boolean {
1506 this.opts
.enableTasksQueue
=== true &&
1507 this.workerNodes
[workerNodeKey
].hasBackPressure()
1511 private hasBackPressure (): boolean {
1513 this.opts
.enableTasksQueue
=== true &&
1514 this.workerNodes
.findIndex(
1515 workerNode
=> !workerNode
.hasBackPressure()
1521 * Executes the given task on the worker given its worker node key.
1523 * @param workerNodeKey - The worker node key.
1524 * @param task - The task to execute.
1526 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
1527 this.beforeTaskExecutionHook(workerNodeKey
, task
)
1528 this.sendToWorker(workerNodeKey
, task
, task
.transferList
)
1529 this.checkAndEmitTaskExecutionEvents()
1532 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
1533 const tasksQueueSize
= this.workerNodes
[workerNodeKey
].enqueueTask(task
)
1534 this.checkAndEmitTaskQueuingEvents()
1535 return tasksQueueSize
1538 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
1539 return this.workerNodes
[workerNodeKey
].dequeueTask()
1542 private tasksQueueSize (workerNodeKey
: number): number {
1543 return this.workerNodes
[workerNodeKey
].tasksQueueSize()
1546 protected flushTasksQueue (workerNodeKey
: number): void {
1547 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1550 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1553 this.workerNodes
[workerNodeKey
].clearTasksQueue()
1556 private flushTasksQueues (): void {
1557 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
1558 this.flushTasksQueue(workerNodeKey
)