'error',
{ ignoreProperties: true }
],
- 'tsdoc/syntax': 'error'
+ 'tsdoc/syntax': 'warn'
}
},
{
Data = unknown,
Response = unknown
> implements IPoolInternal<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly workers: Array<WorkerType<Worker>> = []
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly emitter?: PoolEmitter
/**
this.opts.enableEvents = opts.enableEvents ?? true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract get type (): PoolType
/**
return this.workers.findIndex(workerItem => workerItem.worker === worker)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public setWorkerChoiceStrategy (
workerChoiceStrategy: WorkerChoiceStrategy
): void {
)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract get full (): boolean
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract get busy (): boolean
protected internalBusy (): boolean {
)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public findFreeWorkerKey (): number {
return this.workers.findIndex(workerItem => {
return workerItem.tasksUsage.running === 0
})
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public async execute (data: Data): Promise<Response> {
const [workerKey, worker] = this.chooseWorker()
const messageId = crypto.randomUUID()
return res
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public async destroy (): Promise<void> {
await Promise.all(
this.workers.map(async workerItem => {
}
/**
- * Shutdowns given worker.
+ * Shutdowns given worker in the pool.
*
* @param worker - A worker within `workers`.
*/
/**
* Setup hook that can be overridden by a Poolifier pool implementation
* to run code before workers are created in the abstract constructor.
+ *
+ * @virtual
*/
protected setupHook (): void {
// Can be overridden
* Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
*
* @param worker - The newly created worker.
+ * @virtual
*/
protected abstract afterWorkerSetup (worker: Worker): void
}
/**
- * Gets worker tasks usage.
+ * Gets the given worker tasks usage in the pool.
*
* @param worker - The worker.
* @returns The worker tasks usage.
super(min, filePath, opts)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get type (): PoolType {
return PoolType.DYNAMIC
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get full (): boolean {
return this.workers.length === this.max
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get busy (): boolean {
return this.full && this.findFreeWorkerKey() === -1
}
super(numberOfWorkers, filePath, opts)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected setupHook (): void {
cluster.setupPrimary({ ...this.opts.settings, exec: this.filePath })
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected isMain (): boolean {
return cluster.isPrimary
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public destroyWorker (worker: Worker): void {
this.sendToWorker(worker, { kill: 1 })
worker.kill()
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
worker.send(message)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public registerWorkerMessageListener<Message extends Data | Response>(
worker: Worker,
listener: (message: MessageValue<Message>) => void
worker.on('message', listener)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected createWorker (): Worker {
return cluster.fork(this.opts.env)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected afterWorkerSetup (worker: Worker): void {
// Listen to worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get type (): PoolType {
return PoolType.FIXED
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get full (): boolean {
return this.workers.length === this.numberOfWorkers
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get busy (): boolean {
return this.internalBusy()
}
Data = unknown,
Response = unknown
> implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly isDynamicPool: boolean
- /** {@inheritDoc} */
+ /** @inheritDoc */
public requiredStatistics: RequiredStatistics = {
runTime: false,
avgRunTime: false
this.choose.bind(this)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract reset (): boolean
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract choose (): number
- /** {@inheritDoc} */
+ /** @inheritDoc */
public abstract remove (workerKey: number): boolean
}
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
avgRunTime: true
WorkerVirtualTaskTimestamp
> = new Map<number, WorkerVirtualTaskTimestamp>()
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
this.workerLastVirtualTaskTimestamp.clear()
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
let minWorkerVirtualTaskEndTimestamp = Infinity
let chosenWorkerKey!: number
return chosenWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
const workerDeleted = this.workerLastVirtualTaskTimestamp.delete(workerKey)
for (const [key, value] of this.workerLastVirtualTaskTimestamp.entries()) {
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
avgRunTime: false
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
const freeWorkerKey = this.pool.findFreeWorkerKey()
if (freeWorkerKey !== -1) {
return lessBusyWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
return true
}
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
const freeWorkerKey = this.pool.findFreeWorkerKey()
if (freeWorkerKey !== -1) {
return lessUsedWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
return true
}
*/
private nextWorkerId: number = 0
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
this.nextWorkerId = 0
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
const chosenWorkerKey = this.nextWorkerId
this.nextWorkerId =
return chosenWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
if (this.nextWorkerId === workerKey) {
if (this.pool.workers.length === 0) {
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ /** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
avgRunTime: true
this.initWorkersTaskRunTime()
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
this.currentWorkerId = 0
this.workersTaskRunTime.clear()
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
const chosenWorkerKey = this.currentWorkerId
if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorkerKey)) {
return chosenWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
if (this.currentWorkerId === workerKey) {
if (this.pool.workers.length === 0) {
super(min, filePath, opts)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get type (): PoolType {
return PoolType.DYNAMIC
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get full (): boolean {
return this.workers.length === this.max
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get busy (): boolean {
return this.full && this.findFreeWorkerKey() === -1
}
super(numberOfThreads, filePath, opts)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected isMain (): boolean {
return isMainThread
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public async destroyWorker (
worker: ThreadWorkerWithMessageChannel
): Promise<void> {
await worker.terminate()
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected sendToWorker (
worker: ThreadWorkerWithMessageChannel,
message: MessageValue<Data>
worker.postMessage(message)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public registerWorkerMessageListener<Message extends Data | Response>(
messageChannel: ThreadWorkerWithMessageChannel,
listener: (message: MessageValue<Message>) => void
messageChannel.port2?.on('message', listener)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected createWorker (): ThreadWorkerWithMessageChannel {
return new Worker(this.filePath, {
env: SHARE_ENV
})
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected afterWorkerSetup (worker: ThreadWorkerWithMessageChannel): void {
const { port1, port2 } = new MessageChannel()
worker.postMessage({ parent: port1 }, [port1])
this.registerWorkerMessageListener(worker, super.workerListener())
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get type (): PoolType {
return PoolType.FIXED
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get full (): boolean {
return this.workers.length === this.numberOfWorkers
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public get busy (): boolean {
return this.internalBusy()
}
)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
this.getMainWorker().send(message)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected handleError (e: Error | string): string {
return e instanceof Error ? e.message : e
}
super('worker-thread-pool:poolifier', isMainThread, fn, parentPort, opts)
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
this.getMainWorker().postMessage(message)
}