From: Jérôme Benoit Date: Tue, 11 Apr 2023 20:28:10 +0000 (+0200) Subject: docs: merge pool internal interface to public X-Git-Tag: v2.4.7~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=c4855468bc26a7ee37d2c8ef34bb1ac864448e77;p=poolifier.git docs: merge pool internal interface to public Signed-off-by: Jérôme Benoit --- diff --git a/src/index.ts b/src/index.ts index ad0af487..507fc86f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 3f6e7034..1237491b 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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 { +> implements IPool { /** @inheritDoc */ public readonly workerNodes: Array> = [] diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 89180f1f..92f0068e 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -1,4 +1,4 @@ -import { PoolType } from '../pool-internal' +import { PoolType } from '../pool' import type { ClusterPoolOptions } from './fixed' import { FixedClusterPool } from './fixed' diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index eef394ec..e29c8c0f 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -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 index b245e360..00000000 --- a/src/pools/pool-internal.ts +++ /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 { - /** - * Pool worker nodes. - */ - readonly workerNodes: Array> - - /** - * 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 -} diff --git a/src/pools/pool.ts b/src/pools/pool.ts index fe03741a..92e399ed 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -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 { /** * 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 { +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> /** * Emitter on which events can be listened to. * @@ -106,6 +138,28 @@ export interface IPool { * - `'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. * diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index d60d2905..fd9cf1fb 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -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, + protected readonly pool: IPool, protected readonly opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { this.checkOptions(this.opts) diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index c2782e8b..f4bac5d7 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -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, + pool: IPool, opts?: WorkerChoiceStrategyOptions ) { super(pool, opts) diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 486668a2..34aa332e 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -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, + pool: IPool, private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 05b61684..0f901f7f 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -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' diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index bc665bf1..55c35a57 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -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.