- 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
+ public reset (): boolean {
+ this.currentWorkerNodeId = 0
+ this.workerVirtualTaskRunTime = 0
+ return true
+ }
+
+ /** @inheritDoc */
+ public update (): boolean {
+ return true
+ }
+
+ /** @inheritDoc */
+ public choose (): number {
+ const chosenWorkerNodeKey = this.currentWorkerNodeId
+ const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
+ const workerWeight =
+ this.opts.weights?.[chosenWorkerNodeKey] ?? this.defaultWorkerWeight
+ if (workerVirtualTaskRunTime < workerWeight) {
+ this.workerVirtualTaskRunTime =
+ workerVirtualTaskRunTime +
+ this.getWorkerTaskRunTime(chosenWorkerNodeKey)