*/
protected nextWorkerNodeKey: number | undefined = 0
+ /**
+ * The previous worker node key.
+ */
+ protected previousWorkerNodeKey: number = 0
+
/** @inheritDoc */
public readonly strategyPolicy: StrategyPolicy = {
dynamicWorkerUsage: false,
/** @inheritDoc */
public choose (): number | undefined {
- let roundId: number | undefined
+ let roundId: number = this.roundId
let workerNodeId: number | undefined
for (
let roundIndex = this.roundId;
roundIndex < this.roundWeights.length;
roundIndex++
) {
+ roundId = roundIndex
for (
- let workerNodeKey = this.nextWorkerNodeKey ?? 0;
+ let workerNodeKey =
+ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey;
workerNodeKey < this.pool.workerNodes.length;
workerNodeKey++
) {
this.isWorkerNodeEligible(workerNodeKey) &&
workerWeight >= this.roundWeights[roundIndex]
) {
- roundId = roundIndex
workerNodeId = workerNodeKey
break
}
}
}
- this.roundId = roundId as number
+ this.roundId = roundId
+ if (workerNodeId == null) {
+ this.previousWorkerNodeKey =
+ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+ }
this.nextWorkerNodeKey = workerNodeId
const chosenWorkerNodeKey = this.nextWorkerNodeKey
if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) {
this.roundId =
this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
} else {
- this.nextWorkerNodeKey = (this.nextWorkerNodeKey ?? 0) + 1
+ this.nextWorkerNodeKey =
+ (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
}
return chosenWorkerNodeKey
}
this.roundRobinNextWorkerNodeKey()
if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
this.nextWorkerNodeKey = undefined
+ this.previousWorkerNodeKey =
+ chosenWorkerNodeKey ?? this.previousWorkerNodeKey
}
return chosenWorkerNodeKey
}
this.nextWorkerNodeKey =
this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
? 0
- : (this.nextWorkerNodeKey ?? 0) + 1
+ : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
return this.nextWorkerNodeKey
}
}
this.weightedRoundRobinNextWorkerNodeKey()
if (!this.isWorkerNodeEligible(this.nextWorkerNodeKey as number)) {
this.nextWorkerNodeKey = undefined
+ this.previousWorkerNodeKey =
+ chosenWorkerNodeKey ?? this.previousWorkerNodeKey
}
return chosenWorkerNodeKey
}
private weightedRoundRobinNextWorkerNodeKey (): number | undefined {
const workerVirtualTaskRunTime = this.workerVirtualTaskRunTime
const workerWeight =
- this.opts.weights?.[this.nextWorkerNodeKey ?? 0] ??
- this.defaultWorkerWeight
+ this.opts.weights?.[
+ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+ ] ?? this.defaultWorkerWeight
if (workerVirtualTaskRunTime < workerWeight) {
this.workerVirtualTaskRunTime =
workerVirtualTaskRunTime +
- this.getWorkerTaskRunTime(this.nextWorkerNodeKey ?? 0)
+ this.getWorkerTaskRunTime(
+ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey
+ )
} else {
this.nextWorkerNodeKey =
this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
? 0
- : (this.nextWorkerNodeKey ?? 0) + 1
+ : (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 1
this.workerVirtualTaskRunTime = 0
}
return this.nextWorkerNodeKey