feat: add pool runtime setters
[poolifier.git] / src / pools / selection-strategies / selection-strategies-types.ts
index 4d302f3defb1e7d5619d37693daf4104c944f377..f9a504351a4bbbe9d33bddc145c457a7ce27ef78 100644 (file)
@@ -1,5 +1,3 @@
-import type { AbstractPoolWorker } from '../abstract-pool-worker'
-
 /**
  * Enumeration of worker choice strategies.
  */
@@ -9,9 +7,13 @@ export const WorkerChoiceStrategies = Object.freeze({
    */
   ROUND_ROBIN: 'ROUND_ROBIN',
   /**
-   * Less recently used worker selection strategy.
+   * Less used worker selection strategy.
+   */
+  LESS_USED: 'LESS_USED',
+  /**
+   * Less busy worker selection strategy.
    */
-  LESS_RECENTLY_USED: 'LESS_RECENTLY_USED',
+  LESS_BUSY: 'LESS_BUSY',
   /**
    * Fair share worker selection strategy.
    */
@@ -28,28 +30,61 @@ export const WorkerChoiceStrategies = Object.freeze({
 export type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies
 
 /**
- * Tasks usage statistics requirements.
+ * Worker choice strategy options.
  */
-export type RequiredStatistics = {
-  runTime: boolean
+export interface WorkerChoiceStrategyOptions {
+  /**
+   * Use tasks median run time instead of average run time.
+   */
+  medRunTime?: boolean
 }
 
 /**
- * Worker choice strategy interface.
+ * Pool worker tasks usage statistics requirements.
  *
- * @template Worker Type of worker which manages the strategy.
+ * @internal
  */
-export interface IWorkerChoiceStrategy<Worker extends AbstractPoolWorker> {
+export interface RequiredStatistics {
+  /**
+   * Require tasks run time.
+   */
+  runTime: boolean
   /**
-   * Is the pool attached to the strategy dynamic?.
+   * Require tasks average run time.
    */
-  isDynamicPool: boolean
+  avgRunTime: boolean
+  /**
+   * Require tasks median run time.
+   */
+  medRunTime: boolean
+}
+
+/**
+ * Worker choice strategy interface.
+ */
+export interface IWorkerChoiceStrategy {
   /**
    * Required tasks usage statistics.
    */
-  requiredStatistics: RequiredStatistics
+  readonly requiredStatistics: RequiredStatistics
+  /**
+   * Resets strategy internals (counters, statistics, etc.).
+   */
+  reset: () => boolean
+  /**
+   * Chooses a worker node in the pool and returns its key.
+   */
+  choose: () => number
+  /**
+   * Removes a worker node key from strategy internals.
+   *
+   * @param workerNodeKey - The worker node key.
+   */
+  remove: (workerNodeKey: number) => boolean
   /**
-   * Choose a worker in the pool.
+   * Sets the worker choice strategy options.
+   *
+   * @param opts - The worker choice strategy options.
    */
-  choose(): Worker
+  setOptions: (opts: WorkerChoiceStrategyOptions) => void
 }