3 PromiseWorkerResponseWrapper
4 } from
'../utility-types'
5 import { EMPTY_FUNCTION
} from
'../utils'
6 import { isKillBehavior
, KillBehaviors
} from
'../worker/worker-options'
7 import type { IPoolInternal
} from
'./pool-internal'
8 import { PoolEmitter
, PoolType
} from
'./pool-internal'
9 import type { WorkerChoiceStrategy
} from
'./selection-strategies'
11 WorkerChoiceStrategies
,
12 WorkerChoiceStrategyContext
13 } from
'./selection-strategies'
16 * Callback invoked if the worker raised an error.
18 export type ErrorHandler
<Worker
> = (this: Worker
, e
: Error) => void
21 * Callback invoked when the worker has started successfully.
23 export type OnlineHandler
<Worker
> = (this: Worker
) => void
26 * Callback invoked when the worker exits successfully.
28 export type ExitHandler
<Worker
> = (this: Worker
, code
: number) => void
31 * Basic interface that describes the minimum required implementation of listener events for a pool-worker.
33 export interface IWorker
{
35 * Register a listener to the error event.
37 * @param event `'error'`.
38 * @param handler The error handler.
40 on(event
: 'error', handler
: ErrorHandler
<this>): void
42 * Register a listener to the online event.
44 * @param event `'online'`.
45 * @param handler The online handler.
47 on(event
: 'online', handler
: OnlineHandler
<this>): void
49 * Register a listener to the exit event.
51 * @param event `'exit'`.
52 * @param handler The exit handler.
54 on(event
: 'exit', handler
: ExitHandler
<this>): void
56 * Register a listener to the exit event that will only performed once.
58 * @param event `'exit'`.
59 * @param handler The exit handler.
61 once(event
: 'exit', handler
: ExitHandler
<this>): void
65 * Options for a poolifier pool.
67 export interface PoolOptions
<Worker
> {
69 * A function that will listen for error event on each worker.
71 errorHandler
?: ErrorHandler
<Worker
>
73 * A function that will listen for online event on each worker.
75 onlineHandler
?: OnlineHandler
<Worker
>
77 * A function that will listen for exit event on each worker.
79 exitHandler
?: ExitHandler
<Worker
>
81 * The work choice strategy to use in this pool.
83 workerChoiceStrategy
?: WorkerChoiceStrategy
85 * Pool events emission.
89 enableEvents
?: boolean
93 * Base class containing some shared logic for all poolifier pools.
95 * @template Worker Type of worker which manages this pool.
96 * @template Data Type of data sent to the worker. This can only be serializable data.
97 * @template Response Type of response of execution. This can only be serializable data.
99 export abstract class AbstractPool
<
100 Worker
extends IWorker
,
103 > implements IPoolInternal
<Worker
, Data
, Response
>
106 public readonly workers
: Worker
[] = []
109 public readonly tasks
: Map
<Worker
, number> = new Map
<Worker
, number>()
112 public readonly emitter
?: PoolEmitter
115 public readonly max
?: number
120 * - `key`: This is the message Id of each submitted task.
121 * - `value`: An object that contains the worker, the resolve function and the reject function.
123 * When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message.
125 protected promiseMap
: Map
<
127 PromiseWorkerResponseWrapper
<Worker
, Response
>
128 > = new Map
<number, PromiseWorkerResponseWrapper
<Worker
, Response
>>()
131 * Id of the next message.
133 protected nextMessageId
: number = 0
136 * Worker choice strategy instance implementing the worker choice algorithm.
138 * Default to a strategy implementing a round robin algorithm.
140 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
147 * Constructs a new poolifier pool.
149 * @param numberOfWorkers Number of workers that this pool should manage.
150 * @param filePath Path to the worker-file.
151 * @param opts Options for the pool.
154 public readonly numberOfWorkers
: number,
155 public readonly filePath
: string,
156 public readonly opts
: PoolOptions
<Worker
>
158 if (!this.isMain()) {
159 throw new Error('Cannot start a pool from a worker!')
161 this.checkNumberOfWorkers(this.numberOfWorkers
)
162 this.checkFilePath(this.filePath
)
163 this.checkPoolOptions(this.opts
)
166 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
167 this.createAndSetupWorker()
170 if (this.opts
.enableEvents
) {
171 this.emitter
= new PoolEmitter()
173 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
176 const workerCreated
= this.createAndSetupWorker()
177 this.registerWorkerMessageListener(workerCreated
, async message
=> {
178 const tasksInProgress
= this.tasks
.get(workerCreated
)
180 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
181 tasksInProgress
=== 0
183 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
184 await this.destroyWorker(workerCreated
)
189 this.opts
.workerChoiceStrategy
193 private checkFilePath (filePath
: string): void {
195 throw new Error('Please specify a file with a worker implementation')
199 private checkNumberOfWorkers (numberOfWorkers
: number): void {
200 if (numberOfWorkers
== null) {
202 'Cannot instantiate a pool without specifying the number of workers'
204 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
206 'Cannot instantiate a pool with a non integer number of workers'
208 } else if (numberOfWorkers
< 0) {
210 'Cannot instantiate a pool with a negative number of workers'
212 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
213 throw new Error('Cannot instantiate a fixed pool with no worker')
217 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
218 this.opts
.workerChoiceStrategy
=
219 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
220 this.opts
.enableEvents
= opts
.enableEvents
?? true
224 public abstract get
type (): PoolType
227 public get
numberOfRunningTasks (): number {
228 return this.promiseMap
.size
232 public setWorkerChoiceStrategy (
233 workerChoiceStrategy
: WorkerChoiceStrategy
235 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
236 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
242 public abstract get
busy (): boolean
244 protected internalGetBusyStatus (): boolean {
246 this.numberOfRunningTasks
>= this.numberOfWorkers
&&
247 this.findFreeTasksMapEntry() === false
252 public findFreeTasksMapEntry (): [Worker
, number] | false {
253 for (const [worker
, numberOfTasks
] of this.tasks
) {
254 if (numberOfTasks
=== 0) {
255 // A worker is free, return the matching tasks map entry
256 return [worker
, numberOfTasks
]
263 public execute (data
: Data
): Promise
<Response
> {
264 // Configure worker to handle message with the specified task
265 const worker
= this.chooseWorker()
266 const messageId
= ++this.nextMessageId
267 const res
= this.internalExecute(worker
, messageId
)
268 this.checkAndEmitBusy()
269 this.sendToWorker(worker
, { data
: data
|| ({} as Data
), id
: messageId
})
274 public async destroy (): Promise
<void> {
275 await Promise
.all(this.workers
.map(worker
=> this.destroyWorker(worker
)))
279 * Shut down given worker.
281 * @param worker A worker within `workers`.
283 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
286 * Setup hook that can be overridden by a Poolifier pool implementation
287 * to run code before workers are created in the abstract constructor.
289 protected setupHook (): void {
294 * Should return whether the worker is the main worker or not.
296 protected abstract isMain (): boolean
299 * Increase the number of tasks that the given workers has applied.
301 * @param worker Worker whose tasks are increased.
303 protected increaseWorkersTask (worker
: Worker
): void {
304 this.stepWorkerNumberOfTasks(worker
, 1)
308 * Decrease the number of tasks that the given workers has applied.
310 * @param worker Worker whose tasks are decreased.
312 protected decreaseWorkersTasks (worker
: Worker
): void {
313 this.stepWorkerNumberOfTasks(worker
, -1)
317 * Step the number of tasks that the given workers has applied.
319 * @param worker Worker whose tasks are set.
320 * @param step Worker number of tasks step.
322 private stepWorkerNumberOfTasks (worker
: Worker
, step
: number): void {
323 const numberOfTasksInProgress
= this.tasks
.get(worker
)
324 if (numberOfTasksInProgress
!== undefined) {
325 this.tasks
.set(worker
, numberOfTasksInProgress
+ step
)
327 throw Error('Worker could not be found in tasks map')
332 * Removes the given worker from the pool.
334 * @param worker Worker that will be removed.
336 protected removeWorker (worker
: Worker
): void {
337 // Clean worker from data structure
338 const workerIndex
= this.workers
.indexOf(worker
)
339 this.workers
.splice(workerIndex
, 1)
340 this.tasks
.delete(worker
)
344 * Choose a worker for the next task.
346 * The default implementation uses a round robin algorithm to distribute the load.
350 protected chooseWorker (): Worker
{
351 return this.workerChoiceStrategyContext
.execute()
355 * Send a message to the given worker.
357 * @param worker The worker which should receive the message.
358 * @param message The message.
360 protected abstract sendToWorker (
362 message
: MessageValue
<Data
>
366 * Register a listener callback on a given worker.
368 * @param worker A worker.
369 * @param listener A message listener callback.
371 protected abstract registerWorkerMessageListener
<
372 Message
extends Data
| Response
373 > (worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
375 protected internalExecute (
378 ): Promise
<Response
> {
379 this.increaseWorkersTask(worker
)
380 return new Promise
<Response
>((resolve
, reject
) => {
381 this.promiseMap
.set(messageId
, { resolve
, reject
, worker
})
386 * Returns a newly created worker.
388 protected abstract createWorker (): Worker
391 * Function that can be hooked up when a worker has been newly created and moved to the workers registry.
393 * Can be used to update the `maxListeners` or binding the `main-worker`<->`worker` connection if not bind by default.
395 * @param worker The newly created worker.
397 protected abstract afterWorkerSetup (worker
: Worker
): void
400 * Creates a new worker for this pool and sets it up completely.
402 * @returns New, completely set up worker.
404 protected createAndSetupWorker (): Worker
{
405 const worker
: Worker
= this.createWorker()
407 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
408 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
409 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
410 worker
.once('exit', () => this.removeWorker(worker
))
412 this.workers
.push(worker
)
415 this.tasks
.set(worker
, 0)
417 this.afterWorkerSetup(worker
)
423 * This function is the listener registered for each worker.
425 * @returns The listener function to execute when a message is sent from a worker.
427 protected workerListener (): (message
: MessageValue
<Response
>) => void {
430 const value
= this.promiseMap
.get(message
.id
)
432 this.decreaseWorkersTasks(value
.worker
)
433 if (message
.error
) value
.reject(message
.error
)
434 else value
.resolve(message
.data
as Response
)
435 this.promiseMap
.delete(message
.id
)
441 private checkAndEmitBusy (): void {
442 if (this.opts
.enableEvents
&& this.busy
) {
443 this.emitter
?.emit('busy')