perf: use a single map to store pool workers and their related data
[poolifier.git] / src / pools / pool-internal.ts
index ddee27eac0ed21bf43ca4f61b8378ee3a3146b6a..97e5d5d2d56ae75435b19d1ccb8afae10ff2e8ed 100644 (file)
@@ -1,81 +1,94 @@
-import EventEmitter from 'events'
-import type { MessageValue } from '../utility-types'
-import type { IWorker } from './abstract-pool'
 import type { IPool } from './pool'
+import type { IPoolWorker } from './pool-worker'
 
 /**
- * Internal poolifier pool emitter.
+ * Internal pool types.
  */
-export class PoolEmitter extends EventEmitter {}
+export enum PoolType {
+  FIXED = 'fixed',
+  DYNAMIC = 'dynamic'
+}
+
+/**
+ * Internal tasks usage statistics.
+ */
+export interface TasksUsage {
+  run: number
+  running: number
+  runTime: number
+  avgRunTime: number
+}
+
+/**
+ * Internal worker type.
+ *
+ * @typeParam Worker - Type of worker which manages this pool.
+ */
+export interface WorkerType<Worker extends IPoolWorker> {
+  worker: Worker
+  tasksUsage: TasksUsage
+}
 
 /**
  * Internal contract definition for a poolifier pool.
  *
- * @template Worker Type of worker which manages this pool.
- * @template Data Type of data sent to the worker.
- * @template Response Type of response of execution.
+ * @typeParam Worker - Type of worker which manages this pool.
+ * @typeParam Data - Type of data sent to the worker.
+ * @typeParam Response - Type of response of execution.
  */
 export interface IPoolInternal<
-  Worker extends IWorker,
+  Worker extends IPoolWorker,
   Data = unknown,
   Response = unknown
 > extends IPool<Data, Response> {
   /**
-   * List of currently available workers.
+   * Map of workers.
    */
-  readonly workers: Worker[]
+  readonly workers: Map<number, WorkerType<Worker>>
 
   /**
-   * The tasks map.
+   * Pool type.
    *
-   * - `key`: The `Worker`
-   * - `value`: Number of tasks currently in progress on the worker.
+   * If it is `'dynamic'`, it provides the `max` property.
    */
-  readonly tasks: Map<Worker, number>
+  readonly type: PoolType
 
   /**
-   * Emitter on which events can be listened to.
+   * Whether the pool is busy or not.
    *
-   * Events that can currently be listened to:
-   *
-   * - `'FullPool'`
+   * The pool busyness boolean status.
    */
-  readonly emitter: PoolEmitter
+  readonly busy: boolean
 
   /**
-   * Maximum number of workers that can be created by this pool.
+   * Number of tasks currently concurrently running.
    */
-  readonly max?: number
+  readonly numberOfRunningTasks: number
 
   /**
-   * Whether the pool is dynamic or not.
+   * Finds a free worker based on the number of tasks the worker has applied.
    *
-   * If it is dynamic, it provides the `max` property.
-   */
-  isDynamic(): boolean
-
-  /**
-   * Creates a new worker for this pool and sets it up completely.
+   * If a worker is found with `0` running tasks, it is detected as free and returned.
+   *
+   * If no free worker is found, `false` is returned.
    *
-   * @returns New, completely set up worker.
+   * @returns A free worker if there is one, otherwise `false`.
    */
-  createAndSetupWorker(): Worker
+  findFreeWorker: () => Worker | false
 
   /**
-   * Shut down given worker.
+   * Gets worker running tasks.
    *
-   * @param worker A worker within `workers`.
+   * @param worker - The worker.
+   * @returns The number of tasks currently running on the worker.
    */
-  destroyWorker(worker: Worker): void | Promise<void>
+  getWorkerRunningTasks: (worker: Worker) => number | undefined
 
   /**
-   * Register a listener callback on a given worker.
+   * Gets worker average tasks runtime.
    *
-   * @param worker A worker.
-   * @param listener A message listener callback.
+   * @param worker - The worker.
+   * @returns The average tasks runtime on the worker.
    */
-  registerWorkerMessageListener<Message extends Data | Response>(
-    worker: Worker,
-    listener: (message: MessageValue<Message>) => void
-  ): void
+  getWorkerAverageTasksRunTime: (worker: Worker) => number | undefined
 }