X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fthread%2Fdynamic.ts;h=836cae0b0615e1f74733e09f9f21b450bf15506b;hb=61ee03c80d4de04751bf0218d9574bbd8c94e159;hp=b0a22346690bd36c471b6b45c844af8ffae098ea;hpb=c426b04af6b85ef3ec6859929eb906bcea4fcaf7;p=poolifier.git diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index b0a22346..836cae0b 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -1,7 +1,6 @@ -import { type Worker } from 'node:worker_threads' -import { type PoolOptions, type PoolType, PoolTypes } from '../pool' -import { checkDynamicPoolSize } from '../utils' -import { FixedThreadPool } from './fixed' +import { PoolEvents, type PoolType, PoolTypes } from '../pool.js' +import { checkDynamicPoolSize } from '../utils.js' +import { FixedThreadPool, type ThreadPoolOptions } from './fixed.js' /** * A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads. @@ -30,15 +29,31 @@ export class DynamicThreadPool< min: number, max: number, filePath: string, - opts: PoolOptions = {} + opts: ThreadPoolOptions = {} ) { super(min, filePath, opts, max) checkDynamicPoolSize( this.minimumNumberOfWorkers, - this.maximumNumberOfWorkers as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.maximumNumberOfWorkers! ) } + /** @inheritDoc */ + protected shallCreateDynamicWorker (): boolean { + return ( + (!this.full && this.internalBusy()) || + (this.minimumNumberOfWorkers === 0 && this.workerNodes.length === 0) + ) + } + + /** @inheritDoc */ + protected checkAndEmitDynamicWorkerCreationEvents (): void { + if (this.full) { + this.emitter?.emit(PoolEvents.full, this.info) + } + } + /** @inheritDoc */ protected get type (): PoolType { return PoolTypes.dynamic