From 7923fe59f4a88b218744b16f977faf93015407ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 8 Sep 2024 16:46:01 +0200 Subject: [PATCH] perf: track dynamic pool empty event lifecycle MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/cluster/dynamic.ts | 20 ++++++++++++++++---- src/pools/thread/dynamic.ts | 20 ++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 4ebd6331..e52e91db 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -16,6 +16,11 @@ export class DynamicClusterPool< Data = unknown, Response = unknown > extends FixedClusterPool { + /** + * Whether the pool empty event has been emitted or not + */ + private emptyEventEmitted: boolean + /** * Whether the pool full event has been emitted or not. */ @@ -39,14 +44,20 @@ export class DynamicClusterPool< this.minimumNumberOfWorkers, this.maximumNumberOfWorkers ) + this.emptyEventEmitted = false this.fullEventEmitted = false } /** @inheritDoc */ protected checkAndEmitDynamicWorkerCreationEvents (): void { - if (this.emitter != null && !this.fullEventEmitted && this.full) { - this.emitter.emit(PoolEvents.full, this.info) - this.fullEventEmitted = true + if (this.emitter != null) { + if (!this.fullEventEmitted && this.full) { + this.emitter.emit(PoolEvents.full, this.info) + this.fullEventEmitted = true + } + if (this.emptyEventEmitted && !this.empty) { + this.emptyEventEmitted = false + } } } @@ -57,8 +68,9 @@ export class DynamicClusterPool< this.emitter.emit(PoolEvents.fullEnd, this.info) this.fullEventEmitted = false } - if (this.empty) { + if (!this.emptyEventEmitted && this.empty) { this.emitter.emit(PoolEvents.empty, this.info) + this.emptyEventEmitted = true } } } diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 086f8733..b184da93 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -16,6 +16,11 @@ export class DynamicThreadPool< Data = unknown, Response = unknown > extends FixedThreadPool { + /** + * Whether the pool empty event has been emitted or not + */ + private emptyEventEmitted: boolean + /** * Whether the pool full event has been emitted or not. */ @@ -39,14 +44,20 @@ export class DynamicThreadPool< this.minimumNumberOfWorkers, this.maximumNumberOfWorkers ) + this.emptyEventEmitted = false this.fullEventEmitted = false } /** @inheritDoc */ protected checkAndEmitDynamicWorkerCreationEvents (): void { - if (this.emitter != null && !this.fullEventEmitted && this.full) { - this.emitter.emit(PoolEvents.full, this.info) - this.fullEventEmitted = true + if (this.emitter != null) { + if (!this.fullEventEmitted && this.full) { + this.emitter.emit(PoolEvents.full, this.info) + this.fullEventEmitted = true + } + if (this.emptyEventEmitted && !this.empty) { + this.emptyEventEmitted = false + } } } @@ -57,8 +68,9 @@ export class DynamicThreadPool< this.emitter.emit(PoolEvents.fullEnd, this.info) this.fullEventEmitted = false } - if (this.empty) { + if (!this.emptyEventEmitted && this.empty) { this.emitter.emit(PoolEvents.empty, this.info) + this.emptyEventEmitted = true } } } -- 2.34.1