From: Jérôme Benoit Date: Fri, 7 Jul 2023 17:33:38 +0000 (+0200) Subject: refactor: cleanup message passing code X-Git-Tag: v2.6.10~14 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=48487131ad37630e3021c3e4feee1b311d8bcd11;p=poolifier.git refactor: cleanup message passing code Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 097882b7..48d0ed9a 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -957,7 +957,7 @@ export abstract class AbstractPool< void (this.destroyWorker(worker) as Promise) } }) - this.sendToWorker(worker, { dynamic: true }) + this.sendToWorker(worker, { checkAlive: true }) return worker } diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index fe920f8a..3c69ec2f 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -61,7 +61,7 @@ export class FixedClusterPool< /** @inheritDoc */ protected destroyWorker (worker: Worker): void { - this.sendToWorker(worker, { kill: 1 }) + this.sendToWorker(worker, { kill: true }) worker.on('disconnect', () => { worker.kill() }) diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 9861a3c1..29f00880 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -55,7 +55,7 @@ export class FixedThreadPool< /** @inheritDoc */ protected async destroyWorker (worker: Worker): Promise { - this.sendToWorker(worker, { kill: 1 }) + this.sendToWorker(worker, { kill: true }) await worker.terminate() } diff --git a/src/utility-types.ts b/src/utility-types.ts index 980c0089..20352350 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -68,7 +68,7 @@ export interface MessageValue /** * Kill code. */ - readonly kill?: KillBehavior | 1 + readonly kill?: KillBehavior | true /** * Task error. */ @@ -86,9 +86,9 @@ export interface MessageValue */ readonly started?: boolean /** - * Whether the worker is dynamic or not. + * Whether the worker starts or stops its aliveness check. */ - readonly dynamic?: boolean + readonly checkAlive?: boolean } /** diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index ba74409e..92d68bcb 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -148,9 +148,9 @@ export abstract class AbstractWorker< if (message.statistics != null) { // Statistics message received this.statistics = message.statistics - } else if (message.dynamic === true) { - // Worker dynamic message received - this.startCheckAlive() + } else if (message.checkAlive != null) { + // Check alive message received + message.checkAlive ? this.startCheckAlive() : this.stopCheckAlive() } else if (message.id != null && message.data != null) { // Task message received const fn = this.getTaskFunction(message.name) @@ -159,13 +159,16 @@ export abstract class AbstractWorker< } else { this.runInAsyncScope(this.runSync.bind(this), this, fn, message) } - } else if (message.kill != null) { + } else if (message.kill === true) { // Kill message received - this.aliveInterval != null && clearInterval(this.aliveInterval) + this.stopCheckAlive() this.emitDestroy() } } + /** + * Starts the worker alive check interval. + */ private startCheckAlive (): void { this.lastTaskTimestamp = performance.now() this.aliveInterval = setInterval( @@ -175,6 +178,25 @@ export abstract class AbstractWorker< this.checkAlive.bind(this)() } + /** + * Stops the worker alive check interval. + */ + private stopCheckAlive (): void { + this.aliveInterval != null && clearInterval(this.aliveInterval) + } + + /** + * Checks if the worker should be terminated, because its living too long. + */ + private checkAlive (): void { + if ( + performance.now() - this.lastTaskTimestamp > + (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) + ) { + this.sendToMainWorker({ kill: this.opts.killBehavior }) + } + } + /** * Returns the main worker. * @@ -196,18 +218,6 @@ export abstract class AbstractWorker< message: MessageValue ): void - /** - * Checks if the worker should be terminated, because its living too long. - */ - protected checkAlive (): void { - if ( - performance.now() - this.lastTaskTimestamp > - (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) - ) { - this.sendToMainWorker({ kill: this.opts.killBehavior }) - } - } - /** * Handles an error and convert it to a string so it can be sent back to the main worker. *