* Selects the next worker with an interleaved weighted round robin scheduling algorithm.
*
* @typeParam Worker - Type of worker which manages the strategy.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
Worker extends IWorker,
/** @inheritDoc */
public reset (): boolean {
- this.nextWorkerNodeId = 0
+ this.nextWorkerNodeKey = 0
this.roundId = 0
return true
}
roundIndex++
) {
for (
- let workerNodeKey = this.nextWorkerNodeId;
+ let workerNodeKey = this.nextWorkerNodeKey;
workerNodeKey < this.pool.workerNodes.length;
workerNodeKey++
) {
const workerWeight =
this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
- if (workerWeight >= this.roundWeights[roundIndex]) {
+ if (
+ this.isWorkerNodeReady(workerNodeKey) &&
+ workerWeight >= this.roundWeights[roundIndex]
+ ) {
roundId = roundIndex
workerNodeId = workerNodeKey
break
}
}
this.roundId = roundId ?? 0
- this.nextWorkerNodeId = workerNodeId ?? 0
- const chosenWorkerNodeKey = this.nextWorkerNodeId
- if (this.nextWorkerNodeId === this.pool.workerNodes.length - 1) {
- this.nextWorkerNodeId = 0
+ this.nextWorkerNodeKey = workerNodeId ?? 0
+ const chosenWorkerNodeKey = this.nextWorkerNodeKey
+ if (this.nextWorkerNodeKey === this.pool.workerNodes.length - 1) {
+ this.nextWorkerNodeKey = 0
this.roundId =
this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
} else {
- this.nextWorkerNodeId = this.nextWorkerNodeId + 1
+ this.nextWorkerNodeKey = this.nextWorkerNodeKey + 1
}
return chosenWorkerNodeKey
}
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
- if (this.nextWorkerNodeId === workerNodeKey) {
+ if (this.nextWorkerNodeKey === workerNodeKey) {
if (this.pool.workerNodes.length === 0) {
- this.nextWorkerNodeId = 0
- } else if (this.nextWorkerNodeId > this.pool.workerNodes.length - 1) {
- this.nextWorkerNodeId = this.pool.workerNodes.length - 1
+ this.nextWorkerNodeKey = 0
+ } else if (this.nextWorkerNodeKey > this.pool.workerNodes.length - 1) {
+ this.nextWorkerNodeKey = this.pool.workerNodes.length - 1
this.roundId =
this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
}