1 import crypto from
'node:crypto'
2 import type { MessageValue
, PromiseResponseWrapper
} from
'../utility-types'
4 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
8 import { KillBehaviors
, isKillBehavior
} from
'../worker/worker-options'
13 type TasksQueueOptions
,
16 import { PoolEmitter
} from
'./pool'
17 import type { IWorker
, Task
, TasksUsage
, WorkerNode
} from
'./worker'
19 WorkerChoiceStrategies
,
20 type WorkerChoiceStrategy
21 } from
'./selection-strategies/selection-strategies-types'
22 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
23 import { CircularArray
} from
'../circular-array'
26 * Base class that implements some shared logic for all poolifier pools.
28 * @typeParam Worker - Type of worker which manages this pool.
29 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
30 * @typeParam Response - Type of response of execution. This can only be serializable data.
32 export abstract class AbstractPool
<
33 Worker
extends IWorker
,
36 > implements IPool
<Worker
, Data
, Response
> {
38 public readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>> = []
41 public readonly emitter
?: PoolEmitter
44 * The execution response promise map.
46 * - `key`: The message id of each submitted task.
47 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
49 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
51 protected promiseResponseMap
: Map
<
53 PromiseResponseWrapper
<Worker
, Response
>
54 > = new Map
<string, PromiseResponseWrapper
<Worker
, Response
>>()
57 * Worker choice strategy context referencing a worker choice algorithm implementation.
59 * Default to a round robin algorithm.
61 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
68 * Constructs a new poolifier pool.
70 * @param numberOfWorkers - Number of workers that this pool should manage.
71 * @param filePath - Path to the worker-file.
72 * @param opts - Options for the pool.
75 public readonly numberOfWorkers
: number,
76 public readonly filePath
: string,
77 public readonly opts
: PoolOptions
<Worker
>
80 throw new Error('Cannot start a pool from a worker!')
82 this.checkNumberOfWorkers(this.numberOfWorkers
)
83 this.checkFilePath(this.filePath
)
84 this.checkPoolOptions(this.opts
)
86 this.chooseWorkerNode
.bind(this)
87 this.executeTask
.bind(this)
88 this.enqueueTask
.bind(this)
89 this.checkAndEmitEvents
.bind(this)
93 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
94 this.createAndSetupWorker()
97 if (this.opts
.enableEvents
=== true) {
98 this.emitter
= new PoolEmitter()
100 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
106 this.opts
.workerChoiceStrategy
,
107 this.opts
.workerChoiceStrategyOptions
111 private checkFilePath (filePath
: string): void {
114 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
116 throw new Error('Please specify a file with a worker implementation')
120 private checkNumberOfWorkers (numberOfWorkers
: number): void {
121 if (numberOfWorkers
== null) {
123 'Cannot instantiate a pool without specifying the number of workers'
125 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
127 'Cannot instantiate a pool with a non integer number of workers'
129 } else if (numberOfWorkers
< 0) {
130 throw new RangeError(
131 'Cannot instantiate a pool with a negative number of workers'
133 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
134 throw new Error('Cannot instantiate a fixed pool with no worker')
138 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
139 this.opts
.workerChoiceStrategy
=
140 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
141 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
142 this.opts
.workerChoiceStrategyOptions
=
143 opts
.workerChoiceStrategyOptions
?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
144 this.opts
.enableEvents
= opts
.enableEvents
?? true
145 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
146 if (this.opts
.enableTasksQueue
) {
147 if ((opts
.tasksQueueOptions
?.concurrency
as number) <= 0) {
149 `Invalid worker tasks concurrency '${
150 (opts.tasksQueueOptions as TasksQueueOptions).concurrency as number
154 this.opts
.tasksQueueOptions
= {
155 concurrency
: opts
.tasksQueueOptions
?.concurrency
?? 1
160 private checkValidWorkerChoiceStrategy (
161 workerChoiceStrategy
: WorkerChoiceStrategy
163 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
165 `Invalid worker choice strategy '${workerChoiceStrategy}'`
171 public abstract get
type (): PoolType
174 * Number of tasks running in the pool.
176 private get
numberOfRunningTasks (): number {
177 return this.workerNodes
.reduce(
178 (accumulator
, workerNode
) => accumulator
+ workerNode
.tasksUsage
.running
,
184 * Number of tasks queued in the pool.
186 private get
numberOfQueuedTasks (): number {
187 if (this.opts
.enableTasksQueue
=== false) {
190 return this.workerNodes
.reduce(
191 (accumulator
, workerNode
) => accumulator
+ workerNode
.tasksQueue
.length
,
197 * Gets the given worker its worker node key.
199 * @param worker - The worker.
200 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
202 private getWorkerNodeKey (worker
: Worker
): number {
203 return this.workerNodes
.findIndex(
204 workerNode
=> workerNode
.worker
=== worker
209 public setWorkerChoiceStrategy (
210 workerChoiceStrategy
: WorkerChoiceStrategy
212 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
213 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
214 for (const workerNode
of this.workerNodes
) {
215 this.setWorkerNodeTasksUsage(workerNode
, {
219 runTimeHistory
: new CircularArray(),
225 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
231 public abstract get
full (): boolean
234 public abstract get
busy (): boolean
236 protected internalBusy (): boolean {
237 return this.findFreeWorkerNodeKey() === -1
241 public findFreeWorkerNodeKey (): number {
242 return this.workerNodes
.findIndex(workerNode
=> {
243 return workerNode
.tasksUsage
?.running
=== 0
248 public async execute (data
: Data
): Promise
<Response
> {
249 const [workerNodeKey
, workerNode
] = this.chooseWorkerNode()
250 const submittedTask
: Task
<Data
> = {
251 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
252 data
: data
?? ({} as Data
),
253 id
: crypto
.randomUUID()
255 const res
= new Promise
<Response
>((resolve
, reject
) => {
256 this.promiseResponseMap
.set(submittedTask
.id
, {
259 worker
: workerNode
.worker
263 this.opts
.enableTasksQueue
=== true &&
265 this.workerNodes
[workerNodeKey
].tasksUsage
.running
>=
266 ((this.opts
.tasksQueueOptions
as TasksQueueOptions
)
267 .concurrency
as number))
269 this.enqueueTask(workerNodeKey
, submittedTask
)
271 this.executeTask(workerNodeKey
, submittedTask
)
273 this.checkAndEmitEvents()
274 // eslint-disable-next-line @typescript-eslint/return-await
279 public async destroy (): Promise
<void> {
281 this.workerNodes
.map(async (workerNode
, workerNodeKey
) => {
282 this.flushTasksQueue(workerNodeKey
)
283 await this.destroyWorker(workerNode
.worker
)
289 * Shutdowns the given worker.
291 * @param worker - A worker within `workerNodes`.
293 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
296 * Setup hook to execute code before worker node are created in the abstract constructor.
301 protected setupHook (): void {
302 // Intentionally empty
306 * Should return whether the worker is the main worker or not.
308 protected abstract isMain (): boolean
311 * Hook executed before the worker task execution.
314 * @param workerNodeKey - The worker node key.
316 protected beforeTaskExecutionHook (workerNodeKey
: number): void {
317 ++this.workerNodes
[workerNodeKey
].tasksUsage
.running
321 * Hook executed after the worker task execution.
324 * @param worker - The worker.
325 * @param message - The received message.
327 protected afterTaskExecutionHook (
329 message
: MessageValue
<Response
>
331 const workerTasksUsage
= this.getWorkerTasksUsage(worker
) as TasksUsage
332 --workerTasksUsage
.running
333 ++workerTasksUsage
.run
334 if (message
.error
!= null) {
335 ++workerTasksUsage
.error
337 if (this.workerChoiceStrategyContext
.getRequiredStatistics().runTime
) {
338 workerTasksUsage
.runTime
+= message
.runTime
?? 0
340 this.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
&&
341 workerTasksUsage
.run
!== 0
343 workerTasksUsage
.avgRunTime
=
344 workerTasksUsage
.runTime
/ workerTasksUsage
.run
346 if (this.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
) {
347 workerTasksUsage
.runTimeHistory
.push(message
.runTime
?? 0)
348 workerTasksUsage
.medRunTime
= median(workerTasksUsage
.runTimeHistory
)
354 * Chooses a worker node for the next task.
356 * The default uses a round robin algorithm to distribute the load.
358 * @returns [worker node key, worker node].
360 protected chooseWorkerNode (): [number, WorkerNode
<Worker
, Data
>] {
361 let workerNodeKey
: number
362 if (this.type === PoolType
.DYNAMIC
&& !this.full
&& this.internalBusy()) {
363 const workerCreated
= this.createAndSetupWorker()
364 this.registerWorkerMessageListener(workerCreated
, message
=> {
366 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
367 (message
.kill
!= null &&
368 this.getWorkerTasksUsage(workerCreated
)?.running
=== 0)
370 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
371 this.flushTasksQueueByWorker(workerCreated
)
372 void this.destroyWorker(workerCreated
)
375 workerNodeKey
= this.getWorkerNodeKey(workerCreated
)
377 workerNodeKey
= this.workerChoiceStrategyContext
.execute()
379 return [workerNodeKey
, this.workerNodes
[workerNodeKey
]]
383 * Sends a message to the given worker.
385 * @param worker - The worker which should receive the message.
386 * @param message - The message.
388 protected abstract sendToWorker (
390 message
: MessageValue
<Data
>
394 * Registers a listener callback on the given worker.
396 * @param worker - The worker which should register a listener.
397 * @param listener - The message listener callback.
399 protected abstract registerWorkerMessageListener
<
400 Message
extends Data
| Response
401 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
404 * Returns a newly created worker.
406 protected abstract createWorker (): Worker
409 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
411 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
413 * @param worker - The newly created worker.
415 protected abstract afterWorkerSetup (worker
: Worker
): void
418 * Creates a new worker and sets it up completely in the pool worker nodes.
420 * @returns New, completely set up worker.
422 protected createAndSetupWorker (): Worker
{
423 const worker
= this.createWorker()
425 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
426 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
427 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
428 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
429 worker
.once('exit', () => {
430 this.removeWorkerNode(worker
)
433 this.pushWorkerNode(worker
)
435 this.afterWorkerSetup(worker
)
441 * This function is the listener registered for each worker message.
443 * @returns The listener function to execute when a message is received from a worker.
445 protected workerListener (): (message
: MessageValue
<Response
>) => void {
447 if (message
.id
!= null) {
448 // Task execution response received
449 const promiseResponse
= this.promiseResponseMap
.get(message
.id
)
450 if (promiseResponse
!= null) {
451 if (message
.error
!= null) {
452 promiseResponse
.reject(message
.error
)
454 promiseResponse
.resolve(message
.data
as Response
)
456 this.afterTaskExecutionHook(promiseResponse
.worker
, message
)
457 this.promiseResponseMap
.delete(message
.id
)
458 const workerNodeKey
= this.getWorkerNodeKey(promiseResponse
.worker
)
460 this.opts
.enableTasksQueue
=== true &&
461 this.tasksQueueSize(workerNodeKey
) > 0
465 this.dequeueTask(workerNodeKey
) as Task
<Data
>
473 private checkAndEmitEvents (): void {
474 if (this.opts
.enableEvents
=== true) {
476 this.emitter
?.emit(PoolEvents
.busy
)
478 if (this.type === PoolType
.DYNAMIC
&& this.full
) {
479 this.emitter
?.emit(PoolEvents
.full
)
485 * Sets the given worker node its tasks usage in the pool.
487 * @param workerNode - The worker node.
488 * @param tasksUsage - The worker node tasks usage.
490 private setWorkerNodeTasksUsage (
491 workerNode
: WorkerNode
<Worker
, Data
>,
492 tasksUsage
: TasksUsage
494 workerNode
.tasksUsage
= tasksUsage
498 * Gets the given worker its tasks usage in the pool.
500 * @param worker - The worker.
501 * @returns The worker tasks usage.
503 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
| undefined {
504 const workerNodeKey
= this.getWorkerNodeKey(worker
)
505 if (workerNodeKey
!== -1) {
506 return this.workerNodes
[workerNodeKey
].tasksUsage
508 throw new Error('Worker could not be found in the pool worker nodes')
512 * Pushes the given worker in the pool worker nodes.
514 * @param worker - The worker.
515 * @returns The worker nodes length.
517 private pushWorkerNode (worker
: Worker
): number {
518 return this.workerNodes
.push({
524 runTimeHistory
: new CircularArray(),
534 * Sets the given worker in the pool worker nodes.
536 * @param workerNodeKey - The worker node key.
537 * @param worker - The worker.
538 * @param tasksUsage - The worker tasks usage.
539 * @param tasksQueue - The worker task queue.
541 private setWorkerNode (
542 workerNodeKey
: number,
544 tasksUsage
: TasksUsage
,
545 tasksQueue
: Array<Task
<Data
>>
547 this.workerNodes
[workerNodeKey
] = {
555 * Removes the given worker from the pool worker nodes.
557 * @param worker - The worker.
559 private removeWorkerNode (worker
: Worker
): void {
560 const workerNodeKey
= this.getWorkerNodeKey(worker
)
561 this.workerNodes
.splice(workerNodeKey
, 1)
562 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
565 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
566 this.beforeTaskExecutionHook(workerNodeKey
)
567 this.sendToWorker(this.workerNodes
[workerNodeKey
].worker
, task
)
570 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
571 return this.workerNodes
[workerNodeKey
].tasksQueue
.push(task
)
574 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
575 return this.workerNodes
[workerNodeKey
].tasksQueue
.shift()
578 private tasksQueueSize (workerNodeKey
: number): number {
579 return this.workerNodes
[workerNodeKey
].tasksQueue
.length
582 private flushTasksQueue (workerNodeKey
: number): void {
583 if (this.tasksQueueSize(workerNodeKey
) > 0) {
584 for (const task
of this.workerNodes
[workerNodeKey
].tasksQueue
) {
585 this.executeTask(workerNodeKey
, task
)
590 private flushTasksQueueByWorker (worker
: Worker
): void {
591 const workerNodeKey
= this.getWorkerNodeKey(worker
)
592 this.flushTasksQueue(workerNodeKey
)