| 1 |
import { AsyncResource } from 'async_hooks'; |
| 2 |
import { Worker } from 'worker_threads'; |
| 3 |
import { cpus } from 'os'; |
| 4 |
import { EventEmitter } from 'events'; |
| 5 |
|
| 6 |
import serializeJavascript from 'serialize-javascript'; |
| 7 |
|
| 8 |
import { freeWorker, taskInfo, workerPoolWorkerFlag } from './constants'; |
| 9 |
|
| 10 |
import type { |
| 11 |
WorkerCallback, |
| 12 |
WorkerContext, |
| 13 |
WorkerOutput, |
| 14 |
WorkerPoolOptions, |
| 15 |
WorkerPoolTask, |
| 16 |
WorkerWithTaskInfo |
| 17 |
} from './type'; |
| 18 |
|
| 19 |
class WorkerPoolTaskInfo extends AsyncResource { |
| 20 |
constructor(private callback: WorkerCallback) { |
| 21 |
super('WorkerPoolTaskInfo'); |
| 22 |
} |
| 23 |
|
| 24 |
done(err: Error | null, result: any) { |
| 25 |
this.runInAsyncScope(this.callback, null, err, result); |
| 26 |
this.emitDestroy(); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
export class WorkerPool extends EventEmitter { |
| 31 |
protected maxInstances: number; |
| 32 |
|
| 33 |
protected filePath: string; |
| 34 |
|
| 35 |
protected tasks: WorkerPoolTask[] = []; |
| 36 |
|
| 37 |
protected workers: WorkerWithTaskInfo[] = []; |
| 38 |
protected freeWorkers: WorkerWithTaskInfo[] = []; |
| 39 |
|
| 40 |
constructor(options: WorkerPoolOptions) { |
| 41 |
super(); |
| 42 |
|
| 43 |
this.maxInstances = options.maxWorkers || cpus().length; |
| 44 |
this.filePath = options.filePath; |
| 45 |
|
| 46 |
this.on(freeWorker, () => { |
| 47 |
if (this.tasks.length > 0) { |
| 48 |
const { context, cb } = this.tasks.shift()!; |
| 49 |
this.runTask(context, cb); |
| 50 |
} |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
get numWorkers(): number { |
| 55 |
return this.workers.length; |
| 56 |
} |
| 57 |
|
| 58 |
addAsync(context: WorkerContext): Promise<WorkerOutput> { |
| 59 |
return new Promise((resolve, reject) => { |
| 60 |
this.runTask(context, (err, output) => { |
| 61 |
if (err) { |
| 62 |
reject(err); |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if (!output) { |
| 67 |
reject(new Error('The output is empty')); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
resolve(output); |
| 72 |
}); |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
close() { |
| 77 |
for (let i = 0; i < this.workers.length; i++) { |
| 78 |
const worker = this.workers[i]; |
| 79 |
worker.terminate(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private addNewWorker() { |
| 84 |
const worker: WorkerWithTaskInfo = new Worker(this.filePath, { |
| 85 |
workerData: workerPoolWorkerFlag |
| 86 |
}); |
| 87 |
|
| 88 |
worker.on('message', (result) => { |
| 89 |
worker[taskInfo]?.done(null, result); |
| 90 |
worker[taskInfo] = null; |
| 91 |
this.freeWorkers.push(worker); |
| 92 |
this.emit(freeWorker); |
| 93 |
}); |
| 94 |
|
| 95 |
worker.on('error', (err) => { |
| 96 |
if (worker[taskInfo]) { |
| 97 |
worker[taskInfo].done(err, null); |
| 98 |
} else { |
| 99 |
this.emit('error', err); |
| 100 |
} |
| 101 |
this.workers.splice(this.workers.indexOf(worker), 1); |
| 102 |
this.addNewWorker(); |
| 103 |
}); |
| 104 |
|
| 105 |
this.workers.push(worker); |
| 106 |
this.freeWorkers.push(worker); |
| 107 |
this.emit(freeWorker); |
| 108 |
} |
| 109 |
|
| 110 |
private runTask(context: WorkerContext, cb: WorkerCallback) { |
| 111 |
if (this.freeWorkers.length === 0) { |
| 112 |
this.tasks.push({ context, cb }); |
| 113 |
if (this.numWorkers < this.maxInstances) { |
| 114 |
this.addNewWorker(); |
| 115 |
} |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
const worker = this.freeWorkers.pop(); |
| 120 |
if (worker) { |
| 121 |
worker[taskInfo] = new WorkerPoolTaskInfo(cb); |
| 122 |
worker.postMessage({ |
| 123 |
code: context.code, |
| 124 |
options: serializeJavascript(context.options) |
| 125 |
}); |
| 126 |
} |
| 127 |
} |
| 128 |
} |