perf: track dynamic pool empty event lifecycle
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 8 Sep 2024 14:46:01 +0000 (16:46 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 8 Sep 2024 14:46:01 +0000 (16:46 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/cluster/dynamic.ts
src/pools/thread/dynamic.ts

index 4ebd63317c3319af30a3ffefc355aa384d6b0a18..e52e91db5aa9ec76a6ee4f6c0625a9c45ef27839 100644 (file)
@@ -16,6 +16,11 @@ export class DynamicClusterPool<
   Data = unknown,
   Response = unknown
 > extends FixedClusterPool<Data, Response> {
+  /**
+   * 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
       }
     }
   }
index 086f8733cebe00c0398b3cb57308ff507960a564..b184da93a250d538b52cac4c783b5e3275dd5394 100644 (file)
@@ -16,6 +16,11 @@ export class DynamicThreadPool<
   Data = unknown,
   Response = unknown
 > extends FixedThreadPool<Data, Response> {
+  /**
+   * 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
       }
     }
   }