- public choose (): Worker {
- const currentWorker = this.pool.workers[this.currentWorkerIndex]
- if (this.isDynamicPool === true) {
- this.workerTaskRunTime.has(currentWorker) === false &&
- this.workerTaskRunTime.set(currentWorker, {
- weight: this.defaultWorkerWeight,
- runTime: 0
- })
- }
- const workerVirtualTaskRunTime =
- this.getWorkerVirtualTaskRunTime(currentWorker) ?? 0
- const workerTaskWeight =
- this.workerTaskRunTime.get(currentWorker)?.weight ??
- this.defaultWorkerWeight
- if (this.currentWorkerIndex === this.previousWorkerIndex) {
- const workerTaskRunTime =
- (this.workerTaskRunTime.get(currentWorker)?.runTime ?? 0) +
- workerVirtualTaskRunTime
- this.workerTaskRunTime.set(currentWorker, {
- weight: workerTaskWeight,
- runTime: workerTaskRunTime
- })
- } else {
- this.workerTaskRunTime.set(currentWorker, {
- weight: workerTaskWeight,
- runTime: 0
- })
- }
- if (
- workerVirtualTaskRunTime <
- (this.workerTaskRunTime.get(currentWorker) ?? this.defaultWorkerWeight)
- ) {
- 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]