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(
94 const createdWorker
= this.createAndSetupWorker()
95 this.registerWorkerMessageListener(createdWorker
, message
=> {
97 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
98 this.getWorkerTasksUsage(createdWorker
)?.running
=== 0
100 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
101 void this.destroyWorker(createdWorker
)
104 return this.getWorkerKey(createdWorker
)
106 this.opts
.workerChoiceStrategy
110 private checkFilePath (filePath
: string): void {
113 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
115 throw new Error('Please specify a file with a worker implementation')
119 private checkNumberOfWorkers (numberOfWorkers
: number): void {
120 if (numberOfWorkers
== null) {
122 'Cannot instantiate a pool without specifying the number of workers'
124 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
126 'Cannot instantiate a pool with a non integer number of workers'
128 } else if (numberOfWorkers
< 0) {
129 throw new RangeError(
130 'Cannot instantiate a pool with a negative number of workers'
132 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
133 throw new Error('Cannot instantiate a fixed pool with no worker')
137 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
138 this.opts
.workerChoiceStrategy
=
139 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
140 this.opts
.enableEvents
= opts
.enableEvents
?? true
144 public abstract get
type (): PoolType
147 * Number of tasks concurrently running.
149 private get
numberOfRunningTasks (): number {
150 return this.promiseResponseMap
.size
154 * Gets the given worker key.
156 * @param worker - The worker.
157 * @returns The worker key if the worker is found in the pool, `-1` otherwise.
159 private getWorkerKey (worker
: Worker
): number {
160 return this.workers
.findIndex(workerItem
=> workerItem
.worker
=== worker
)
164 public setWorkerChoiceStrategy (
165 workerChoiceStrategy
: WorkerChoiceStrategy
167 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
168 for (const [index
, workerItem
] of this.workers
.entries()) {
169 this.setWorker(index
, workerItem
.worker
, {
177 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
183 public abstract get
full (): boolean
186 public abstract get
busy (): boolean
188 protected internalBusy (): boolean {
190 this.numberOfRunningTasks
>= this.numberOfWorkers
&&
191 this.findFreeWorkerKey() === -1
196 public findFreeWorkerKey (): number {
197 return this.workers
.findIndex(workerItem
=> {
198 return workerItem
.tasksUsage
.running
=== 0
203 public async execute (data
: Data
): Promise
<Response
> {
204 const [workerKey
, worker
] = this.chooseWorker()
205 const messageId
= crypto
.randomUUID()
206 const res
= this.internalExecute(workerKey
, worker
, messageId
)
207 this.checkAndEmitBusy()
208 this.sendToWorker(worker
, {
209 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
210 data
: data
?? ({} as Data
),
213 // eslint-disable-next-line @typescript-eslint/return-await
218 public async destroy (): Promise
<void> {
220 this.workers
.map(async workerItem
=> {
221 await this.destroyWorker(workerItem
.worker
)
227 * Shutdowns given worker.
229 * @param worker - A worker within `workers`.
231 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
234 * Setup hook that can be overridden by a Poolifier pool implementation
235 * to run code before workers are created in the abstract constructor.
237 protected setupHook (): void {
242 * Should return whether the worker is the main worker or not.
244 protected abstract isMain (): boolean
247 * Hook executed before the worker task promise resolution.
250 * @param workerKey - The worker key.
252 protected beforePromiseResponseHook (workerKey
: number): void {
253 ++this.workers
[workerKey
].tasksUsage
.running
257 * Hook executed after the worker task promise resolution.
260 * @param worker - The worker.
261 * @param message - The received message.
263 protected afterPromiseResponseHook (
265 message
: MessageValue
<Response
>
267 const workerTasksUsage
= this.getWorkerTasksUsage(worker
) as TasksUsage
268 --workerTasksUsage
.running
269 ++workerTasksUsage
.run
270 if (message
.error
!= null) {
271 ++workerTasksUsage
.error
273 if (this.workerChoiceStrategyContext
.getRequiredStatistics().runTime
) {
274 workerTasksUsage
.runTime
+= message
.taskRunTime
?? 0
276 this.workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
&&
277 workerTasksUsage
.run
!== 0
279 workerTasksUsage
.avgRunTime
=
280 workerTasksUsage
.runTime
/ workerTasksUsage
.run
286 * Removes the given worker from the pool.
288 * @param worker - The worker that will be removed.
290 protected removeWorker (worker
: Worker
): void {
291 const workerKey
= this.getWorkerKey(worker
)
292 this.workers
.splice(workerKey
, 1)
293 this.workerChoiceStrategyContext
.remove(workerKey
)
297 * Chooses a worker for the next task.
299 * The default implementation uses a round robin algorithm to distribute the load.
301 * @returns [worker key, worker].
303 protected chooseWorker (): [number, Worker
] {
304 const workerKey
= this.workerChoiceStrategyContext
.execute()
305 return [workerKey
, this.workers
[workerKey
].worker
]
309 * Sends a message to the given worker.
311 * @param worker - The worker which should receive the message.
312 * @param message - The message.
314 protected abstract sendToWorker (
316 message
: MessageValue
<Data
>
320 * Registers a listener callback on a given worker.
322 * @param worker - The worker which should register a listener.
323 * @param listener - The message listener callback.
325 protected abstract registerWorkerMessageListener
<
326 Message
extends Data
| Response
327 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
330 * Returns a newly created worker.
332 protected abstract createWorker (): Worker
335 * Function that can be hooked up when a worker has been newly created and moved to the workers registry.
337 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
339 * @param worker - The newly created worker.
341 protected abstract afterWorkerSetup (worker
: Worker
): void
344 * Creates a new worker for this pool and sets it up completely.
346 * @returns New, completely set up worker.
348 protected createAndSetupWorker (): Worker
{
349 const worker
= this.createWorker()
351 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
352 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
353 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
354 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
355 worker
.once('exit', () => {
356 this.removeWorker(worker
)
359 this.pushWorker(worker
, {
367 this.afterWorkerSetup(worker
)
373 * This function is the listener registered for each worker.
375 * @returns The listener function to execute when a message is received from a worker.
377 protected workerListener (): (message
: MessageValue
<Response
>) => void {
379 if (message
.id
!== undefined) {
380 const promiseResponse
= this.promiseResponseMap
.get(message
.id
)
381 if (promiseResponse
!== undefined) {
382 if (message
.error
!= null) {
383 promiseResponse
.reject(message
.error
)
385 promiseResponse
.resolve(message
.data
as Response
)
387 this.afterPromiseResponseHook(promiseResponse
.worker
, message
)
388 this.promiseResponseMap
.delete(message
.id
)
394 private async internalExecute (
398 ): Promise
<Response
> {
399 this.beforePromiseResponseHook(workerKey
)
400 return await new Promise
<Response
>((resolve
, reject
) => {
401 this.promiseResponseMap
.set(messageId
, { resolve
, reject
, worker
})
405 private checkAndEmitBusy (): void {
406 if (this.opts
.enableEvents
=== true && this.busy
) {
407 this.emitter
?.emit('busy')
412 * Gets worker tasks usage.
414 * @param worker - The worker.
415 * @returns The worker tasks usage.
417 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
| undefined {
418 const workerKey
= this.getWorkerKey(worker
)
419 if (workerKey
!== -1) {
420 return this.workers
[workerKey
].tasksUsage
422 throw new Error('Worker could not be found in the pool')
426 * Pushes the given worker.
428 * @param worker - The worker.
429 * @param tasksUsage - The worker tasks usage.
431 private pushWorker (worker
: Worker
, tasksUsage
: TasksUsage
): void {
439 * Sets the given worker.
441 * @param workerKey - The worker key.
442 * @param worker - The worker.
443 * @param tasksUsage - The worker tasks usage.
448 tasksUsage
: TasksUsage
450 this.workers
[workerKey
] = {