StrategyData,
TaskStatistics,
WorkerInfo,
- WorkerNodeEventCallback,
WorkerType,
WorkerUsage
} from './pools/worker'
IWorker,
IWorkerNode,
WorkerInfo,
+ WorkerNodeEventDetail,
WorkerType,
WorkerUsage
} from './worker'
private setTaskStealing (): void {
for (const [workerNodeKey] of this.workerNodes.entries()) {
- this.workerNodes[workerNodeKey].onEmptyQueue =
- this.taskStealingOnEmptyQueue.bind(this)
+ this.workerNodes[workerNodeKey].addEventListener(
+ 'emptyqueue',
+ this.handleEmptyQueueEvent
+ )
}
}
private unsetTaskStealing (): void {
for (const [workerNodeKey] of this.workerNodes.entries()) {
- delete this.workerNodes[workerNodeKey].onEmptyQueue
+ this.workerNodes[workerNodeKey].removeEventListener(
+ 'emptyqueue',
+ this.handleEmptyQueueEvent
+ )
}
}
private setTasksStealingOnBackPressure (): void {
for (const [workerNodeKey] of this.workerNodes.entries()) {
- this.workerNodes[workerNodeKey].onBackPressure =
- this.tasksStealingOnBackPressure.bind(this)
+ this.workerNodes[workerNodeKey].addEventListener(
+ 'backpressure',
+ this.handleBackPressureEvent
+ )
}
}
private unsetTasksStealingOnBackPressure (): void {
for (const [workerNodeKey] of this.workerNodes.entries()) {
- delete this.workerNodes[workerNodeKey].onBackPressure
+ this.workerNodes[workerNodeKey].removeEventListener(
+ 'backpressure',
+ this.handleBackPressureEvent
+ )
}
}
this.sendStatisticsMessageToWorker(workerNodeKey)
if (this.opts.enableTasksQueue === true) {
if (this.opts.tasksQueueOptions?.taskStealing === true) {
- this.workerNodes[workerNodeKey].onEmptyQueue =
- this.taskStealingOnEmptyQueue.bind(this)
+ this.workerNodes[workerNodeKey].addEventListener(
+ 'emptyqueue',
+ this.handleEmptyQueueEvent.bind(this)
+ )
}
if (this.opts.tasksQueueOptions?.tasksStealingOnBackPressure === true) {
- this.workerNodes[workerNodeKey].onBackPressure =
- this.tasksStealingOnBackPressure.bind(this)
+ this.workerNodes[workerNodeKey].addEventListener(
+ 'backpressure',
+ this.handleBackPressureEvent.bind(this)
+ )
}
}
}
}
}
- private taskStealingOnEmptyQueue (workerId: number): void {
- const destinationWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId)
+ private readonly handleEmptyQueueEvent = (
+ event: CustomEvent<WorkerNodeEventDetail>
+ ): void => {
+ const destinationWorkerNodeKey = this.getWorkerNodeKeyByWorkerId(
+ event.detail.workerId
+ )
const workerNodes = this.workerNodes
.slice()
.sort(
const sourceWorkerNode = workerNodes.find(
workerNode =>
workerNode.info.ready &&
- workerNode.info.id !== workerId &&
+ workerNode.info.id !== event.detail.workerId &&
workerNode.usage.tasks.queued > 0
)
if (sourceWorkerNode != null) {
}
}
- private tasksStealingOnBackPressure (workerId: number): void {
+ private readonly handleBackPressureEvent = (
+ event: CustomEvent<WorkerNodeEventDetail>
+ ): void => {
const sizeOffset = 1
if ((this.opts.tasksQueueOptions?.size as number) <= sizeOffset) {
return
}
const sourceWorkerNode =
- this.workerNodes[this.getWorkerNodeKeyByWorkerId(workerId)]
+ this.workerNodes[this.getWorkerNodeKeyByWorkerId(event.detail.workerId)]
const workerNodes = this.workerNodes
.slice()
.sort(
if (
sourceWorkerNode.usage.tasks.queued > 0 &&
workerNode.info.ready &&
- workerNode.info.id !== workerId &&
+ workerNode.info.id !== event.detail.workerId &&
workerNode.usage.tasks.queued <
(this.opts.tasksQueueOptions?.size as number) - sizeOffset
) {
type IWorkerNode,
type StrategyData,
type WorkerInfo,
- type WorkerNodeEventCallback,
+ type WorkerNodeEventDetail,
type WorkerType,
WorkerTypes,
type WorkerUsage
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
*/
export class WorkerNode<Worker extends IWorker, Data = unknown>
-implements IWorkerNode<Worker, Data> {
+ extends EventTarget
+ implements IWorkerNode<Worker, Data> {
/** @inheritdoc */
public readonly worker: Worker
/** @inheritdoc */
public messageChannel?: MessageChannel
/** @inheritdoc */
public tasksQueueBackPressureSize: number
- /** @inheritdoc */
- public onBackPressure?: WorkerNodeEventCallback
- /** @inheritdoc */
- public onEmptyQueue?: WorkerNodeEventCallback
private readonly tasksQueue: Deque<Task<Data>>
private onBackPressureStarted: boolean
private onEmptyQueueCount: number
* @param tasksQueueBackPressureSize - The tasks queue back pressure size.
*/
constructor (worker: Worker, tasksQueueBackPressureSize: number) {
+ super()
checkWorkerNodeArguments<Worker>(worker, tasksQueueBackPressureSize)
this.worker = worker
this.info = this.initWorkerInfo(worker)
/** @inheritdoc */
public enqueueTask (task: Task<Data>): number {
const tasksQueueSize = this.tasksQueue.push(task)
- if (
- this.onBackPressure != null &&
- this.hasBackPressure() &&
- !this.onBackPressureStarted
- ) {
+ if (this.hasBackPressure() && !this.onBackPressureStarted) {
this.onBackPressureStarted = true
- this.onBackPressure(this.info.id as number)
+ this.dispatchEvent(
+ new CustomEvent<WorkerNodeEventDetail>('backpressure', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
this.onBackPressureStarted = false
}
return tasksQueueSize
/** @inheritdoc */
public unshiftTask (task: Task<Data>): number {
const tasksQueueSize = this.tasksQueue.unshift(task)
- if (
- this.onBackPressure != null &&
- this.hasBackPressure() &&
- !this.onBackPressureStarted
- ) {
+ if (this.hasBackPressure() && !this.onBackPressureStarted) {
this.onBackPressureStarted = true
- this.onBackPressure(this.info.id as number)
+ this.dispatchEvent(
+ new CustomEvent<WorkerNodeEventDetail>('backpressure', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
this.onBackPressureStarted = false
}
return tasksQueueSize
/** @inheritdoc */
public dequeueTask (): Task<Data> | undefined {
const task = this.tasksQueue.shift()
- if (
- this.onEmptyQueue != null &&
- this.tasksQueue.size === 0 &&
- this.onEmptyQueueCount === 0
- ) {
+ if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) {
this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
}
return task
/** @inheritdoc */
public popTask (): Task<Data> | undefined {
const task = this.tasksQueue.pop()
- if (
- this.onEmptyQueue != null &&
- this.tasksQueue.size === 0 &&
- this.onEmptyQueueCount === 0
- ) {
+ if (this.tasksQueue.size === 0 && this.onEmptyQueueCount === 0) {
this.startOnEmptyQueue().catch(EMPTY_FUNCTION)
}
return task
return
}
++this.onEmptyQueueCount
- this.onEmptyQueue?.(this.info.id as number)
+ this.dispatchEvent(
+ new CustomEvent<WorkerNodeEventDetail>('emptyqueue', {
+ detail: { workerId: this.info.id as number }
+ })
+ )
await sleep(exponentialDelay(this.onEmptyQueueCount))
await this.startOnEmptyQueue()
}
}
/**
- * Worker node event callback.
+ * Worker node event detail.
*
- * @param workerId - The worker id.
* @internal
*/
-export type WorkerNodeEventCallback = (workerId: number) => void
+export interface WorkerNodeEventDetail {
+ workerId: number
+}
/**
* Worker node interface.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
*/
-export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
+export interface IWorkerNode<Worker extends IWorker, Data = unknown>
+ extends EventTarget {
/**
* Worker.
*/
* This is the number of tasks that can be enqueued before the worker node has back pressure.
*/
tasksQueueBackPressureSize: number
- /**
- * Callback invoked when worker node tasks queue is back pressured.
- */
- onBackPressure?: WorkerNodeEventCallback
- /**
- * Callback invoked when worker node tasks queue is empty.
- */
- onEmptyQueue?: WorkerNodeEventCallback
/**
* Tasks queue size.
*
)
expect(pool.opts.enableTasksQueue).toBe(false)
expect(pool.opts.tasksQueueOptions).toBeUndefined()
- for (const workerNode of pool.workerNodes) {
- expect(workerNode.onEmptyQueue).toBeUndefined()
- expect(workerNode.onBackPressure).toBeUndefined()
- }
pool.enableTasksQueue(true)
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
taskStealing: true,
tasksStealingOnBackPressure: true
})
- for (const workerNode of pool.workerNodes) {
- expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
- expect(workerNode.onBackPressure).toBeInstanceOf(Function)
- }
pool.enableTasksQueue(true, { concurrency: 2 })
expect(pool.opts.enableTasksQueue).toBe(true)
expect(pool.opts.tasksQueueOptions).toStrictEqual({
taskStealing: true,
tasksStealingOnBackPressure: true
})
- for (const workerNode of pool.workerNodes) {
- expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
- expect(workerNode.onBackPressure).toBeInstanceOf(Function)
- }
pool.enableTasksQueue(false)
expect(pool.opts.enableTasksQueue).toBe(false)
expect(pool.opts.tasksQueueOptions).toBeUndefined()
- for (const workerNode of pool.workerNodes) {
- expect(workerNode.onEmptyQueue).toBeUndefined()
- expect(workerNode.onBackPressure).toBeUndefined()
- }
await pool.destroy()
})
expect(workerNode.tasksQueueBackPressureSize).toBe(
pool.opts.tasksQueueOptions.size
)
- expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
- expect(workerNode.onBackPressure).toBeInstanceOf(Function)
}
pool.setTasksQueueOptions({
concurrency: 2,
expect(workerNode.tasksQueueBackPressureSize).toBe(
pool.opts.tasksQueueOptions.size
)
- expect(workerNode.onEmptyQueue).toBeUndefined()
- expect(workerNode.onBackPressure).toBeUndefined()
}
pool.setTasksQueueOptions({
concurrency: 1,
expect(workerNode.tasksQueueBackPressureSize).toBe(
pool.opts.tasksQueueOptions.size
)
- expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
- expect(workerNode.onBackPressure).toBeInstanceOf(Function)
}
expect(() => pool.setTasksQueueOptions('invalidTasksQueueOptions')).toThrow(
new TypeError('Invalid tasks queue options: must be a plain object')