1 import crypto from
'node:crypto'
2 import type { MessageValue
, PromiseResponseWrapper
} from
'../utility-types'
3 import { EMPTY_FUNCTION
} from
'../utils'
4 import { KillBehaviors
, isKillBehavior
} from
'../worker/worker-options'
5 import type { PoolOptions
} from
'./pool'
6 import { PoolEmitter
} from
'./pool'
7 import type { IPoolInternal
, TasksUsage
, WorkerType
} from
'./pool-internal'
8 import { PoolType
} from
'./pool-internal'
9 import type { IPoolWorker
} from
'./pool-worker'
11 WorkerChoiceStrategies
,
12 type WorkerChoiceStrategy
13 } from
'./selection-strategies/selection-strategies-types'
14 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
17 * Base class that implements some shared logic for all poolifier pools.
19 * @typeParam Worker - Type of worker which manages this pool.
20 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
21 * @typeParam Response - Type of response of execution. This can only be serializable data.
23 export abstract class AbstractPool
<
24 Worker
extends IPoolWorker
,
27 > implements IPoolInternal
<Worker
, Data
, Response
> {
29 public readonly workers
: Array<WorkerType
<Worker
>> = []
32 public readonly emitter
?: PoolEmitter
35 * The promise response map.
37 * - `key`: The message id of each submitted task.
38 * - `value`: An object that contains the worker, the promise resolve and reject callbacks.
40 * When we receive a message from the worker we get a map entry with the promise resolve/reject bound to the message.
42 protected promiseResponseMap
: Map
<
44 PromiseResponseWrapper
<Worker
, Response
>
45 > = new Map
<string, PromiseResponseWrapper
<Worker
, Response
>>()
48 * Worker choice strategy instance implementing the worker choice algorithm.
50 * Default to a strategy implementing a round robin algorithm.
52 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
59 * Constructs a new poolifier pool.
61 * @param numberOfWorkers - Number of workers that this pool should manage.
62 * @param filePath - Path to the worker-file.
63 * @param opts - Options for the pool.
66 public readonly numberOfWorkers
: number,
67 public readonly filePath
: string,
68 public readonly opts
: PoolOptions
<Worker
>
71 throw new Error('Cannot start a pool from a worker!')
73 this.checkNumberOfWorkers(this.numberOfWorkers
)
74 this.checkFilePath(this.filePath
)
75 this.checkPoolOptions(this.opts
)
77 this.chooseWorker
.bind(this)
78 this.internalExecute
.bind(this)
79 this.checkAndEmitBusy
.bind(this)
80 this.sendToWorker
.bind(this)
84 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
85 this.createAndSetupWorker()
88 if (this.opts
.enableEvents
=== true) {
89 this.emitter
= new PoolEmitter()
91 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
98 const createdWorker
= this.createAndSetupWorker()
99 this.registerWorkerMessageListener(createdWorker
, message
=> {
101 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
102 this.getWorkerTasksUsage(createdWorker
)?.running
=== 0
104 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
105 void this.destroyWorker(createdWorker
)
108 return this.getWorkerKey(createdWorker
)
110 this.opts
.workerChoiceStrategy
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 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 this.opts
.workerChoiceStrategy
=
143 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
144 this.opts
.enableEvents
= opts
.enableEvents
?? true
148 public abstract get
type (): PoolType
151 * Number of tasks concurrently running.
153 private get
numberOfRunningTasks (): number {
154 return this.promiseResponseMap
.size
158 * Gets the given worker key.
160 * @param worker - The worker.
161 * @returns The worker key if the worker is found in the pool, `-1` otherwise.
163 private getWorkerKey (worker
: Worker
): number {
164 return this.workers
.findIndex(workerItem
=> workerItem
.worker
=== worker
)
168 public setWorkerChoiceStrategy (
169 workerChoiceStrategy
: WorkerChoiceStrategy
171 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
172 for (const [index
, workerItem
] of this.workers
.entries()) {
173 this.setWorker(index
, workerItem
.worker
, {
181 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
187 public abstract get
full (): boolean
190 public abstract get
busy (): boolean
192 protected internalBusy (): boolean {
194 this.numberOfRunningTasks
>= this.numberOfWorkers
&&
195 this.findFreeWorkerKey() === -1
200 public findFreeWorkerKey (): number {
201 return this.workers
.findIndex(workerItem
=> {
202 return workerItem
.tasksUsage
.running
=== 0
207 public async execute (data
: Data
): Promise
<Response
> {
208 const [workerKey
, worker
] = this.chooseWorker()
209 const messageId
= crypto
.randomUUID()
210 const res
= this.internalExecute(workerKey
, worker
, messageId
)
211 this.checkAndEmitBusy()
212 this.sendToWorker(worker
, {
213 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
214 data
: data
?? ({} as Data
),
217 // eslint-disable-next-line @typescript-eslint/return-await
222 public async destroy (): Promise
<void> {
224 this.workers
.map(async workerItem
=> {
225 await this.destroyWorker(workerItem
.worker
)
231 * Shutdowns given worker.
233 * @param worker - A worker within `workers`.
235 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
238 * Setup hook that can be overridden by a Poolifier pool implementation
239 * to run code before workers are created in the abstract constructor.
241 protected setupHook (): void {
246 * Should return whether the worker is the main worker or not.
248 protected abstract isMain (): boolean
251 * Hook executed before the worker task promise resolution.
254 * @param workerKey - The worker key.
256 protected beforePromiseResponseHook (workerKey
: number): void {
257 ++this.workers
[workerKey
].tasksUsage
.running
261 * Hook executed after the worker task promise resolution.
264 * @param worker - The worker.
265 * @param message - The received message.
267 protected afterPromiseResponseHook (
269 message
: MessageValue
<Response
>
271 const workerTasksUsage
= this.getWorkerTasksUsage(worker
) as TasksUsage
272 --workerTasksUsage
.running
273 ++workerTasksUsage
.run
274 if (message
.error
!= null) {
275 ++workerTasksUsage
.error
277 if (this.workerChoiceStrategyContext
.getRequiredStatistics().runTime
) {
278 workerTasksUsage
.runTime
+= message
.taskRunTime
?? 0
280 this.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
&&
281 workerTasksUsage
.run
!== 0
283 workerTasksUsage
.avgRunTime
=
284 workerTasksUsage
.runTime
/ workerTasksUsage
.run
290 * Removes the given worker from the pool.
292 * @param worker - The worker that will be removed.
294 protected removeWorker (worker
: Worker
): void {
295 const workerKey
= this.getWorkerKey(worker
)
296 this.workers
.splice(workerKey
, 1)
297 this.workerChoiceStrategyContext
.remove(workerKey
)
301 * Chooses a worker for the next task.
303 * The default implementation uses a round robin algorithm to distribute the load.
305 * @returns [worker key, worker].
307 protected chooseWorker (): [number, Worker
] {
308 const workerKey
= this.workerChoiceStrategyContext
.execute()
309 return [workerKey
, this.workers
[workerKey
].worker
]
313 * Sends a message to the given worker.
315 * @param worker - The worker which should receive the message.
316 * @param message - The message.
318 protected abstract sendToWorker (
320 message
: MessageValue
<Data
>
324 * Registers a listener callback on a given worker.
326 * @param worker - The worker which should register a listener.
327 * @param listener - The message listener callback.
329 protected abstract registerWorkerMessageListener
<
330 Message
extends Data
| Response
331 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
334 * Returns a newly created worker.
336 protected abstract createWorker (): Worker
339 * Function that can be hooked up when a worker has been newly created and moved to the workers registry.
341 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
343 * @param worker - The newly created worker.
345 protected abstract afterWorkerSetup (worker
: Worker
): void
348 * Creates a new worker for this pool and sets it up completely.
350 * @returns New, completely set up worker.
352 protected createAndSetupWorker (): Worker
{
353 const worker
= this.createWorker()
355 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
356 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
357 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
358 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
359 worker
.once('exit', () => {
360 this.removeWorker(worker
)
363 this.pushWorker(worker
, {
371 this.afterWorkerSetup(worker
)
377 * This function is the listener registered for each worker.
379 * @returns The listener function to execute when a message is received from a worker.
381 protected workerListener (): (message
: MessageValue
<Response
>) => void {
383 if (message
.id
!== undefined) {
384 const promiseResponse
= this.promiseResponseMap
.get(message
.id
)
385 if (promiseResponse
!== undefined) {
386 if (message
.error
!= null) {
387 promiseResponse
.reject(message
.error
)
389 promiseResponse
.resolve(message
.data
as Response
)
391 this.afterPromiseResponseHook(promiseResponse
.worker
, message
)
392 this.promiseResponseMap
.delete(message
.id
)
398 private async internalExecute (
402 ): Promise
<Response
> {
403 this.beforePromiseResponseHook(workerKey
)
404 return await new Promise
<Response
>((resolve
, reject
) => {
405 this.promiseResponseMap
.set(messageId
, { resolve
, reject
, worker
})
409 private checkAndEmitBusy (): void {
410 if (this.opts
.enableEvents
=== true && this.busy
) {
411 this.emitter
?.emit('busy')
416 * Gets worker tasks usage.
418 * @param worker - The worker.
419 * @returns The worker tasks usage.
421 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
| undefined {
422 const workerKey
= this.getWorkerKey(worker
)
423 if (workerKey
!== -1) {
424 return this.workers
[workerKey
].tasksUsage
426 throw new Error('Worker could not be found in the pool')
430 * Pushes the given worker.
432 * @param worker - The worker.
433 * @param tasksUsage - The worker tasks usage.
435 private pushWorker (worker
: Worker
, tasksUsage
: TasksUsage
): void {
443 * Sets the given worker.
445 * @param workerKey - The worker key.
446 * @param worker - The worker.
447 * @param tasksUsage - The worker tasks usage.
452 tasksUsage
: TasksUsage
454 this.workers
[workerKey
] = {