- public choose (): Worker {
- const currentWorker = this.pool.workers[this.currentWorkerIndex]
- if (
- this.isDynamicPool === true &&
- this.workersTaskRunTime.has(currentWorker) === false
- ) {
- this.initWorkerTaskRunTime(currentWorker)
- }
- const workerVirtualTaskRunTime =
- this.getWorkerVirtualTaskRunTime(currentWorker) ?? 0
- const workerTaskWeight =
- this.workersTaskRunTime.get(currentWorker)?.weight ??
- this.defaultWorkerWeight
- if (this.currentWorkerIndex === this.previousWorkerIndex) {
- const workerTaskRunTime =
- (this.workersTaskRunTime.get(currentWorker)?.runTime ?? 0) +
- workerVirtualTaskRunTime
- this.setWorkerTaskRunTime(
- currentWorker,
- workerTaskWeight,
- workerTaskRunTime
- )
- } else {
- this.setWorkerTaskRunTime(currentWorker, workerTaskWeight, 0)
- }
- if (workerVirtualTaskRunTime < workerTaskWeight) {
- this.previousWorkerIndex = this.currentWorkerIndex
- } else {
- this.previousWorkerIndex = this.currentWorkerIndex
- this.currentWorkerIndex =
- this.pool.workers.length - 1 === this.currentWorkerIndex
- ? 0
- : this.currentWorkerIndex + 1
- }
- return this.pool.workers[this.currentWorkerIndex]
- }
-
- private initWorkersTaskRunTime (): void {
- for (const worker of this.pool.workers) {
- this.initWorkerTaskRunTime(worker)
- }
- }
-
- private initWorkerTaskRunTime (worker: Worker): void {
- this.setWorkerTaskRunTime(worker, this.defaultWorkerWeight, 0)