repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: methods namepace cleanup
[poolifier.git]
/
src
/
pools
/
selection-strategies
/
least-used-worker-choice-strategy.ts
diff --git
a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts
b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts
index 4161a1d266634518c5800d57a421acb46b690fe6..e8a7218e160879bcaa97399d78a3b39ff7857bc1 100644
(file)
--- a/
src/pools/selection-strategies/least-used-worker-choice-strategy.ts
+++ b/
src/pools/selection-strategies/least-used-worker-choice-strategy.ts
@@
-11,8
+11,8
@@
import type {
* Selects the least used worker.
*
* @typeParam Worker - Type of worker which manages the strategy.
* Selects the least used worker.
*
* @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 LeastUsedWorkerChoiceStrategy<
Worker extends IWorker,
*/
export class LeastUsedWorkerChoiceStrategy<
Worker extends IWorker,
@@
-42,30
+42,33
@@
export class LeastUsedWorkerChoiceStrategy<
/** @inheritDoc */
public choose (): number {
/** @inheritDoc */
public choose (): number {
- const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
- if (freeWorkerNodeKey !== -1) {
- return freeWorkerNodeKey
- }
+ return this.leastUsedNextWorkerNodeKey()
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+
+ private leastUsedNextWorkerNodeKey (): number {
let minNumberOfTasks = Infinity
let minNumberOfTasks = Infinity
- let leastUsedWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
- const workerTaskStatistics = workerNode.
workerU
sage.tasks
+ const workerTaskStatistics = workerNode.
u
sage.tasks
const workerTasks =
workerTaskStatistics.executed +
workerTaskStatistics.executing +
workerTaskStatistics.queued
const workerTasks =
workerTaskStatistics.executed +
workerTaskStatistics.executing +
workerTaskStatistics.queued
- if (workerTasks === 0) {
- return workerNodeKey
- } else if (workerTasks < minNumberOfTasks) {
+ if (this.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) {
+ this.nextWorkerNodeKey = workerNodeKey
+ break
+ } else if (
+ this.isWorkerNodeReady(workerNodeKey) &&
+ workerTasks < minNumberOfTasks
+ ) {
minNumberOfTasks = workerTasks
minNumberOfTasks = workerTasks
-
leastUsed
WorkerNodeKey = workerNodeKey
+
this.next
WorkerNodeKey = workerNodeKey
}
}
}
}
- return leastUsedWorkerNodeKey
- }
-
- /** @inheritDoc */
- public remove (): boolean {
- return true
+ return this.nextWorkerNodeKey
}
}
}
}