From: Jérôme Benoit Date: Sun, 8 Sep 2024 14:46:01 +0000 (+0200) Subject: perf: track dynamic pool empty event lifecycle X-Git-Tag: v4.2.11~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=7923fe59f4a88b218744b16f977faf93015407ac;p=poolifier.git perf: track dynamic pool empty event lifecycle Signed-off-by: Jérôme Benoit --- 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 } } }