+ private executeStrategy (workerChoiceStrategy: IWorkerChoiceStrategy): number {
+ let workerNodeKey: number | undefined
+ let chooseCount = 0
+ let retriesCount = 0
+ do {
+ workerNodeKey = workerChoiceStrategy.choose()
+ if (workerNodeKey == null && chooseCount > 0) {
+ retriesCount++
+ }
+ chooseCount++
+ } while (
+ workerNodeKey == null &&
+ retriesCount < (this.opts?.retries as number)
+ )
+ if (workerNodeKey == null) {
+ throw new Error(
+ `Worker node key chosen is null or undefined after ${retriesCount} retries`
+ )
+ }
+ return workerNodeKey
+ }
+
+ /**
+ * Removes the worker node key from the worker choice strategy in the context.
+ *
+ * @param workerNodeKey - The worker node key.
+ * @returns `true` if the removal is successful, `false` otherwise.
+ */
+ public remove (workerNodeKey: number): boolean {
+ return (
+ this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ ).remove(workerNodeKey)
+ }
+
+ /**
+ * Sets the worker choice strategies in the context options.
+ *
+ * @param pool - The pool instance.
+ * @param opts - The worker choice strategy options.
+ */
+ public setOptions (
+ pool: IPool<Worker, Data, Response>,
+ opts?: InternalWorkerChoiceStrategyOptions
+ ): void {
+ this.opts = {
+ ...getDefaultInternalWorkerChoiceStrategyOptions(pool.info.maxSize),
+ ...opts
+ }
+ for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
+ workerChoiceStrategy.setOptions(this.opts)
+ }