repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix: ensure worker node destroy semantic is always the same
[poolifier.git]
/
src
/
pools
/
selection-strategies
/
round-robin-worker-choice-strategy.ts
diff --git
a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
index afdff788bf69eb999c2c4cc4ec08a59692c28fce..b023234a202cacc50b8f2505859e4e444bcf8f0f 100644
(file)
--- a/
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
+++ b/
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
@@
-4,6
+4,7
@@
import type { IWorker } from '../worker'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
import type {
IWorkerChoiceStrategy,
+ StrategyPolicy,
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
WorkerChoiceStrategyOptions
} from './selection-strategies-types'
@@
-11,8
+12,8
@@
import type {
* Selects the next worker in a round robin fashion.
*
* @typeParam Worker - Type of worker which manages the strategy.
* Selects the next worker in a round robin fashion.
*
* @typeParam Worker - Type of worker which manages the strategy.
- * @typeParam Data - Type of data sent to the worker. This can only be s
erializ
able data.
- * @typeParam Response - Type of execution response. This can only be s
erializ
able data.
+ * @typeParam Data - Type of data sent to the worker. This can only be s
tructured-clone
able data.
+ * @typeParam Response - Type of execution response. This can only be s
tructured-clone
able data.
*/
export class RoundRobinWorkerChoiceStrategy<
Worker extends IWorker,
*/
export class RoundRobinWorkerChoiceStrategy<
Worker extends IWorker,
@@
-21,10
+22,10
@@
export class RoundRobinWorkerChoiceStrategy<
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
implements IWorkerChoiceStrategy {
- /**
- * Id of the next worker node.
- */
- private nextWorkerNodeId: number = 0
+ /**
@inheritDoc */
+ public readonly strategyPolicy: StrategyPolicy = {
+ useDynamicWorker: true
+ }
/** @inheritDoc */
public constructor (
/** @inheritDoc */
public constructor (
@@
-32,12
+33,12
@@
export class RoundRobinWorkerChoiceStrategy<
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.setTaskStatistics(this.opts)
+ this.setTaskStatistics
Requirements
(this.opts)
}
/** @inheritDoc */
public reset (): boolean {
}
/** @inheritDoc */
public reset (): boolean {
- this.nextWorkerNode
Id
= 0
+ this.nextWorkerNode
Key
= 0
return true
}
return true
}
@@
-48,23
+49,30
@@
export class RoundRobinWorkerChoiceStrategy<
/** @inheritDoc */
public choose (): number {
/** @inheritDoc */
public choose (): number {
- const chosenWorkerNodeKey = this.nextWorkerNodeId
- this.nextWorkerNodeId =
- this.nextWorkerNodeId === this.pool.workerNodes.length - 1
- ? 0
- : this.nextWorkerNodeId + 1
+ const chosenWorkerNodeKey = this.nextWorkerNodeKey
+ do {
+ this.roundRobinNextWorkerNodeKey()
+ } while (!this.isWorkerNodeReady(this.nextWorkerNodeKey))
return chosenWorkerNodeKey
}
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
return chosenWorkerNodeKey
}
/** @inheritDoc */
public remove (workerNodeKey: number): boolean {
- if (this.nextWorkerNode
Id
=== workerNodeKey) {
+ if (this.nextWorkerNode
Key
=== workerNodeKey) {
if (this.pool.workerNodes.length === 0) {
if (this.pool.workerNodes.length === 0) {
- this.nextWorkerNode
Id
= 0
- } else if (this.nextWorkerNode
Id
> this.pool.workerNodes.length - 1) {
- this.nextWorkerNode
Id
= this.pool.workerNodes.length - 1
+ this.nextWorkerNode
Key
= 0
+ } else if (this.nextWorkerNode
Key
> this.pool.workerNodes.length - 1) {
+ this.nextWorkerNode
Key
= this.pool.workerNodes.length - 1
}
}
return true
}
}
}
return true
}
+
+ private roundRobinNextWorkerNodeKey (): number {
+ this.nextWorkerNodeKey =
+ this.nextWorkerNodeKey === this.pool.workerNodes.length - 1
+ ? 0
+ : this.nextWorkerNodeKey + 1
+ return this.nextWorkerNodeKey
+ }
}
}