1 import crypto from
'node:crypto'
2 import type { MessageValue
, PromiseResponseWrapper
} from
'../utility-types'
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
9 import { KillBehaviors
, isKillBehavior
} from
'../worker/worker-options'
10 import { CircularArray
} from
'../circular-array'
11 import { Queue
} from
'../queue'
18 type TasksQueueOptions
20 import type { IWorker
, Task
, TasksUsage
, WorkerNode
} from
'./worker'
22 WorkerChoiceStrategies
,
23 type WorkerChoiceStrategy
,
24 type WorkerChoiceStrategyOptions
25 } from
'./selection-strategies/selection-strategies-types'
26 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
29 * Base class that implements some shared logic for all poolifier pools.
31 * @typeParam Worker - Type of worker which manages this pool.
32 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
33 * @typeParam Response - Type of execution response. This can only be serializable data.
35 export abstract class AbstractPool
<
36 Worker
extends IWorker
,
39 > implements IPool
<Worker
, Data
, Response
> {
41 public readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>> = []
44 public readonly emitter
?: PoolEmitter
47 * The execution response promise map.
49 * - `key`: The message id of each submitted task.
50 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
52 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
54 protected promiseResponseMap
: Map
<
56 PromiseResponseWrapper
<Worker
, Response
>
57 > = new Map
<string, PromiseResponseWrapper
<Worker
, Response
>>()
60 * Worker choice strategy context referencing a worker choice algorithm implementation.
62 * Default to a round robin algorithm.
64 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
71 * Constructs a new poolifier pool.
73 * @param numberOfWorkers - Number of workers that this pool should manage.
74 * @param filePath - Path to the worker file.
75 * @param opts - Options for the pool.
78 public readonly numberOfWorkers
: number,
79 public readonly filePath
: string,
80 public readonly opts
: PoolOptions
<Worker
>
83 throw new Error('Cannot start a pool from a worker!')
85 this.checkNumberOfWorkers(this.numberOfWorkers
)
86 this.checkFilePath(this.filePath
)
87 this.checkPoolOptions(this.opts
)
89 this.chooseWorkerNode
= this.chooseWorkerNode
.bind(this)
90 this.executeTask
= this.executeTask
.bind(this)
91 this.enqueueTask
= this.enqueueTask
.bind(this)
92 this.checkAndEmitEvents
= this.checkAndEmitEvents
.bind(this)
96 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
97 this.createAndSetupWorker()
100 if (this.opts
.enableEvents
=== true) {
101 this.emitter
= new PoolEmitter()
103 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
109 this.opts
.workerChoiceStrategy
,
110 this.opts
.workerChoiceStrategyOptions
114 private checkFilePath (filePath
: string): void {
117 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
119 throw new Error('Please specify a file with a worker implementation')
123 private checkNumberOfWorkers (numberOfWorkers
: number): void {
124 if (numberOfWorkers
== null) {
126 'Cannot instantiate a pool without specifying the number of workers'
128 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
130 'Cannot instantiate a pool with a non safe integer number of workers'
132 } else if (numberOfWorkers
< 0) {
133 throw new RangeError(
134 'Cannot instantiate a pool with a negative number of workers'
136 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
137 throw new Error('Cannot instantiate a fixed pool with no worker')
141 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
142 if (isPlainObject(opts
)) {
143 this.opts
.workerChoiceStrategy
=
144 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
145 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
146 this.opts
.workerChoiceStrategyOptions
=
147 opts
.workerChoiceStrategyOptions
??
148 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
149 this.opts
.enableEvents
= opts
.enableEvents
?? true
150 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
151 if (this.opts
.enableTasksQueue
) {
152 this.checkValidTasksQueueOptions(
153 opts
.tasksQueueOptions
as TasksQueueOptions
155 this.opts
.tasksQueueOptions
= this.buildTasksQueueOptions(
156 opts
.tasksQueueOptions
as TasksQueueOptions
160 throw new TypeError('Invalid pool options: must be a plain object')
164 private checkValidWorkerChoiceStrategy (
165 workerChoiceStrategy
: WorkerChoiceStrategy
167 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
169 `Invalid worker choice strategy '${workerChoiceStrategy}'`
174 private checkValidWorkerChoiceStrategyOptions (
175 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
177 if (!isPlainObject(workerChoiceStrategyOptions
)) {
179 'Invalid worker choice strategy options: must be a plain object'
184 private checkValidTasksQueueOptions (
185 tasksQueueOptions
: TasksQueueOptions
187 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
188 throw new TypeError('Invalid tasks queue options: must be a plain object')
190 if ((tasksQueueOptions
?.concurrency
as number) <= 0) {
192 `Invalid worker tasks concurrency '${
193 tasksQueueOptions.concurrency as number
200 public abstract get
type (): PoolType
203 public abstract get
size (): number
206 * Number of tasks running in the pool.
208 private get
numberOfRunningTasks (): number {
209 return this.workerNodes
.reduce(
210 (accumulator
, workerNode
) => accumulator
+ workerNode
.tasksUsage
.running
,
216 * Number of tasks queued in the pool.
218 private get
numberOfQueuedTasks (): number {
219 if (this.opts
.enableTasksQueue
=== false) {
222 return this.workerNodes
.reduce(
223 (accumulator
, workerNode
) => accumulator
+ workerNode
.tasksQueue
.size
,
229 * Gets the given worker its worker node key.
231 * @param worker - The worker.
232 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
234 private getWorkerNodeKey (worker
: Worker
): number {
235 return this.workerNodes
.findIndex(
236 workerNode
=> workerNode
.worker
=== worker
241 public setWorkerChoiceStrategy (
242 workerChoiceStrategy
: WorkerChoiceStrategy
,
243 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
245 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
246 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
247 for (const workerNode
of this.workerNodes
) {
248 this.setWorkerNodeTasksUsage(workerNode
, {
252 runTimeHistory
: new CircularArray(),
258 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
259 this.opts
.workerChoiceStrategy
261 if (workerChoiceStrategyOptions
!= null) {
262 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
267 public setWorkerChoiceStrategyOptions (
268 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
270 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
271 this.opts
.workerChoiceStrategyOptions
= workerChoiceStrategyOptions
272 this.workerChoiceStrategyContext
.setOptions(
273 this.opts
.workerChoiceStrategyOptions
278 public enableTasksQueue (
280 tasksQueueOptions
?: TasksQueueOptions
282 if (this.opts
.enableTasksQueue
=== true && !enable
) {
283 this.flushTasksQueues()
285 this.opts
.enableTasksQueue
= enable
286 this.setTasksQueueOptions(tasksQueueOptions
as TasksQueueOptions
)
290 public setTasksQueueOptions (tasksQueueOptions
: TasksQueueOptions
): void {
291 if (this.opts
.enableTasksQueue
=== true) {
292 this.checkValidTasksQueueOptions(tasksQueueOptions
)
293 this.opts
.tasksQueueOptions
=
294 this.buildTasksQueueOptions(tasksQueueOptions
)
296 delete this.opts
.tasksQueueOptions
300 private buildTasksQueueOptions (
301 tasksQueueOptions
: TasksQueueOptions
302 ): TasksQueueOptions
{
304 concurrency
: tasksQueueOptions
?.concurrency
?? 1
309 * Whether the pool is full or not.
311 * The pool filling boolean status.
313 protected abstract get
full (): boolean
316 * Whether the pool is busy or not.
318 * The pool busyness boolean status.
320 protected abstract get
busy (): boolean
322 protected internalBusy (): boolean {
324 this.workerNodes
.findIndex(workerNode
=> {
325 return workerNode
.tasksUsage
?.running
=== 0
331 public async execute (data
?: Data
, name
?: string): Promise
<Response
> {
332 const [workerNodeKey
, workerNode
] = this.chooseWorkerNode()
333 const submittedTask
: Task
<Data
> = {
335 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
336 data
: data
?? ({} as Data
),
337 id
: crypto
.randomUUID()
339 const res
= new Promise
<Response
>((resolve
, reject
) => {
340 this.promiseResponseMap
.set(submittedTask
.id
as string, {
343 worker
: workerNode
.worker
347 this.opts
.enableTasksQueue
=== true &&
349 this.workerNodes
[workerNodeKey
].tasksUsage
.running
>=
350 ((this.opts
.tasksQueueOptions
as TasksQueueOptions
)
351 .concurrency
as number))
353 this.enqueueTask(workerNodeKey
, submittedTask
)
355 this.executeTask(workerNodeKey
, submittedTask
)
357 this.checkAndEmitEvents()
358 // eslint-disable-next-line @typescript-eslint/return-await
363 public async destroy (): Promise
<void> {
365 this.workerNodes
.map(async (workerNode
, workerNodeKey
) => {
366 this.flushTasksQueue(workerNodeKey
)
367 await this.destroyWorker(workerNode
.worker
)
373 * Shutdowns the given worker.
375 * @param worker - A worker within `workerNodes`.
377 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
380 * Setup hook to execute code before worker node are created in the abstract constructor.
385 protected setupHook (): void {
386 // Intentionally empty
390 * Should return whether the worker is the main worker or not.
392 protected abstract isMain (): boolean
395 * Hook executed before the worker task execution.
398 * @param workerNodeKey - The worker node key.
400 protected beforeTaskExecutionHook (workerNodeKey
: number): void {
401 ++this.workerNodes
[workerNodeKey
].tasksUsage
.running
405 * Hook executed after the worker task execution.
408 * @param worker - The worker.
409 * @param message - The received message.
411 protected afterTaskExecutionHook (
413 message
: MessageValue
<Response
>
415 const workerTasksUsage
= this.getWorkerTasksUsage(worker
)
416 --workerTasksUsage
.running
417 ++workerTasksUsage
.run
418 if (message
.error
!= null) {
419 ++workerTasksUsage
.error
421 if (this.workerChoiceStrategyContext
.getRequiredStatistics().runTime
) {
422 workerTasksUsage
.runTime
+= message
.runTime
?? 0
424 this.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
&&
425 workerTasksUsage
.run
!== 0
427 workerTasksUsage
.avgRunTime
=
428 workerTasksUsage
.runTime
/ workerTasksUsage
.run
430 if (this.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
) {
431 workerTasksUsage
.runTimeHistory
.push(message
.runTime
?? 0)
432 workerTasksUsage
.medRunTime
= median(workerTasksUsage
.runTimeHistory
)
438 * Chooses a worker node for the next task.
440 * The default uses a round robin algorithm to distribute the load.
442 * @returns [worker node key, worker node].
444 protected chooseWorkerNode (): [number, WorkerNode
<Worker
, Data
>] {
445 let workerNodeKey
: number
446 if (this.type === PoolType
.DYNAMIC
&& !this.full
&& this.internalBusy()) {
447 const workerCreated
= this.createAndSetupWorker()
448 this.registerWorkerMessageListener(workerCreated
, message
=> {
450 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
451 (message
.kill
!= null &&
452 this.getWorkerTasksUsage(workerCreated
)?.running
=== 0)
454 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
455 this.flushTasksQueueByWorker(workerCreated
)
456 void (this.destroyWorker(workerCreated
) as Promise
<void>)
459 workerNodeKey
= this.getWorkerNodeKey(workerCreated
)
461 workerNodeKey
= this.workerChoiceStrategyContext
.execute()
463 return [workerNodeKey
, this.workerNodes
[workerNodeKey
]]
467 * Sends a message to the given worker.
469 * @param worker - The worker which should receive the message.
470 * @param message - The message.
472 protected abstract sendToWorker (
474 message
: MessageValue
<Data
>
478 * Registers a listener callback on the given worker.
480 * @param worker - The worker which should register a listener.
481 * @param listener - The message listener callback.
483 protected abstract registerWorkerMessageListener
<
484 Message
extends Data
| Response
485 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
488 * Returns a newly created worker.
490 protected abstract createWorker (): Worker
493 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
495 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
497 * @param worker - The newly created worker.
499 protected abstract afterWorkerSetup (worker
: Worker
): void
502 * Creates a new worker and sets it up completely in the pool worker nodes.
504 * @returns New, completely set up worker.
506 protected createAndSetupWorker (): Worker
{
507 const worker
= this.createWorker()
509 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
510 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
511 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
512 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
513 worker
.once('exit', () => {
514 this.removeWorkerNode(worker
)
517 this.pushWorkerNode(worker
)
519 this.afterWorkerSetup(worker
)
525 * This function is the listener registered for each worker message.
527 * @returns The listener function to execute when a message is received from a worker.
529 protected workerListener (): (message
: MessageValue
<Response
>) => void {
531 if (message
.id
!= null) {
532 // Task execution response received
533 const promiseResponse
= this.promiseResponseMap
.get(message
.id
)
534 if (promiseResponse
!= null) {
535 if (message
.error
!= null) {
536 promiseResponse
.reject(message
.error
)
538 promiseResponse
.resolve(message
.data
as Response
)
540 this.afterTaskExecutionHook(promiseResponse
.worker
, message
)
541 this.promiseResponseMap
.delete(message
.id
)
542 const workerNodeKey
= this.getWorkerNodeKey(promiseResponse
.worker
)
544 this.opts
.enableTasksQueue
=== true &&
545 this.tasksQueueSize(workerNodeKey
) > 0
549 this.dequeueTask(workerNodeKey
) as Task
<Data
>
557 private checkAndEmitEvents (): void {
558 if (this.opts
.enableEvents
=== true) {
560 this.emitter
?.emit(PoolEvents
.busy
)
562 if (this.type === PoolType
.DYNAMIC
&& this.full
) {
563 this.emitter
?.emit(PoolEvents
.full
)
569 * Sets the given worker node its tasks usage in the pool.
571 * @param workerNode - The worker node.
572 * @param tasksUsage - The worker node tasks usage.
574 private setWorkerNodeTasksUsage (
575 workerNode
: WorkerNode
<Worker
, Data
>,
576 tasksUsage
: TasksUsage
578 workerNode
.tasksUsage
= tasksUsage
582 * Gets the given worker its tasks usage in the pool.
584 * @param worker - The worker.
585 * @throws Error if the worker is not found in the pool worker nodes.
586 * @returns The worker tasks usage.
588 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
{
589 const workerNodeKey
= this.getWorkerNodeKey(worker
)
590 if (workerNodeKey
!== -1) {
591 return this.workerNodes
[workerNodeKey
].tasksUsage
593 throw new Error('Worker could not be found in the pool worker nodes')
597 * Pushes the given worker in the pool worker nodes.
599 * @param worker - The worker.
600 * @returns The worker nodes length.
602 private pushWorkerNode (worker
: Worker
): number {
603 return this.workerNodes
.push({
609 runTimeHistory
: new CircularArray(),
614 tasksQueue
: new Queue
<Task
<Data
>>()
619 * Sets the given worker in the pool worker nodes.
621 * @param workerNodeKey - The worker node key.
622 * @param worker - The worker.
623 * @param tasksUsage - The worker tasks usage.
624 * @param tasksQueue - The worker task queue.
626 private setWorkerNode (
627 workerNodeKey
: number,
629 tasksUsage
: TasksUsage
,
630 tasksQueue
: Queue
<Task
<Data
>>
632 this.workerNodes
[workerNodeKey
] = {
640 * Removes the given worker from the pool worker nodes.
642 * @param worker - The worker.
644 private removeWorkerNode (worker
: Worker
): void {
645 const workerNodeKey
= this.getWorkerNodeKey(worker
)
646 this.workerNodes
.splice(workerNodeKey
, 1)
647 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
650 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
651 this.beforeTaskExecutionHook(workerNodeKey
)
652 this.sendToWorker(this.workerNodes
[workerNodeKey
].worker
, task
)
655 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
656 return this.workerNodes
[workerNodeKey
].tasksQueue
.enqueue(task
)
659 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
660 return this.workerNodes
[workerNodeKey
].tasksQueue
.dequeue()
663 private tasksQueueSize (workerNodeKey
: number): number {
664 return this.workerNodes
[workerNodeKey
].tasksQueue
.size
667 private flushTasksQueue (workerNodeKey
: number): void {
668 if (this.tasksQueueSize(workerNodeKey
) > 0) {
669 for (let i
= 0; i
< this.tasksQueueSize(workerNodeKey
); i
++) {
672 this.dequeueTask(workerNodeKey
) as Task
<Data
>
678 private flushTasksQueueByWorker (worker
: Worker
): void {
679 const workerNodeKey
= this.getWorkerNodeKey(worker
)
680 this.flushTasksQueue(workerNodeKey
)
683 private flushTasksQueues (): void {
684 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
685 this.flushTasksQueue(workerNodeKey
)