import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
- StrategyPolicy,
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
- /** @inheritDoc */
- public readonly strategyPolicy: StrategyPolicy = {
- useDynamicWorker: true
- }
-
/**
* Round id.
* This is used to determine the current round weight.
/** @inheritDoc */
public reset (): boolean {
- this.nextWorkerNodeId = 0
+ this.resetWorkerNodeKeyProperties()
this.roundId = 0
return true
}
}
/** @inheritDoc */
- public choose (): number {
- let roundId: number | undefined
+ public choose (): number | undefined {
+ let roundId!: number
let workerNodeId: number | undefined
for (
let roundIndex = this.roundId;
roundIndex < this.roundWeights.length;
roundIndex++
) {
+ roundId = roundIndex
for (
- let workerNodeKey = this.nextWorkerNodeId;
+ let workerNodeKey =
+ this.nextWorkerNodeKey ?? this.previousWorkerNodeKey;
workerNodeKey < this.pool.workerNodes.length;
workerNodeKey++
) {
const workerWeight =
this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
- if (workerWeight >= this.roundWeights[roundIndex]) {
- roundId = roundIndex
+ if (
+ this.isWorkerNodeEligible(workerNodeKey) &&
+ workerWeight >= this.roundWeights[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.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.nextWorkerNodeKey = 0
this.roundId =
this.roundId === this.roundWeights.length - 1 ? 0 : this.roundId + 1
} else {
- this.nextWorkerNodeId = this.nextWorkerNodeId + 1
+ this.nextWorkerNodeKey =
+ (this.nextWorkerNodeKey ?? this.previousWorkerNodeKey) + 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
}