X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=dc49dc8f6edada76925ddb66ae27b0224c9cce8c;hb=5519f8f39ad88a04da95fb6225e61dbdfbab002f;hp=54447c82ba2db2e9a88503d6e670f73022a143bc;hpb=e843b9042b77e0e4e17c193820a3e05ffc92cffe;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 54447c82..dc49dc8f 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. */ @@ -179,14 +173,14 @@ 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 @@ -268,10 +262,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 } @@ -382,6 +375,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 }) })