X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fabstract-pool.ts;h=3be40e6d9cc8dd2634ab02569abd6261dca6f326;hb=f82cd3579677f83c17289218d4ae8242e7d0a8d7;hp=6462cb09a882c6c005ceea481cacb5519e2de0de;hpb=bbeadd16bc03b9199221a7fb5732af46e7867ded;p=poolifier.git diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 6462cb09..3be40e6d 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 } 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, @@ -23,13 +27,13 @@ import { CircularArray } from '../circular-array' * * @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. + * @typeParam Response - Type of execution response. This can only be serializable data. */ export abstract class AbstractPool< Worker extends IWorker, Data = unknown, Response = unknown -> implements IPoolInternal { +> implements IPool { /** @inheritDoc */ public readonly workerNodes: Array> = [] @@ -139,6 +143,18 @@ export abstract class AbstractPool< opts.workerChoiceStrategyOptions ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS this.opts.enableEvents = opts.enableEvents ?? true this.opts.enableTasksQueue = opts.enableTasksQueue ?? false + if (this.opts.enableTasksQueue) { + if ((opts.tasksQueueOptions?.concurrency as number) <= 0) { + throw new Error( + `Invalid worker tasks concurrency '${ + (opts.tasksQueueOptions as TasksQueueOptions).concurrency as number + }'` + ) + } + this.opts.tasksQueueOptions = { + concurrency: opts.tasksQueueOptions?.concurrency ?? 1 + } + } } private checkValidWorkerChoiceStrategy ( @@ -211,11 +227,19 @@ export abstract class AbstractPool< ) } - /** @inheritDoc */ - public abstract get full (): boolean + /** + * Whether the pool is full or not. + * + * The pool filling boolean status. + */ + protected abstract get full (): boolean - /** @inheritDoc */ - public abstract get busy (): boolean + /** + * Whether the pool is busy or not. + * + * The pool busyness boolean status. + */ + protected abstract get busy (): boolean protected internalBusy (): boolean { return this.findFreeWorkerNodeKey() === -1 @@ -237,7 +261,7 @@ export abstract class AbstractPool< id: crypto.randomUUID() } const res = new Promise((resolve, reject) => { - this.promiseResponseMap.set(submittedTask.id, { + this.promiseResponseMap.set(submittedTask.id as string, { resolve, reject, worker: workerNode.worker @@ -245,7 +269,10 @@ export abstract class AbstractPool< }) if ( this.opts.enableTasksQueue === true && - (this.busy || this.workerNodes[workerNodeKey].tasksUsage.running > 0) + (this.busy || + this.workerNodes[workerNodeKey].tasksUsage.running >= + ((this.opts.tasksQueueOptions as TasksQueueOptions) + .concurrency as number)) ) { this.enqueueTask(workerNodeKey, submittedTask) } else { @@ -479,6 +506,7 @@ export abstract class AbstractPool< * Gets the given worker its tasks usage in the pool. * * @param worker - The worker. + * @throws {@link Error} if the worker is not found in the pool worker nodes. * @returns The worker tasks usage. */ private getWorkerTasksUsage (worker: Worker): TasksUsage | undefined {