X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=inline;f=src%2Fpools%2Fabstract-pool.ts;h=363db0e3f9b64ff650a0c351de05135c8de1de96;hb=fc3e65861bc1939ae047ee1e8e91a1ce577035f4;hp=1d4e7ae0d07f18ede013a5fefda5f7430302584c;hpb=5a94e4b950eaf2234e07f87261ddea1482e839c6;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 1d4e7ae0..363db0e3 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -1,3 +1,4 @@ +import crypto from 'node:crypto' import type { MessageValue, PromiseWorkerResponseWrapper @@ -50,14 +51,9 @@ export abstract class AbstractPool< * When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message. */ protected promiseMap: Map< - number, + string, PromiseWorkerResponseWrapper - > = new Map>() - - /** - * Id of the next message. - */ - protected nextMessageId: number = 0 + > = new Map>() /** * Worker choice strategy instance implementing the worker choice algorithm. @@ -158,7 +154,7 @@ export abstract class AbstractPool< } /** - * Gets worker key. + * Gets the given worker key. * * @param worker - The worker. * @returns The worker key. @@ -220,16 +216,15 @@ export abstract class AbstractPool< /** {@inheritDoc} */ public async execute (data: Data): Promise { - // Configure worker to handle message with the specified task const worker = this.chooseWorker() - const res = this.internalExecute(worker, this.nextMessageId) + const messageId = crypto.randomUUID() + const res = this.internalExecute(worker, messageId) this.checkAndEmitBusy() this.sendToWorker(worker, { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions data: data ?? ({} as Data), - id: this.nextMessageId + id: messageId }) - ++this.nextMessageId // eslint-disable-next-line @typescript-eslint/return-await return res } @@ -398,7 +393,7 @@ export abstract class AbstractPool< private async internalExecute ( worker: Worker, - messageId: number + messageId: string ): Promise { this.beforePromiseWorkerResponseHook(worker) return await new Promise((resolve, reject) => { @@ -431,7 +426,7 @@ export abstract class AbstractPool< } /** - * Get tasks usage of the given worker. + * Gets tasks usage of the given worker. * * @param worker - Worker which tasks usage is returned. */