From 7c89e6a4cb61feed3743298f9813871af02df875 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 21 Jul 2023 14:15:24 +0200 Subject: [PATCH] perf: speed up dynamic worker termination MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 2 +- src/pools/abstract-pool.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91530cd6..4bdbedb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Drastically reduce lookup by worker in the worker nodes. +- Drastically reduce lookups by worker in the worker nodes. ## [2.6.19] - 2023-07-20 diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 086d7ee8..09871491 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -10,6 +10,7 @@ import { DEFAULT_TASK_NAME, DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, EMPTY_FUNCTION, + isAsyncFunction, isKillBehavior, isPlainObject, median, @@ -913,7 +914,16 @@ export abstract class AbstractPool< this.tasksQueueSize(localWorkerNodeKey) === 0))) ) { // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime) - void (this.destroyWorkerNode(localWorkerNodeKey) as Promise) + const destroyWorkerNodeBounded = this.destroyWorkerNode.bind(this) + if (isAsyncFunction(destroyWorkerNodeBounded)) { + ( + destroyWorkerNodeBounded as (workerNodeKey: number) => Promise + )(localWorkerNodeKey).catch(EMPTY_FUNCTION) + } else { + (destroyWorkerNodeBounded as (workerNodeKey: number) => void)( + localWorkerNodeKey + ) + } } }) const workerInfo = this.getWorkerInfo(workerNodeKey) -- 2.34.1