docs: merge pool internal interface to public
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 11 Apr 2023 20:28:10 +0000 (22:28 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 11 Apr 2023 20:28:10 +0000 (22:28 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/index.ts
src/pools/abstract-pool.ts
src/pools/cluster/dynamic.ts
src/pools/cluster/fixed.ts
src/pools/pool-internal.ts [deleted file]
src/pools/pool.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
src/pools/thread/dynamic.ts
src/pools/thread/fixed.ts

index ad0af4875b64cf176ba7718fec69d60403412735..507fc86ffacae84875972e78ef02ef51f92f46c5 100644 (file)
@@ -1,6 +1,7 @@
 export { DynamicClusterPool } from './pools/cluster/dynamic'
 export { FixedClusterPool } from './pools/cluster/fixed'
 export type { ClusterPoolOptions } from './pools/cluster/fixed'
+export type { AbstractPool } from './pools/abstract-pool'
 export { PoolEvents } from './pools/pool'
 export type {
   IPool,
@@ -14,17 +15,22 @@ export type {
   ExitHandler,
   IWorker,
   MessageHandler,
-  OnlineHandler
+  OnlineHandler,
+  WorkerNode
 } from './pools/worker'
 export { WorkerChoiceStrategies } from './pools/selection-strategies/selection-strategies-types'
 export type {
+  IWorkerChoiceStrategy,
   WorkerChoiceStrategy,
   WorkerChoiceStrategyOptions
 } from './pools/selection-strategies/selection-strategies-types'
+export type { WorkerChoiceStrategyContext } from './pools/selection-strategies/worker-choice-strategy-context'
 export { DynamicThreadPool } from './pools/thread/dynamic'
 export { FixedThreadPool } from './pools/thread/fixed'
 export type { ThreadWorkerWithMessageChannel } from './pools/thread/fixed'
+export type { AbstractWorker } from './worker/abstract-worker'
 export { ClusterWorker } from './worker/cluster-worker'
 export { ThreadWorker } from './worker/thread-worker'
 export { KillBehaviors } from './worker/worker-options'
 export type { KillBehavior, WorkerOptions } from './worker/worker-options'
+export type { Draft, MessageValue } from './utility-types'
index 3f6e7034f4c6994d4177038cd5c89bf1f3b26636..1237491b94df3dc62bc79a056bed64bb9643edd4 100644 (file)
@@ -6,10 +6,14 @@ import {
   median
 } from '../utils'
 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
-import { PoolEvents, type PoolOptions, type TasksQueueOptions } from './pool'
+import {
+  PoolEvents,
+  type IPool,
+  type PoolOptions,
+  type TasksQueueOptions,
+  PoolType
+} from './pool'
 import { PoolEmitter } from './pool'
-import type { IPoolInternal } from './pool-internal'
-import { PoolType } from './pool-internal'
 import type { IWorker, Task, TasksUsage, WorkerNode } from './worker'
 import {
   WorkerChoiceStrategies,
@@ -29,7 +33,7 @@ export abstract class AbstractPool<
   Worker extends IWorker,
   Data = unknown,
   Response = unknown
-> implements IPoolInternal<Worker, Data, Response> {
+> implements IPool<Worker, Data, Response> {
   /** @inheritDoc */
   public readonly workerNodes: Array<WorkerNode<Worker, Data>> = []
 
index 89180f1f161d9351190189ff06cd7e76a40f30d5..92f0068e0bad123633b62798f97435a87a0f010b 100644 (file)
@@ -1,4 +1,4 @@
-import { PoolType } from '../pool-internal'
+import { PoolType } from '../pool'
 import type { ClusterPoolOptions } from './fixed'
 import { FixedClusterPool } from './fixed'
 
index eef394ece07192bcb7b4bad60a6d1c534d0b2e96..e29c8c0faa1720af8c5c43bab67b5bc031a90251 100644 (file)
@@ -3,7 +3,7 @@ import cluster from 'node:cluster'
 import type { MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
 import type { PoolOptions } from '../pool'
-import { PoolType } from '../pool-internal'
+import { PoolType } from '../pool'
 
 /**
  * Options for a poolifier cluster pool.
diff --git a/src/pools/pool-internal.ts b/src/pools/pool-internal.ts
deleted file mode 100644 (file)
index b245e36..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-import type { IPool } from './pool'
-import type { IWorker, WorkerNode } from './worker'
-
-/**
- * Internal pool types.
- *
- * @enum
- */
-export enum PoolType {
-  /**
-   * Fixed pool type.
-   */
-  FIXED = 'fixed',
-  /**
-   * Dynamic pool type.
-   */
-  DYNAMIC = 'dynamic'
-}
-
-/**
- * Internal contract definition for a poolifier pool.
- *
- * @typeParam Worker - Type of worker which manages this pool.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
- */
-export interface IPoolInternal<
-  Worker extends IWorker,
-  Data = unknown,
-  Response = unknown
-> extends IPool<Data, Response> {
-  /**
-   * Pool worker nodes.
-   */
-  readonly workerNodes: Array<WorkerNode<Worker, Data>>
-
-  /**
-   * Pool type.
-   *
-   * If it is `'dynamic'`, it provides the `max` property.
-   */
-  readonly type: PoolType
-
-  /**
-   * Whether the pool is full or not.
-   *
-   * The pool filling boolean status.
-   */
-  readonly full: boolean
-
-  /**
-   * Whether the pool is busy or not.
-   *
-   * The pool busyness boolean status.
-   */
-  readonly busy: boolean
-
-  /**
-   * Finds a free worker node key based on the number of tasks the worker has applied.
-   *
-   * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
-   *
-   * If no free worker is found, `-1` is returned.
-   *
-   * @returns A worker node key if there is one, `-1` otherwise.
-   */
-  findFreeWorkerNodeKey: () => number
-}
index fe03741a68254c686991c5ed6b222857e1ff70c8..92e399edf37bc51f2af2a45db428851f2e63be18 100644 (file)
@@ -4,13 +4,30 @@ import type {
   ExitHandler,
   IWorker,
   MessageHandler,
-  OnlineHandler
+  OnlineHandler,
+  WorkerNode
 } from './worker'
 import type {
   WorkerChoiceStrategy,
   WorkerChoiceStrategyOptions
 } from './selection-strategies/selection-strategies-types'
 
+/**
+ * Pool types.
+ *
+ * @enum
+ */
+export enum PoolType {
+  /**
+   * Fixed pool type.
+   */
+  FIXED = 'fixed',
+  /**
+   * Dynamic pool type.
+   */
+  DYNAMIC = 'dynamic'
+}
+
 /**
  * Pool events emitter.
  */
@@ -93,10 +110,25 @@ export interface PoolOptions<Worker extends IWorker> {
 /**
  * Contract definition for a poolifier pool.
  *
+ * @typeParam Worker - Type of worker which manages this pool.
  * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
  * @typeParam Response - Type of response of execution. This can only be serializable data.
  */
-export interface IPool<Data = unknown, Response = unknown> {
+export interface IPool<
+  Worker extends IWorker,
+  Data = unknown,
+  Response = unknown
+> {
+  /**
+   * Pool type.
+   *
+   * If it is `'dynamic'`, it provides the `max` property.
+   */
+  readonly type: PoolType
+  /**
+   * Pool worker nodes.
+   */
+  readonly workerNodes: Array<WorkerNode<Worker, Data>>
   /**
    * Emitter on which events can be listened to.
    *
@@ -106,6 +138,28 @@ export interface IPool<Data = unknown, Response = unknown> {
    * - `'busy'`: Emitted when the pool is busy.
    */
   readonly emitter?: PoolEmitter
+  /**
+   * Whether the pool is full or not.
+   *
+   * The pool filling boolean status.
+   */
+  readonly full: boolean
+  /**
+   * Whether the pool is busy or not.
+   *
+   * The pool busyness boolean status.
+   */
+  readonly busy: boolean
+  /**
+   * Finds a free worker node key based on the number of tasks the worker has applied.
+   *
+   * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
+   *
+   * If no free worker is found, `-1` is returned.
+   *
+   * @returns A worker node key if there is one, `-1` otherwise.
+   */
+  findFreeWorkerNodeKey: () => number
   /**
    * Performs the task specified in the constructor with the data parameter.
    *
index d60d2905674e5b6fc5baa66616a63e9ab9f1fc85..fd9cf1fb8b3f0499ed8c7615e805c1fc4ebffa31 100644 (file)
@@ -1,6 +1,5 @@
 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
-import type { IPoolInternal } from '../pool-internal'
-import { PoolType } from '../pool-internal'
+import { PoolType, type IPool } from '../pool'
 import type { IWorker } from '../worker'
 import type {
   IWorkerChoiceStrategy,
@@ -36,7 +35,7 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @param opts - The worker choice strategy options.
    */
   public constructor (
-    protected readonly pool: IPoolInternal<Worker, Data, Response>,
+    protected readonly pool: IPool<Worker, Data, Response>,
     protected readonly opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
     this.checkOptions(this.opts)
index c2782e8b2bf844b1a767eafcfe8554b0fc568ec7..f4bac5d76c4e3b669bb5720b99c03b07eabc577d 100644 (file)
@@ -1,5 +1,4 @@
 import { cpus } from 'node:os'
-import type { IPoolInternal } from '../pool-internal'
 import type { IWorker } from '../worker'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
 import type {
@@ -7,6 +6,7 @@ import type {
   RequiredStatistics,
   WorkerChoiceStrategyOptions
 } from './selection-strategies-types'
+import type { IPool } from '../pool'
 
 /**
  * Virtual task runtime.
@@ -61,7 +61,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
    * @param opts - The worker choice strategy options.
    */
   public constructor (
-    pool: IPoolInternal<Worker, Data, Response>,
+    pool: IPool<Worker, Data, Response>,
     opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
index 486668a2f80d49d80a70f00528a7c8b4ad33467a..34aa332e39677883c066c9bd823276e8e56e886c 100644 (file)
@@ -1,5 +1,5 @@
 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
-import type { IPoolInternal } from '../pool-internal'
+import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
 import { LessBusyWorkerChoiceStrategy } from './less-busy-worker-choice-strategy'
@@ -39,7 +39,7 @@ export class WorkerChoiceStrategyContext<
    * @param opts - The worker choice strategy options.
    */
   public constructor (
-    pool: IPoolInternal<Worker, Data, Response>,
+    pool: IPool<Worker, Data, Response>,
     private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
     opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
   ) {
index 05b61684173af58c7ed7dbbc52218c874a35fa7a..0f901f7fdfc674b3c7761db870cf3ae29358c6ef 100644 (file)
@@ -1,5 +1,5 @@
 import type { PoolOptions } from '../pool'
-import { PoolType } from '../pool-internal'
+import { PoolType } from '../pool'
 import type { ThreadWorkerWithMessageChannel } from './fixed'
 import { FixedThreadPool } from './fixed'
 
index bc665bf1a5f13e6c136a7281b96ffc465e470c03..55c35a57122433ea5c2a43d3e825f7887eb9aa3a 100644 (file)
@@ -7,7 +7,7 @@ import {
 import type { Draft, MessageValue } from '../../utility-types'
 import { AbstractPool } from '../abstract-pool'
 import type { PoolOptions } from '../pool'
-import { PoolType } from '../pool-internal'
+import { PoolType } from '../pool'
 
 /**
  * A thread worker with message channels for communication between main thread and thread worker.