Removed max tasks (#225)
[poolifier.git] / src / pools / abstract-pool.ts
index c123849e6bc2aff5844185be05303f9b15bd36d6..9d7e4f0c2306ad2dd9970cc7ac7cd9094959b136 100644 (file)
@@ -82,15 +82,6 @@ export interface PoolOptions<Worker> {
    * A function that will listen for exit event on each worker.
    */
   exitHandler?: ExitHandler<Worker>
-  /**
-   * This is just to avoid non-useful warning messages.
-   *
-   * Will be used to set `maxListeners` on event emitters (workers are event emitters).
-   *
-   * @default 1000
-   * @see [Node events emitter.setMaxListeners(n)](https://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n)
-   */
-  maxTasks?: number
   /**
    * The work choice strategy to use in this pool.
    */
@@ -152,16 +143,17 @@ export abstract class AbstractPool<
    *
    * @param numberOfWorkers Number of workers that this pool should manage.
    * @param filePath Path to the worker-file.
-   * @param opts Options for the pool. Default: `{ maxTasks: 1000 }`
+   * @param opts Options for the pool.
    */
   public constructor (
     public readonly numberOfWorkers: number,
     public readonly filePath: string,
-    public readonly opts: PoolOptions<Worker> = { maxTasks: 1000 }
+    public readonly opts: PoolOptions<Worker>
   ) {
     if (!this.isMain()) {
       throw new Error('Cannot start a pool from a worker!')
     }
+    this.checkNumberOfWorkers(this.numberOfWorkers)
     this.checkFilePath(this.filePath)
     this.setupHook()
 
@@ -182,6 +174,24 @@ export abstract class AbstractPool<
     }
   }
 
+  private checkNumberOfWorkers (numberOfWorkers: number): void {
+    if (numberOfWorkers == null) {
+      throw new Error(
+        'Cannot instantiate a pool without specifying the number of workers'
+      )
+    } else if (!Number.isSafeInteger(numberOfWorkers)) {
+      throw new Error(
+        'Cannot instantiate a pool with a non integer number of workers'
+      )
+    } else if (numberOfWorkers < 0) {
+      throw new Error(
+        'Cannot instantiate a pool with a negative number of workers'
+      )
+    } else if (!this.isDynamic() && numberOfWorkers === 0) {
+      throw new Error('Cannot instantiate a fixed pool with no worker')
+    }
+  }
+
   /** @inheritdoc */
   public isDynamic (): boolean {
     return false
@@ -191,6 +201,7 @@ export abstract class AbstractPool<
   public setWorkerChoiceStrategy (
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
+    this.opts.workerChoiceStrategy = workerChoiceStrategy
     this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
       workerChoiceStrategy
     )