tasksQueueOptions?: TasksQueueOptions
): void {
if (this.opts.enableTasksQueue === true && !enable) {
+ this.unsetTaskStealing()
+ this.unsetTasksStealingOnBackPressure()
this.flushTasksQueues()
}
this.opts.enableTasksQueue = enable
this.opts.tasksQueueOptions =
this.buildTasksQueueOptions(tasksQueueOptions)
this.setTasksQueueSize(this.opts.tasksQueueOptions.size as number)
+ if (this.opts.tasksQueueOptions.taskStealing === true) {
+ this.setTaskStealing()
+ } else {
+ this.unsetTaskStealing()
+ }
+ if (this.opts.tasksQueueOptions.tasksStealingOnBackPressure === true) {
+ this.setTasksStealingOnBackPressure()
+ } else {
+ this.unsetTasksStealingOnBackPressure()
+ }
} else if (this.opts.tasksQueueOptions != null) {
delete this.opts.tasksQueueOptions
}
}
}
+ private setTaskStealing (): void {
+ for (const [workerNodeKey] of this.workerNodes.entries()) {
+ this.workerNodes[workerNodeKey].onEmptyQueue =
+ this.taskStealingOnEmptyQueue.bind(this)
+ }
+ }
+
+ private unsetTaskStealing (): void {
+ for (const [workerNodeKey] of this.workerNodes.entries()) {
+ delete this.workerNodes[workerNodeKey].onEmptyQueue
+ }
+ }
+
+ private setTasksStealingOnBackPressure (): void {
+ for (const [workerNodeKey] of this.workerNodes.entries()) {
+ this.workerNodes[workerNodeKey].onBackPressure =
+ this.tasksStealingOnBackPressure.bind(this)
+ }
+ }
+
+ private unsetTasksStealingOnBackPressure (): void {
+ for (const [workerNodeKey] of this.workerNodes.entries()) {
+ delete this.workerNodes[workerNodeKey].onBackPressure
+ }
+ }
+
private buildTasksQueueOptions (
tasksQueueOptions: TasksQueueOptions
): TasksQueueOptions {
)
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()
})
taskStealing: true,
tasksStealingOnBackPressure: true
})
- pool.setTasksQueueOptions({ concurrency: 2 })
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
+ expect(workerNode.onBackPressure).toBeInstanceOf(Function)
+ }
+ pool.setTasksQueueOptions({
+ concurrency: 2,
+ taskStealing: false,
+ tasksStealingOnBackPressure: false
+ })
expect(pool.opts.tasksQueueOptions).toStrictEqual({
concurrency: 2,
size: 4,
+ taskStealing: false,
+ tasksStealingOnBackPressure: false
+ })
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.onEmptyQueue).toBeUndefined()
+ expect(workerNode.onBackPressure).toBeUndefined()
+ }
+ pool.setTasksQueueOptions({
+ concurrency: 1,
+ taskStealing: true,
+ tasksStealingOnBackPressure: true
+ })
+ expect(pool.opts.tasksQueueOptions).toStrictEqual({
+ concurrency: 1,
+ size: 4,
taskStealing: true,
tasksStealingOnBackPressure: true
})
+ for (const workerNode of pool.workerNodes) {
+ expect(workerNode.onEmptyQueue).toBeInstanceOf(Function)
+ expect(workerNode.onBackPressure).toBeInstanceOf(Function)
+ }
expect(() =>
pool.setTasksQueueOptions('invalidTasksQueueOptions')
).toThrowError(