1 import crypto from
'node:crypto'
2 import type { MessageValue
, PromiseResponseWrapper
} from
'../utility-types'
3 import { EMPTY_FUNCTION
, median
} from
'../utils'
4 import { KillBehaviors
, isKillBehavior
} from
'../worker/worker-options'
5 import { PoolEvents
, type PoolOptions
} from
'./pool'
6 import { PoolEmitter
} from
'./pool'
7 import type { IPoolInternal
} from
'./pool-internal'
8 import { PoolType
} from
'./pool-internal'
9 import type { IWorker
, Task
, TasksUsage
, WorkerNode
} from
'./worker'
11 WorkerChoiceStrategies
,
12 type WorkerChoiceStrategy
13 } from
'./selection-strategies/selection-strategies-types'
14 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
15 import { CircularArray
} from
'../circular-array'
18 * Base class that implements some shared logic for all poolifier pools.
20 * @typeParam Worker - Type of worker which manages this pool.
21 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
22 * @typeParam Response - Type of response of execution. This can only be serializable data.
24 export abstract class AbstractPool
<
25 Worker
extends IWorker
,
28 > implements IPoolInternal
<Worker
, Data
, Response
> {
30 public readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>> = []
33 public readonly emitter
?: PoolEmitter
36 * The promise response map.
38 * - `key`: The message id of each submitted task.
39 * - `value`: An object that contains the worker, the promise resolve and reject callbacks.
41 * When we receive a message from the worker we get a map entry with the promise resolve/reject bound to the message.
43 protected promiseResponseMap
: Map
<
45 PromiseResponseWrapper
<Worker
, Response
>
46 > = new Map
<string, PromiseResponseWrapper
<Worker
, Response
>>()
49 * Worker choice strategy context referencing a worker choice algorithm implementation.
51 * Default to a round robin algorithm.
53 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
60 * Constructs a new poolifier pool.
62 * @param numberOfWorkers - Number of workers that this pool should manage.
63 * @param filePath - Path to the worker-file.
64 * @param opts - Options for the pool.
67 public readonly numberOfWorkers
: number,
68 public readonly filePath
: string,
69 public readonly opts
: PoolOptions
<Worker
>
72 throw new Error('Cannot start a pool from a worker!')
74 this.checkNumberOfWorkers(this.numberOfWorkers
)
75 this.checkFilePath(this.filePath
)
76 this.checkPoolOptions(this.opts
)
78 this.chooseWorker
.bind(this)
79 this.internalExecute
.bind(this)
80 this.checkAndEmitFull
.bind(this)
81 this.checkAndEmitBusy
.bind(this)
82 this.sendToWorker
.bind(this)
86 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
87 this.createAndSetupWorker()
90 if (this.opts
.enableEvents
=== true) {
91 this.emitter
= new PoolEmitter()
93 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
97 >(this, this.opts
.workerChoiceStrategy
)
100 private checkFilePath (filePath
: string): void {
103 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
105 throw new Error('Please specify a file with a worker implementation')
109 private checkNumberOfWorkers (numberOfWorkers
: number): void {
110 if (numberOfWorkers
== null) {
112 'Cannot instantiate a pool without specifying the number of workers'
114 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
116 'Cannot instantiate a pool with a non integer number of workers'
118 } else if (numberOfWorkers
< 0) {
119 throw new RangeError(
120 'Cannot instantiate a pool with a negative number of workers'
122 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
123 throw new Error('Cannot instantiate a fixed pool with no worker')
127 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
128 this.opts
.workerChoiceStrategy
=
129 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
130 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
131 this.opts
.enableEvents
= opts
.enableEvents
?? true
134 private checkValidWorkerChoiceStrategy (
135 workerChoiceStrategy
: WorkerChoiceStrategy
137 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
139 `Invalid worker choice strategy '${workerChoiceStrategy}'`
145 public abstract get
type (): PoolType
148 * Number of tasks concurrently running in the pool.
150 private get
numberOfRunningTasks (): number {
151 return this.promiseResponseMap
.size
155 * Gets the given worker its worker node key.
157 * @param worker - The worker.
158 * @returns The worker node key if the worker is found in the pool worker nodes, `-1` otherwise.
160 private getWorkerNodeKey (worker
: Worker
): number {
161 return this.workerNodes
.findIndex(
162 workerNode
=> workerNode
.worker
=== worker
167 public setWorkerChoiceStrategy (
168 workerChoiceStrategy
: WorkerChoiceStrategy
170 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
171 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
172 for (const [index
, workerNode
] of this.workerNodes
.entries()) {
180 runTimeHistory
: new CircularArray(),
185 workerNode
.tasksQueue
188 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
194 public abstract get
full (): boolean
197 public abstract get
busy (): boolean
199 protected internalBusy (): boolean {
201 this.numberOfRunningTasks
>= this.numberOfWorkers
&&
202 this.findFreeWorkerNodeKey() === -1
207 public findFreeWorkerNodeKey (): number {
208 return this.workerNodes
.findIndex(workerNode
=> {
209 return workerNode
.tasksUsage
?.running
=== 0
214 public async execute (data
: Data
): Promise
<Response
> {
215 const [workerNodeKey
, worker
] = this.chooseWorker()
216 const messageId
= crypto
.randomUUID()
217 const res
= this.internalExecute(workerNodeKey
, worker
, messageId
)
218 this.checkAndEmitFull()
219 this.checkAndEmitBusy()
220 this.sendToWorker(worker
, {
221 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
222 data
: data
?? ({} as Data
),
225 // eslint-disable-next-line @typescript-eslint/return-await
230 public async destroy (): Promise
<void> {
232 this.workerNodes
.map(async workerNode
=> {
233 await this.destroyWorker(workerNode
.worker
)
239 * Shutdowns the given worker.
241 * @param worker - A worker within `workerNodes`.
243 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
246 * Setup hook to run code before worker node are created in the abstract constructor.
251 protected setupHook (): void {
252 // Intentionally empty
256 * Should return whether the worker is the main worker or not.
258 protected abstract isMain (): boolean
261 * Hook executed before the worker task promise resolution.
264 * @param workerNodeKey - The worker node key.
266 protected beforePromiseResponseHook (workerNodeKey
: number): void {
267 ++this.workerNodes
[workerNodeKey
].tasksUsage
.running
271 * Hook executed after the worker task promise resolution.
274 * @param worker - The worker.
275 * @param message - The received message.
277 protected afterPromiseResponseHook (
279 message
: MessageValue
<Response
>
281 const workerTasksUsage
= this.getWorkerTasksUsage(worker
) as TasksUsage
282 --workerTasksUsage
.running
283 ++workerTasksUsage
.run
284 if (message
.error
!= null) {
285 ++workerTasksUsage
.error
287 if (this.workerChoiceStrategyContext
.getRequiredStatistics().runTime
) {
288 workerTasksUsage
.runTime
+= message
.runTime
?? 0
290 this.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
&&
291 workerTasksUsage
.run
!== 0
293 workerTasksUsage
.avgRunTime
=
294 workerTasksUsage
.runTime
/ workerTasksUsage
.run
296 if (this.workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
) {
297 workerTasksUsage
.runTimeHistory
.push(message
.runTime
?? 0)
298 workerTasksUsage
.medRunTime
= median(workerTasksUsage
.runTimeHistory
)
304 * Chooses a worker node for the next task.
306 * The default uses a round robin algorithm to distribute the load.
308 * @returns [worker node key, worker].
310 protected chooseWorker (): [number, Worker
] {
311 let workerNodeKey
: number
313 this.type === PoolType
.DYNAMIC
&&
315 this.findFreeWorkerNodeKey() === -1
317 const createdWorker
= this.createAndSetupWorker()
318 this.registerWorkerMessageListener(createdWorker
, message
=> {
320 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
321 (message
.kill
!= null &&
322 this.getWorkerTasksUsage(createdWorker
)?.running
=== 0)
324 // Kill message received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
325 void this.destroyWorker(createdWorker
)
328 workerNodeKey
= this.getWorkerNodeKey(createdWorker
)
330 workerNodeKey
= this.workerChoiceStrategyContext
.execute()
332 return [workerNodeKey
, this.workerNodes
[workerNodeKey
].worker
]
336 * Sends a message to the given worker.
338 * @param worker - The worker which should receive the message.
339 * @param message - The message.
341 protected abstract sendToWorker (
343 message
: MessageValue
<Data
>
347 * Registers a listener callback on the given worker.
349 * @param worker - The worker which should register a listener.
350 * @param listener - The message listener callback.
352 protected abstract registerWorkerMessageListener
<
353 Message
extends Data
| Response
354 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
357 * Returns a newly created worker.
359 protected abstract createWorker (): Worker
362 * Function that can be hooked up when a worker has been newly created and moved to the pool worker nodes.
364 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
366 * @param worker - The newly created worker.
368 protected abstract afterWorkerSetup (worker
: Worker
): void
371 * Creates a new worker and sets it up completely in the pool worker nodes.
373 * @returns New, completely set up worker.
375 protected createAndSetupWorker (): Worker
{
376 const worker
= this.createWorker()
378 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
379 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
380 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
381 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
382 worker
.once('exit', () => {
383 this.removeWorkerNode(worker
)
386 this.pushWorkerNode(worker
)
388 this.afterWorkerSetup(worker
)
394 * This function is the listener registered for each worker.
396 * @returns The listener function to execute when a message is received from a worker.
398 protected workerListener (): (message
: MessageValue
<Response
>) => void {
400 if (message
.id
!= null) {
401 // Task response received
402 const promiseResponse
= this.promiseResponseMap
.get(message
.id
)
403 if (promiseResponse
!= null) {
404 if (message
.error
!= null) {
405 promiseResponse
.reject(message
.error
)
407 promiseResponse
.resolve(message
.data
as Response
)
409 this.afterPromiseResponseHook(promiseResponse
.worker
, message
)
410 this.promiseResponseMap
.delete(message
.id
)
416 private async internalExecute (
417 workerNodeKey
: number,
420 ): Promise
<Response
> {
421 this.beforePromiseResponseHook(workerNodeKey
)
422 return await new Promise
<Response
>((resolve
, reject
) => {
423 this.promiseResponseMap
.set(messageId
, { resolve
, reject
, worker
})
427 private checkAndEmitBusy (): void {
428 if (this.opts
.enableEvents
=== true && this.busy
) {
429 this.emitter
?.emit(PoolEvents
.busy
)
433 private checkAndEmitFull (): void {
435 this.type === PoolType
.DYNAMIC
&&
436 this.opts
.enableEvents
=== true &&
439 this.emitter
?.emit(PoolEvents
.full
)
444 * Gets the given worker its tasks usage in the pool.
446 * @param worker - The worker.
447 * @returns The worker tasks usage.
449 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
| undefined {
450 const workerNodeKey
= this.getWorkerNodeKey(worker
)
451 if (workerNodeKey
!== -1) {
452 return this.workerNodes
[workerNodeKey
].tasksUsage
454 throw new Error('Worker could not be found in the pool worker nodes')
458 * Pushes the given worker in the pool worker nodes.
460 * @param worker - The worker.
461 * @returns The worker nodes length.
463 private pushWorkerNode (worker
: Worker
): number {
464 return this.workerNodes
.push({
470 runTimeHistory
: new CircularArray(),
480 * Sets the given worker in the pool worker nodes.
482 * @param workerNodeKey - The worker node key.
483 * @param worker - The worker.
484 * @param tasksUsage - The worker tasks usage.
485 * @param tasksQueue - The worker task queue.
487 private setWorkerNode (
488 workerNodeKey
: number,
490 tasksUsage
: TasksUsage
,
491 tasksQueue
: Array<Task
<Data
>>
493 this.workerNodes
[workerNodeKey
] = {
501 * Removes the given worker from the pool worker nodes.
503 * @param worker - The worker.
505 protected removeWorkerNode (worker
: Worker
): void {
506 const workerNodeKey
= this.getWorkerNodeKey(worker
)
507 this.workerNodes
.splice(workerNodeKey
, 1)
508 this.workerChoiceStrategyContext
.remove(workerNodeKey
)