X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=a092dffde443b35d3c09fb8212b159ede3851c58;hb=f06cc469150312e33a65c59d01341e038922f0f1;hp=ff7413dd52f2f24e32b7359ce0f14af6180cca67;hpb=7c0ba92006a5c188738ffc5ff642c51f172df3d6;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index ff7413dd..a092dffd 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -2,6 +2,7 @@ import type { MessageValue, PromiseWorkerResponseWrapper } from '../utility-types' +import { EMPTY_FUNCTION } from '../utils' import { isKillBehavior, KillBehaviors } from '../worker/worker-options' import type { IPoolInternal } from './pool-internal' import { PoolEmitter, PoolType } from './pool-internal' @@ -11,13 +12,6 @@ import { WorkerChoiceStrategyContext } from './selection-strategies' -/** - * An intentional empty function. - */ -const EMPTY_FUNCTION: () => void = () => { - /* Intentionally empty */ -} - /** * Callback invoked if the worker raised an error. */ @@ -106,7 +100,8 @@ export abstract class AbstractPool< Worker extends IWorker, Data = unknown, Response = unknown -> implements IPoolInternal { +> implements IPoolInternal +{ /** @inheritdoc */ public readonly workers: Worker[] = [] @@ -122,7 +117,7 @@ export abstract class AbstractPool< /** * The promise map. * - * - `key`: This is the message ID of each submitted task. + * - `key`: This is the message Id of each submitted task. * - `value`: An object that contains the worker, the resolve function and the reject function. * * When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message. @@ -133,7 +128,7 @@ export abstract class AbstractPool< > = new Map>() /** - * ID of the next message. + * Id of the next message. */ protected nextMessageId: number = 0 @@ -179,19 +174,19 @@ export abstract class AbstractPool< this, () => { const workerCreated = this.createAndSetupWorker() - this.registerWorkerMessageListener(workerCreated, message => { + this.registerWorkerMessageListener(workerCreated, async message => { const tasksInProgress = this.tasks.get(workerCreated) if ( isKillBehavior(KillBehaviors.HARD, message.kill) || tasksInProgress === 0 ) { // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - void this.destroyWorker(workerCreated) + await this.destroyWorker(workerCreated) } }) return workerCreated }, - opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN + this.opts.workerChoiceStrategy ) } @@ -220,6 +215,8 @@ export abstract class AbstractPool< } private checkPoolOptions (opts: PoolOptions): void { + this.opts.workerChoiceStrategy = + opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN this.opts.enableEvents = opts.enableEvents ?? true } @@ -266,10 +263,9 @@ export abstract class AbstractPool< public execute (data: Data): Promise { // Configure worker to handle message with the specified task const worker = this.chooseWorker() - this.increaseWorkersTask(worker) - this.checkAndEmitBusy() const messageId = ++this.nextMessageId const res = this.internalExecute(worker, messageId) + this.checkAndEmitBusy() this.sendToWorker(worker, { data: data || ({} as Data), id: messageId }) return res } @@ -380,6 +376,7 @@ export abstract class AbstractPool< worker: Worker, messageId: number ): Promise { + this.increaseWorkersTask(worker) return new Promise((resolve, reject) => { this.promiseMap.set(messageId, { resolve, reject, worker }) })