Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/fastify-hybrid...
[poolifier.git] / src / pools / cluster / dynamic.ts
index a9ccd6977255bcadc43bee150d8684a3ee3ad75e..51d1cbeece56d374df9228cbee9dbdfed86f1989 100644 (file)
@@ -1,5 +1,6 @@
-import { type PoolType, PoolTypes } from '../pool'
-import { type ClusterPoolOptions, FixedClusterPool } from './fixed'
+import { PoolEvents, type PoolType, PoolTypes } from '../pool.js'
+import { checkDynamicPoolSize } from '../utils.js'
+import { type ClusterPoolOptions, FixedClusterPool } from './fixed.js'
 
 /**
  * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
@@ -26,21 +27,32 @@ export class DynamicClusterPool<
    */
   public constructor (
     min: number,
-    protected readonly max: number,
+    max: number,
     filePath: string,
     opts: ClusterPoolOptions = {}
   ) {
-    super(min, filePath, opts)
+    super(min, filePath, opts, max)
+    checkDynamicPoolSize(
+      this.minimumNumberOfWorkers,
+      this.maximumNumberOfWorkers
+    )
   }
 
   /** @inheritDoc */
-  protected get type (): PoolType {
-    return PoolTypes.dynamic
+  protected shallCreateDynamicWorker (): boolean {
+    return (!this.full && this.internalBusy()) || this.empty
   }
 
   /** @inheritDoc */
-  protected get maxSize (): number {
-    return this.max
+  protected checkAndEmitDynamicWorkerCreationEvents (): void {
+    if (this.full) {
+      this.emitter?.emit(PoolEvents.full, this.info)
+    }
+  }
+
+  /** @inheritDoc */
+  protected get type (): PoolType {
+    return PoolTypes.dynamic
   }
 
   /** @inheritDoc */