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:
73 * - `key`: The message id of each submitted task.
74 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
76 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
78 protected promiseResponseMap
: Map
<string, PromiseResponseWrapper
<Response
>> =
79 new Map
<string, PromiseResponseWrapper
<Response
>>()
82 * Worker choice strategy context referencing a worker choice algorithm implementation.
84 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
91 * Dynamic pool maximum size property placeholder.
93 protected readonly max
?: number
96 * The task functions added at runtime map:
97 * - `key`: The task function name.
98 * - `value`: The task function itself.
100 private readonly taskFunctions
: Map
<string, TaskFunction
<Data
, Response
>>
103 * Whether the pool is started or not.
105 private started
: boolean
107 * Whether the pool is starting or not.
109 private starting
: boolean
111 * The start timestamp of the pool.
113 private readonly startTimestamp
116 * Constructs a new poolifier pool.
118 * @param numberOfWorkers - Number of workers that this pool should manage.
119 * @param filePath - Path to the worker file.
120 * @param opts - Options for the pool.
123 protected readonly numberOfWorkers
: number,
124 protected readonly filePath
: string,
125 protected readonly opts
: PoolOptions
<Worker
>
127 if (!this.isMain()) {
129 'Cannot start a pool from a worker with the same type as the pool'
132 this.checkNumberOfWorkers(this.numberOfWorkers
)
133 this.checkFilePath(this.filePath
)
134 this.checkPoolOptions(this.opts
)
136 this.chooseWorkerNode
= this.chooseWorkerNode
.bind(this)
137 this.executeTask
= this.executeTask
.bind(this)
138 this.enqueueTask
= this.enqueueTask
.bind(this)
140 if (this.opts
.enableEvents
=== true) {
141 this.emitter
= new PoolEmitter()
143 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
149 this.opts
.workerChoiceStrategy
,
150 this.opts
.workerChoiceStrategyOptions
155 this.taskFunctions
= new Map
<string, TaskFunction
<Data
, Response
>>()
158 this.starting
= false
159 if (this.opts
.startWorkers
=== true) {
163 this.startTimestamp
= performance
.now()
166 private checkFilePath (filePath
: string): void {
169 typeof filePath
!== 'string' ||
170 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
172 throw new Error('Please specify a file with a worker implementation')
174 if (!existsSync(filePath
)) {
175 throw new Error(`Cannot find the worker file '${filePath}'`)
179 private checkNumberOfWorkers (numberOfWorkers
: number): void {
180 if (numberOfWorkers
== null) {
182 'Cannot instantiate a pool without specifying the number of workers'
184 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
186 'Cannot instantiate a pool with a non safe integer number of workers'
188 } else if (numberOfWorkers
< 0) {
189 throw new RangeError(
190 'Cannot instantiate a pool with a negative number of workers'
192 } else if (this.type === PoolTypes
.fixed
&& numberOfWorkers
=== 0) {
193 throw new RangeError('Cannot instantiate a fixed pool with zero worker')
197 protected checkDynamicPoolSize (min
: number, max
: number): void {
198 if (this.type === PoolTypes
.dynamic
) {
201 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
203 } else if (!Number.isSafeInteger(max
)) {
205 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
207 } else if (min
> max
) {
208 throw new RangeError(
209 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
211 } else if (max
=== 0) {
212 throw new RangeError(
213 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
215 } else if (min
=== max
) {
216 throw new RangeError(
217 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
223 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
224 if (isPlainObject(opts
)) {
225 this.opts
.startWorkers
= opts
.startWorkers
?? true
226 this.opts
.workerChoiceStrategy
=
227 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
228 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
229 this.opts
.workerChoiceStrategyOptions
= {
230 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
231 ...opts
.workerChoiceStrategyOptions
233 this.checkValidWorkerChoiceStrategyOptions(
234 this.opts
.workerChoiceStrategyOptions
236 this.opts
.restartWorkerOnError
= opts
.restartWorkerOnError
?? true
237 this.opts
.enableEvents
= opts
.enableEvents
?? true
238 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
239 if (this.opts
.enableTasksQueue
) {
240 this.checkValidTasksQueueOptions(
241 opts
.tasksQueueOptions
as TasksQueueOptions
243 this.opts
.tasksQueueOptions
= this.buildTasksQueueOptions(
244 opts
.tasksQueueOptions
as TasksQueueOptions
248 throw new TypeError('Invalid pool options: must be a plain object')
252 private checkValidWorkerChoiceStrategy (
253 workerChoiceStrategy
: WorkerChoiceStrategy
255 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
257 `Invalid worker choice strategy '${workerChoiceStrategy}'`
262 private checkValidWorkerChoiceStrategyOptions (
263 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
265 if (!isPlainObject(workerChoiceStrategyOptions
)) {
267 'Invalid worker choice strategy options: must be a plain object'
271 workerChoiceStrategyOptions
.retries
!= null &&
272 !Number.isSafeInteger(workerChoiceStrategyOptions
.retries
)
275 'Invalid worker choice strategy options: retries must be an integer'
279 workerChoiceStrategyOptions
.retries
!= null &&
280 workerChoiceStrategyOptions
.retries
< 0
282 throw new RangeError(
283 `Invalid worker choice strategy options: retries '${workerChoiceStrategyOptions.retries}' must be greater or equal than zero`
287 workerChoiceStrategyOptions
.weights
!= null &&
288 Object.keys(workerChoiceStrategyOptions
.weights
).length
!== this.maxSize
291 'Invalid worker choice strategy options: must have a weight for each worker node'
295 workerChoiceStrategyOptions
.measurement
!= null &&
296 !Object.values(Measurements
).includes(
297 workerChoiceStrategyOptions
.measurement
301 `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
306 private checkValidTasksQueueOptions (
307 tasksQueueOptions
: TasksQueueOptions
309 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
310 throw new TypeError('Invalid tasks queue options: must be a plain object')
313 tasksQueueOptions
?.concurrency
!= null &&
314 !Number.isSafeInteger(tasksQueueOptions
?.concurrency
)
317 'Invalid worker node tasks concurrency: must be an integer'
321 tasksQueueOptions
?.concurrency
!= null &&
322 tasksQueueOptions
?.concurrency
<= 0
324 throw new RangeError(
325 `Invalid worker node tasks concurrency: ${tasksQueueOptions?.concurrency} is a negative integer or zero`
329 tasksQueueOptions
?.size
!= null &&
330 !Number.isSafeInteger(tasksQueueOptions
?.size
)
333 'Invalid worker node tasks queue size: must be an integer'
336 if (tasksQueueOptions
?.size
!= null && tasksQueueOptions
?.size
<= 0) {
337 throw new RangeError(
338 `Invalid worker node tasks queue size: ${tasksQueueOptions?.size} is a negative integer or zero`
344 public get
info (): PoolInfo
{
349 started
: this.started
,
351 strategy
: this.opts
.workerChoiceStrategy
as WorkerChoiceStrategy
,
352 minSize
: this.minSize
,
353 maxSize
: this.maxSize
,
354 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
355 .runTime
.aggregate
&&
356 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
357 .waitTime
.aggregate
&& { utilization
: round(this.utilization
) }),
358 workerNodes
: this.workerNodes
.length
,
359 idleWorkerNodes
: this.workerNodes
.reduce(
360 (accumulator
, workerNode
) =>
361 workerNode
.usage
.tasks
.executing
=== 0
366 busyWorkerNodes
: this.workerNodes
.reduce(
367 (accumulator
, workerNode
) =>
368 workerNode
.usage
.tasks
.executing
> 0 ? accumulator
+ 1 : accumulator
,
371 executedTasks
: this.workerNodes
.reduce(
372 (accumulator
, workerNode
) =>
373 accumulator
+ workerNode
.usage
.tasks
.executed
,
376 executingTasks
: this.workerNodes
.reduce(
377 (accumulator
, workerNode
) =>
378 accumulator
+ workerNode
.usage
.tasks
.executing
,
381 ...(this.opts
.enableTasksQueue
=== true && {
382 queuedTasks
: this.workerNodes
.reduce(
383 (accumulator
, workerNode
) =>
384 accumulator
+ workerNode
.usage
.tasks
.queued
,
388 ...(this.opts
.enableTasksQueue
=== true && {
389 maxQueuedTasks
: this.workerNodes
.reduce(
390 (accumulator
, workerNode
) =>
391 accumulator
+ (workerNode
.usage
.tasks
?.maxQueued
?? 0),
395 ...(this.opts
.enableTasksQueue
=== true && {
396 backPressure
: this.hasBackPressure()
398 ...(this.opts
.enableTasksQueue
=== true && {
399 stolenTasks
: this.workerNodes
.reduce(
400 (accumulator
, workerNode
) =>
401 accumulator
+ workerNode
.usage
.tasks
.stolen
,
405 failedTasks
: this.workerNodes
.reduce(
406 (accumulator
, workerNode
) =>
407 accumulator
+ workerNode
.usage
.tasks
.failed
,
410 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
411 .runTime
.aggregate
&& {
415 ...this.workerNodes
.map(
416 workerNode
=> workerNode
.usage
.runTime
?.minimum
?? Infinity
422 ...this.workerNodes
.map(
423 workerNode
=> workerNode
.usage
.runTime
?.maximum
?? -Infinity
427 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
428 .runTime
.average
&& {
431 this.workerNodes
.reduce
<number[]>(
432 (accumulator
, workerNode
) =>
433 accumulator
.concat(workerNode
.usage
.runTime
.history
),
439 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
443 this.workerNodes
.reduce
<number[]>(
444 (accumulator
, workerNode
) =>
445 accumulator
.concat(workerNode
.usage
.runTime
.history
),
453 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
454 .waitTime
.aggregate
&& {
458 ...this.workerNodes
.map(
459 workerNode
=> workerNode
.usage
.waitTime
?.minimum
?? Infinity
465 ...this.workerNodes
.map(
466 workerNode
=> workerNode
.usage
.waitTime
?.maximum
?? -Infinity
470 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
471 .waitTime
.average
&& {
474 this.workerNodes
.reduce
<number[]>(
475 (accumulator
, workerNode
) =>
476 accumulator
.concat(workerNode
.usage
.waitTime
.history
),
482 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
483 .waitTime
.median
&& {
486 this.workerNodes
.reduce
<number[]>(
487 (accumulator
, workerNode
) =>
488 accumulator
.concat(workerNode
.usage
.waitTime
.history
),
500 * The pool readiness boolean status.
502 private get
ready (): boolean {
504 this.workerNodes
.reduce(
505 (accumulator
, workerNode
) =>
506 !workerNode
.info
.dynamic
&& workerNode
.info
.ready
515 * The approximate pool utilization.
517 * @returns The pool utilization.
519 private get
utilization (): number {
520 const poolTimeCapacity
=
521 (performance
.now() - this.startTimestamp
) * this.maxSize
522 const totalTasksRunTime
= this.workerNodes
.reduce(
523 (accumulator
, workerNode
) =>
524 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
527 const totalTasksWaitTime
= this.workerNodes
.reduce(
528 (accumulator
, workerNode
) =>
529 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
532 return (totalTasksRunTime
+ totalTasksWaitTime
) / poolTimeCapacity
538 * If it is `'dynamic'`, it provides the `max` property.
540 protected abstract get
type (): PoolType
545 protected abstract get
worker (): WorkerType
548 * The pool minimum size.
550 protected get
minSize (): number {
551 return this.numberOfWorkers
555 * The pool maximum size.
557 protected get
maxSize (): number {
558 return this.max
?? this.numberOfWorkers
562 * Checks if the worker id sent in the received message from a worker is valid.
564 * @param message - The received message.
565 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
567 private checkMessageWorkerId (message
: MessageValue
<Response
>): void {
568 if (message
.workerId
== null) {
569 throw new Error('Worker message received without worker id')
571 message
.workerId
!= null &&
572 this.getWorkerNodeKeyByWorkerId(message
.workerId
) === -1
575 `Worker message received from unknown worker '${message.workerId}'`
581 * Gets the given worker its worker node key.
583 * @param worker - The worker.
584 * @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
586 private getWorkerNodeKeyByWorker (worker
: Worker
): number {
587 return this.workerNodes
.findIndex(
588 workerNode
=> workerNode
.worker
=== worker
593 * Gets the worker node key given its worker id.
595 * @param workerId - The worker id.
596 * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
598 private getWorkerNodeKeyByWorkerId (workerId
: number | undefined): number {
599 return this.workerNodes
.findIndex(
600 workerNode
=> workerNode
.info
.id
=== workerId
605 public setWorkerChoiceStrategy (
606 workerChoiceStrategy
: WorkerChoiceStrategy
,
607 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
609 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
610 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
611 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
612 this.opts
.workerChoiceStrategy
614 if (workerChoiceStrategyOptions
!= null) {
615 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
617 for (const [workerNodeKey
, workerNode
] of this.workerNodes
.entries()) {
618 workerNode
.resetUsage()
619 this.sendStatisticsMessageToWorker(workerNodeKey
)
624 public setWorkerChoiceStrategyOptions (
625 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
627 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
628 this.opts
.workerChoiceStrategyOptions
= {
629 ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
630 ...workerChoiceStrategyOptions
632 this.workerChoiceStrategyContext
.setOptions(
633 this.opts
.workerChoiceStrategyOptions
638 public enableTasksQueue (
640 tasksQueueOptions
?: TasksQueueOptions
642 if (this.opts
.enableTasksQueue
=== true && !enable
) {
643 this.flushTasksQueues()
645 this.opts
.enableTasksQueue
= enable
646 this.setTasksQueueOptions(tasksQueueOptions
as TasksQueueOptions
)
650 public setTasksQueueOptions (tasksQueueOptions
: TasksQueueOptions
): void {
651 if (this.opts
.enableTasksQueue
=== true) {
652 this.checkValidTasksQueueOptions(tasksQueueOptions
)
653 this.opts
.tasksQueueOptions
=
654 this.buildTasksQueueOptions(tasksQueueOptions
)
655 this.setTasksQueueSize(this.opts
.tasksQueueOptions
.size
as number)
656 } else if (this.opts
.tasksQueueOptions
!= null) {
657 delete this.opts
.tasksQueueOptions
661 private setTasksQueueSize (size
: number): void {
662 for (const workerNode
of this.workerNodes
) {
663 workerNode
.tasksQueueBackPressureSize
= size
667 private buildTasksQueueOptions (
668 tasksQueueOptions
: TasksQueueOptions
669 ): TasksQueueOptions
{
672 size
: Math.pow(this.maxSize
, 2),
675 tasksStealingOnBackPressure
: true
682 * Whether the pool is full or not.
684 * The pool filling boolean status.
686 protected get
full (): boolean {
687 return this.workerNodes
.length
>= this.maxSize
691 * Whether the pool is busy or not.
693 * The pool busyness boolean status.
695 protected abstract get
busy (): boolean
698 * Whether worker nodes are executing concurrently their tasks quota or not.
700 * @returns Worker nodes busyness boolean status.
702 protected internalBusy (): boolean {
703 if (this.opts
.enableTasksQueue
=== true) {
705 this.workerNodes
.findIndex(
707 workerNode
.info
.ready
&&
708 workerNode
.usage
.tasks
.executing
<
709 (this.opts
.tasksQueueOptions
?.concurrency
as number)
714 this.workerNodes
.findIndex(
716 workerNode
.info
.ready
&& workerNode
.usage
.tasks
.executing
=== 0
721 private async sendTaskFunctionOperationToWorker (
722 workerNodeKey
: number,
723 message
: MessageValue
<Data
>
724 ): Promise
<boolean> {
725 const workerId
= this.getWorkerInfo(workerNodeKey
).id
as number
726 return await new Promise
<boolean>((resolve
, reject
) => {
727 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
729 message
.workerId
=== workerId
&&
730 message
.taskFunctionOperationStatus
=== true
734 message
.workerId
=== workerId
&&
735 message
.taskFunctionOperationStatus
=== false
739 `Task function operation ${
740 message.taskFunctionOperation as string
741 } failed on worker ${message.workerId}`
746 this.sendToWorker(workerNodeKey
, message
)
750 private async sendTaskFunctionOperationToWorkers (
751 message
: Omit
<MessageValue
<Data
>, 'workerId'>
752 ): Promise
<boolean> {
753 return await new Promise
<boolean>((resolve
, reject
) => {
754 const responsesReceived
= new Array<MessageValue
<Data
| Response
>>()
755 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
756 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
757 if (message
.taskFunctionOperationStatus
!= null) {
758 responsesReceived
.push(message
)
760 responsesReceived
.length
=== this.workerNodes
.length
&&
761 responsesReceived
.every(
762 message
=> message
.taskFunctionOperationStatus
=== true
767 responsesReceived
.length
=== this.workerNodes
.length
&&
768 responsesReceived
.some(
769 message
=> message
.taskFunctionOperationStatus
=== false
774 `Task function operation ${
775 message.taskFunctionOperation as string
776 } failed on worker ${message.workerId as number}`
782 this.sendToWorker(workerNodeKey
, message
)
788 public hasTaskFunction (name
: string): boolean {
789 for (const workerNode
of this.workerNodes
) {
791 Array.isArray(workerNode
.info
.taskFunctionNames
) &&
792 workerNode
.info
.taskFunctionNames
.includes(name
)
801 public async addTaskFunction (
803 taskFunction
: TaskFunction
<Data
, Response
>
804 ): Promise
<boolean> {
805 this.taskFunctions
.set(name
, taskFunction
)
806 return await this.sendTaskFunctionOperationToWorkers({
807 taskFunctionOperation
: 'add',
808 taskFunctionName
: name
,
809 taskFunction
: taskFunction
.toString()
814 public async removeTaskFunction (name
: string): Promise
<boolean> {
815 this.taskFunctions
.delete(name
)
816 return await this.sendTaskFunctionOperationToWorkers({
817 taskFunctionOperation
: 'remove',
818 taskFunctionName
: name
823 public listTaskFunctionNames (): string[] {
824 for (const workerNode
of this.workerNodes
) {
826 Array.isArray(workerNode
.info
.taskFunctionNames
) &&
827 workerNode
.info
.taskFunctionNames
.length
> 0
829 return workerNode
.info
.taskFunctionNames
836 public async setDefaultTaskFunction (name
: string): Promise
<boolean> {
837 return await this.sendTaskFunctionOperationToWorkers({
838 taskFunctionOperation
: 'default',
839 taskFunctionName
: name
843 private shallExecuteTask (workerNodeKey
: number): boolean {
845 this.tasksQueueSize(workerNodeKey
) === 0 &&
846 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
847 (this.opts
.tasksQueueOptions
?.concurrency
as number)
852 public async execute (
855 transferList
?: TransferListItem
[]
856 ): Promise
<Response
> {
857 return await new Promise
<Response
>((resolve
, reject
) => {
859 reject(new Error('Cannot execute a task on not started pool'))
862 if (name
!= null && typeof name
!== 'string') {
863 reject(new TypeError('name argument must be a string'))
868 typeof name
=== 'string' &&
869 name
.trim().length
=== 0
871 reject(new TypeError('name argument must not be an empty string'))
874 if (transferList
!= null && !Array.isArray(transferList
)) {
875 reject(new TypeError('transferList argument must be an array'))
878 const timestamp
= performance
.now()
879 const workerNodeKey
= this.chooseWorkerNode()
880 const task
: Task
<Data
> = {
881 name
: name
?? DEFAULT_TASK_NAME
,
882 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
883 data
: data
?? ({} as Data
),
888 this.promiseResponseMap
.set(task
.taskId
as string, {
894 this.opts
.enableTasksQueue
=== false ||
895 (this.opts
.enableTasksQueue
=== true &&
896 this.shallExecuteTask(workerNodeKey
))
898 this.executeTask(workerNodeKey
, task
)
900 this.enqueueTask(workerNodeKey
, task
)
906 public start (): void {
909 this.workerNodes
.reduce(
910 (accumulator
, workerNode
) =>
911 !workerNode
.info
.dynamic
? accumulator
+ 1 : accumulator
,
913 ) < this.numberOfWorkers
915 this.createAndSetupWorkerNode()
917 this.starting
= false
922 public async destroy (): Promise
<void> {
924 this.workerNodes
.map(async (_
, workerNodeKey
) => {
925 await this.destroyWorkerNode(workerNodeKey
)
928 this.emitter
?.emit(PoolEvents
.destroy
, this.info
)
932 protected async sendKillMessageToWorker (
933 workerNodeKey
: number
935 await new Promise
<void>((resolve
, reject
) => {
936 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
937 if (message
.kill
=== 'success') {
939 } else if (message
.kill
=== 'failure') {
943 message.workerId as number
944 } kill message handling failed`
949 this.sendToWorker(workerNodeKey
, { kill
: true })
954 * Terminates the worker node given its worker node key.
956 * @param workerNodeKey - The worker node key.
958 protected abstract destroyWorkerNode (workerNodeKey
: number): Promise
<void>
961 * Setup hook to execute code before worker nodes are created in the abstract constructor.
966 protected setupHook (): void {
967 /* Intentionally empty */
971 * Should return whether the worker is the main worker or not.
973 protected abstract isMain (): boolean
976 * Hook executed before the worker task execution.
979 * @param workerNodeKey - The worker node key.
980 * @param task - The task to execute.
982 protected beforeTaskExecutionHook (
983 workerNodeKey
: number,
986 if (this.workerNodes
[workerNodeKey
]?.usage
!= null) {
987 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
988 ++workerUsage
.tasks
.executing
989 this.updateWaitTimeWorkerUsage(workerUsage
, task
)
992 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
993 this.workerNodes
[workerNodeKey
].getTaskFunctionWorkerUsage(
997 const taskFunctionWorkerUsage
= this.workerNodes
[
999 ].getTaskFunctionWorkerUsage(task
.name
as string) as WorkerUsage
1000 ++taskFunctionWorkerUsage
.tasks
.executing
1001 this.updateWaitTimeWorkerUsage(taskFunctionWorkerUsage
, task
)
1006 * Hook executed after the worker task execution.
1007 * Can be overridden.
1009 * @param workerNodeKey - The worker node key.
1010 * @param message - The received message.
1012 protected afterTaskExecutionHook (
1013 workerNodeKey
: number,
1014 message
: MessageValue
<Response
>
1016 if (this.workerNodes
[workerNodeKey
]?.usage
!= null) {
1017 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
1018 this.updateTaskStatisticsWorkerUsage(workerUsage
, message
)
1019 this.updateRunTimeWorkerUsage(workerUsage
, message
)
1020 this.updateEluWorkerUsage(workerUsage
, message
)
1023 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
1024 this.workerNodes
[workerNodeKey
].getTaskFunctionWorkerUsage(
1025 message
.taskPerformance
?.name
as string
1028 const taskFunctionWorkerUsage
= this.workerNodes
[
1030 ].getTaskFunctionWorkerUsage(
1031 message
.taskPerformance
?.name
as string
1033 this.updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage
, message
)
1034 this.updateRunTimeWorkerUsage(taskFunctionWorkerUsage
, message
)
1035 this.updateEluWorkerUsage(taskFunctionWorkerUsage
, message
)
1040 * Whether the worker node shall update its task function worker usage or not.
1042 * @param workerNodeKey - The worker node key.
1043 * @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
1045 private shallUpdateTaskFunctionWorkerUsage (workerNodeKey
: number): boolean {
1046 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
1048 workerInfo
!= null &&
1049 Array.isArray(workerInfo
.taskFunctionNames
) &&
1050 workerInfo
.taskFunctionNames
.length
> 2
1054 private updateTaskStatisticsWorkerUsage (
1055 workerUsage
: WorkerUsage
,
1056 message
: MessageValue
<Response
>
1058 const workerTaskStatistics
= workerUsage
.tasks
1060 workerTaskStatistics
.executing
!= null &&
1061 workerTaskStatistics
.executing
> 0
1063 --workerTaskStatistics
.executing
1065 if (message
.workerError
== null) {
1066 ++workerTaskStatistics
.executed
1068 ++workerTaskStatistics
.failed
1072 private updateRunTimeWorkerUsage (
1073 workerUsage
: WorkerUsage
,
1074 message
: MessageValue
<Response
>
1076 if (message
.workerError
!= null) {
1079 updateMeasurementStatistics(
1080 workerUsage
.runTime
,
1081 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
,
1082 message
.taskPerformance
?.runTime
?? 0
1086 private updateWaitTimeWorkerUsage (
1087 workerUsage
: WorkerUsage
,
1090 const timestamp
= performance
.now()
1091 const taskWaitTime
= timestamp
- (task
.timestamp
?? timestamp
)
1092 updateMeasurementStatistics(
1093 workerUsage
.waitTime
,
1094 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().waitTime
,
1099 private updateEluWorkerUsage (
1100 workerUsage
: WorkerUsage
,
1101 message
: MessageValue
<Response
>
1103 if (message
.workerError
!= null) {
1106 const eluTaskStatisticsRequirements
: MeasurementStatisticsRequirements
=
1107 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().elu
1108 updateMeasurementStatistics(
1109 workerUsage
.elu
.active
,
1110 eluTaskStatisticsRequirements
,
1111 message
.taskPerformance
?.elu
?.active
?? 0
1113 updateMeasurementStatistics(
1114 workerUsage
.elu
.idle
,
1115 eluTaskStatisticsRequirements
,
1116 message
.taskPerformance
?.elu
?.idle
?? 0
1118 if (eluTaskStatisticsRequirements
.aggregate
) {
1119 if (message
.taskPerformance
?.elu
!= null) {
1120 if (workerUsage
.elu
.utilization
!= null) {
1121 workerUsage
.elu
.utilization
=
1122 (workerUsage
.elu
.utilization
+
1123 message
.taskPerformance
.elu
.utilization
) /
1126 workerUsage
.elu
.utilization
= message
.taskPerformance
.elu
.utilization
1133 * Chooses a worker node for the next task.
1135 * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
1137 * @returns The chosen worker node key
1139 private chooseWorkerNode (): number {
1140 if (this.shallCreateDynamicWorker()) {
1141 const workerNodeKey
= this.createAndSetupDynamicWorkerNode()
1143 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerUsage
1145 return workerNodeKey
1148 return this.workerChoiceStrategyContext
.execute()
1152 * Conditions for dynamic worker creation.
1154 * @returns Whether to create a dynamic worker or not.
1156 private shallCreateDynamicWorker (): boolean {
1157 return this.type === PoolTypes
.dynamic
&& !this.full
&& this.internalBusy()
1161 * Sends a message to worker given its worker node key.
1163 * @param workerNodeKey - The worker node key.
1164 * @param message - The message.
1165 * @param transferList - The optional array of transferable objects.
1167 protected abstract sendToWorker (
1168 workerNodeKey
: number,
1169 message
: MessageValue
<Data
>,
1170 transferList
?: TransferListItem
[]
1174 * Creates a new worker.
1176 * @returns Newly created worker.
1178 protected abstract createWorker (): Worker
1181 * Creates a new, completely set up worker node.
1183 * @returns New, completely set up worker node key.
1185 protected createAndSetupWorkerNode (): number {
1186 const worker
= this.createWorker()
1188 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
1189 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
1190 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
1191 worker
.on('error', error
=> {
1192 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1193 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
1194 workerInfo
.ready
= false
1195 this.workerNodes
[workerNodeKey
].closeChannel()
1196 this.emitter
?.emit(PoolEvents
.error
, error
)
1198 this.opts
.restartWorkerOnError
=== true &&
1202 if (workerInfo
.dynamic
) {
1203 this.createAndSetupDynamicWorkerNode()
1205 this.createAndSetupWorkerNode()
1208 if (this.opts
.enableTasksQueue
=== true) {
1209 this.redistributeQueuedTasks(workerNodeKey
)
1212 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
1213 worker
.once('exit', () => {
1214 this.removeWorkerNode(worker
)
1217 const workerNodeKey
= this.addWorkerNode(worker
)
1219 this.afterWorkerNodeSetup(workerNodeKey
)
1221 return workerNodeKey
1225 * Creates a new, completely set up dynamic worker node.
1227 * @returns New, completely set up dynamic worker node key.
1229 protected createAndSetupDynamicWorkerNode (): number {
1230 const workerNodeKey
= this.createAndSetupWorkerNode()
1231 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
1232 const localWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(
1235 const workerUsage
= this.workerNodes
[localWorkerNodeKey
].usage
1236 // Kill message received from worker
1238 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
1239 (isKillBehavior(KillBehaviors
.SOFT
, message
.kill
) &&
1240 ((this.opts
.enableTasksQueue
=== false &&
1241 workerUsage
.tasks
.executing
=== 0) ||
1242 (this.opts
.enableTasksQueue
=== true &&
1243 workerUsage
.tasks
.executing
=== 0 &&
1244 this.tasksQueueSize(localWorkerNodeKey
) === 0)))
1246 this.destroyWorkerNode(localWorkerNodeKey
).catch(error
=> {
1247 this.emitter
?.emit(PoolEvents
.error
, error
)
1251 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
1252 this.sendToWorker(workerNodeKey
, {
1255 if (this.taskFunctions
.size
> 0) {
1256 for (const [taskFunctionName
, taskFunction
] of this.taskFunctions
) {
1257 this.sendTaskFunctionOperationToWorker(workerNodeKey
, {
1258 taskFunctionOperation
: 'add',
1260 taskFunction
: taskFunction
.toString()
1262 this.emitter
?.emit(PoolEvents
.error
, error
)
1266 workerInfo
.dynamic
= true
1268 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerReady
||
1269 this.workerChoiceStrategyContext
.getStrategyPolicy().dynamicWorkerUsage
1271 workerInfo
.ready
= true
1273 this.checkAndEmitDynamicWorkerCreationEvents()
1274 return workerNodeKey
1278 * Registers a listener callback on the worker given its worker node key.
1280 * @param workerNodeKey - The worker node key.
1281 * @param listener - The message listener callback.
1283 protected abstract registerWorkerMessageListener
<
1284 Message
extends Data
| Response
1286 workerNodeKey
: number,
1287 listener
: (message
: MessageValue
<Message
>) => void
1291 * Method hooked up after a worker node has been newly created.
1292 * Can be overridden.
1294 * @param workerNodeKey - The newly created worker node key.
1296 protected afterWorkerNodeSetup (workerNodeKey
: number): void {
1297 // Listen to worker messages.
1298 this.registerWorkerMessageListener(workerNodeKey
, this.workerListener())
1299 // Send the startup message to worker.
1300 this.sendStartupMessageToWorker(workerNodeKey
)
1301 // Send the statistics message to worker.
1302 this.sendStatisticsMessageToWorker(workerNodeKey
)
1303 if (this.opts
.enableTasksQueue
=== true) {
1304 if (this.opts
.tasksQueueOptions
?.taskStealing
=== true) {
1305 this.workerNodes
[workerNodeKey
].onEmptyQueue
=
1306 this.taskStealingOnEmptyQueue
.bind(this)
1308 if (this.opts
.tasksQueueOptions
?.tasksStealingOnBackPressure
=== true) {
1309 this.workerNodes
[workerNodeKey
].onBackPressure
=
1310 this.tasksStealingOnBackPressure
.bind(this)
1316 * Sends the startup message to worker given its worker node key.
1318 * @param workerNodeKey - The worker node key.
1320 protected abstract sendStartupMessageToWorker (workerNodeKey
: number): void
1323 * Sends the statistics message to worker given its worker node key.
1325 * @param workerNodeKey - The worker node key.
1327 private sendStatisticsMessageToWorker (workerNodeKey
: number): void {
1328 this.sendToWorker(workerNodeKey
, {
1331 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1333 elu
: this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
1339 private redistributeQueuedTasks (workerNodeKey
: number): void {
1340 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1341 const destinationWorkerNodeKey
= this.workerNodes
.reduce(
1342 (minWorkerNodeKey
, workerNode
, workerNodeKey
, workerNodes
) => {
1343 return workerNode
.info
.ready
&&
1344 workerNode
.usage
.tasks
.queued
<
1345 workerNodes
[minWorkerNodeKey
].usage
.tasks
.queued
1351 const task
= this.dequeueTask(workerNodeKey
) as Task
<Data
>
1352 if (this.shallExecuteTask(destinationWorkerNodeKey
)) {
1353 this.executeTask(destinationWorkerNodeKey
, task
)
1355 this.enqueueTask(destinationWorkerNodeKey
, task
)
1360 private updateTaskStolenStatisticsWorkerUsage (
1361 workerNodeKey
: number,
1364 const workerNode
= this.workerNodes
[workerNodeKey
]
1365 if (workerNode
?.usage
!= null) {
1366 ++workerNode
.usage
.tasks
.stolen
1369 this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey
) &&
1370 workerNode
.getTaskFunctionWorkerUsage(taskName
) != null
1372 const taskFunctionWorkerUsage
= workerNode
.getTaskFunctionWorkerUsage(
1375 ++taskFunctionWorkerUsage
.tasks
.stolen
1379 private taskStealingOnEmptyQueue (workerId
: number): void {
1380 const destinationWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(workerId
)
1381 const workerNodes
= this.workerNodes
1384 (workerNodeA
, workerNodeB
) =>
1385 workerNodeB
.usage
.tasks
.queued
- workerNodeA
.usage
.tasks
.queued
1387 const sourceWorkerNode
= workerNodes
.find(
1389 workerNode
.info
.ready
&&
1390 workerNode
.info
.id
!== workerId
&&
1391 workerNode
.usage
.tasks
.queued
> 0
1393 if (sourceWorkerNode
!= null) {
1394 const task
= sourceWorkerNode
.popTask() as Task
<Data
>
1395 if (this.shallExecuteTask(destinationWorkerNodeKey
)) {
1396 this.executeTask(destinationWorkerNodeKey
, task
)
1398 this.enqueueTask(destinationWorkerNodeKey
, task
)
1400 this.updateTaskStolenStatisticsWorkerUsage(
1401 destinationWorkerNodeKey
,
1407 private tasksStealingOnBackPressure (workerId
: number): void {
1408 const sizeOffset
= 1
1409 if ((this.opts
.tasksQueueOptions
?.size
as number) <= sizeOffset
) {
1412 const sourceWorkerNode
=
1413 this.workerNodes
[this.getWorkerNodeKeyByWorkerId(workerId
)]
1414 const workerNodes
= this.workerNodes
1417 (workerNodeA
, workerNodeB
) =>
1418 workerNodeA
.usage
.tasks
.queued
- workerNodeB
.usage
.tasks
.queued
1420 for (const [workerNodeKey
, workerNode
] of workerNodes
.entries()) {
1422 sourceWorkerNode
.usage
.tasks
.queued
> 0 &&
1423 workerNode
.info
.ready
&&
1424 workerNode
.info
.id
!== workerId
&&
1425 workerNode
.usage
.tasks
.queued
<
1426 (this.opts
.tasksQueueOptions
?.size
as number) - sizeOffset
1428 const task
= sourceWorkerNode
.popTask() as Task
<Data
>
1429 if (this.shallExecuteTask(workerNodeKey
)) {
1430 this.executeTask(workerNodeKey
, task
)
1432 this.enqueueTask(workerNodeKey
, task
)
1434 this.updateTaskStolenStatisticsWorkerUsage(
1443 * This method is the listener registered for each worker message.
1445 * @returns The listener function to execute when a message is received from a worker.
1447 protected workerListener (): (message
: MessageValue
<Response
>) => void {
1449 this.checkMessageWorkerId(message
)
1450 if (message
.ready
!= null && message
.taskFunctionNames
!= null) {
1451 // Worker ready response received from worker
1452 this.handleWorkerReadyResponse(message
)
1453 } else if (message
.taskId
!= null) {
1454 // Task execution response received from worker
1455 this.handleTaskExecutionResponse(message
)
1456 } else if (message
.taskFunctionNames
!= null) {
1457 // Task function names message received from worker
1459 this.getWorkerNodeKeyByWorkerId(message
.workerId
)
1460 ).taskFunctionNames
= message
.taskFunctionNames
1465 private handleWorkerReadyResponse (message
: MessageValue
<Response
>): void {
1466 if (message
.ready
=== false) {
1468 `Worker ${message.workerId as number} failed to initialize`
1471 const workerInfo
= this.getWorkerInfo(
1472 this.getWorkerNodeKeyByWorkerId(message
.workerId
)
1474 workerInfo
.ready
= message
.ready
as boolean
1475 workerInfo
.taskFunctionNames
= message
.taskFunctionNames
1476 if (this.emitter
!= null && this.ready
) {
1477 this.emitter
.emit(PoolEvents
.ready
, this.info
)
1481 private handleTaskExecutionResponse (message
: MessageValue
<Response
>): void {
1482 const { taskId
, workerError
, data
} = message
1483 const promiseResponse
= this.promiseResponseMap
.get(taskId
as string)
1484 if (promiseResponse
!= null) {
1485 if (workerError
!= null) {
1486 this.emitter
?.emit(PoolEvents
.taskError
, workerError
)
1487 promiseResponse
.reject(workerError
.message
)
1489 promiseResponse
.resolve(data
as Response
)
1491 const workerNodeKey
= promiseResponse
.workerNodeKey
1492 this.afterTaskExecutionHook(workerNodeKey
, message
)
1493 this.workerChoiceStrategyContext
.update(workerNodeKey
)
1494 this.promiseResponseMap
.delete(taskId
as string)
1496 this.opts
.enableTasksQueue
=== true &&
1497 this.tasksQueueSize(workerNodeKey
) > 0 &&
1498 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
1499 (this.opts
.tasksQueueOptions
?.concurrency
as number)
1503 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1509 private checkAndEmitTaskExecutionEvents (): void {
1511 this.emitter
?.emit(PoolEvents
.busy
, this.info
)
1515 private checkAndEmitTaskQueuingEvents (): void {
1516 if (this.hasBackPressure()) {
1517 this.emitter
?.emit(PoolEvents
.backPressure
, this.info
)
1521 private checkAndEmitDynamicWorkerCreationEvents (): void {
1522 if (this.type === PoolTypes
.dynamic
) {
1524 this.emitter
?.emit(PoolEvents
.full
, this.info
)
1530 * Gets the worker information given its worker node key.
1532 * @param workerNodeKey - The worker node key.
1533 * @returns The worker information.
1535 protected getWorkerInfo (workerNodeKey
: number): WorkerInfo
{
1536 return this.workerNodes
[workerNodeKey
].info
1540 * Adds the given worker in the pool worker nodes.
1542 * @param worker - The worker.
1543 * @returns The added worker node key.
1544 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1546 private addWorkerNode (worker
: Worker
): number {
1547 const workerNode
= new WorkerNode
<Worker
, Data
>(
1549 this.opts
.tasksQueueOptions
?.size
?? Math.pow(this.maxSize
, 2)
1551 // Flag the worker node as ready at pool startup.
1552 if (this.starting
) {
1553 workerNode
.info
.ready
= true
1555 this.workerNodes
.push(workerNode
)
1556 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1557 if (workerNodeKey
=== -1) {
1558 throw new Error('Worker added not found in worker nodes')
1560 return workerNodeKey
1564 * Removes the given worker from the pool worker nodes.
1566 * @param worker - The worker.
1568 private removeWorkerNode (worker
: Worker
): void {
1569 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1570 if (workerNodeKey
!== -1) {
1571 this.workerNodes
.splice(workerNodeKey
, 1)
1572 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
1577 public hasWorkerNodeBackPressure (workerNodeKey
: number): boolean {
1579 this.opts
.enableTasksQueue
=== true &&
1580 this.workerNodes
[workerNodeKey
].hasBackPressure()
1584 private hasBackPressure (): boolean {
1586 this.opts
.enableTasksQueue
=== true &&
1587 this.workerNodes
.findIndex(
1588 workerNode
=> !workerNode
.hasBackPressure()
1594 * Executes the given task on the worker given its worker node key.
1596 * @param workerNodeKey - The worker node key.
1597 * @param task - The task to execute.
1599 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
1600 this.beforeTaskExecutionHook(workerNodeKey
, task
)
1601 this.sendToWorker(workerNodeKey
, task
, task
.transferList
)
1602 this.checkAndEmitTaskExecutionEvents()
1605 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
1606 const tasksQueueSize
= this.workerNodes
[workerNodeKey
].enqueueTask(task
)
1607 this.checkAndEmitTaskQueuingEvents()
1608 return tasksQueueSize
1611 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
1612 return this.workerNodes
[workerNodeKey
].dequeueTask()
1615 private tasksQueueSize (workerNodeKey
: number): number {
1616 return this.workerNodes
[workerNodeKey
].tasksQueueSize()
1619 protected flushTasksQueue (workerNodeKey
: number): void {
1620 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1623 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1626 this.workerNodes
[workerNodeKey
].clearTasksQueue()
1629 private flushTasksQueues (): void {
1630 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
1631 this.flushTasksQueue(workerNodeKey
)