import type { IPoolInternal } from '../pool-internal'
import type { IPoolWorker } from '../pool-worker'
import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy'
-import type { RequiredStatistics } from './selection-strategies-types'
+import type {
+ IWorkerChoiceStrategy,
+ RequiredStatistics
+} from './selection-strategies-types'
/**
* Virtual task runtime.
* @typeParam Response - Type of response of execution. This can only be serializable data.
*/
export class WeightedRoundRobinWorkerChoiceStrategy<
- Worker extends IPoolWorker,
- Data,
- Response
-> extends AbstractWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ Worker extends IPoolWorker,
+ Data = unknown,
+ Response = unknown
+ >
+ extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
+ implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
- runTime: true
+ runTime: true,
+ avgRunTime: true,
+ medRunTime: false
}
/**
this.initWorkersTaskRunTime()
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
this.currentWorkerId = 0
this.workersTaskRunTime.clear()
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
const chosenWorkerKey = this.currentWorkerId
if (this.isDynamicPool && !this.workersTaskRunTime.has(chosenWorkerKey)) {
return chosenWorkerKey
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public remove (workerKey: number): boolean {
if (this.currentWorkerId === workerKey) {
- this.currentWorkerId =
- this.currentWorkerId > this.pool.workers.length - 1
- ? this.pool.workers.length - 1
- : this.currentWorkerId
+ if (this.pool.workers.length === 0) {
+ this.currentWorkerId = 0
+ } else {
+ this.currentWorkerId =
+ this.currentWorkerId > this.pool.workers.length - 1
+ ? this.pool.workers.length - 1
+ : this.currentWorkerId
+ }
}
const workerDeleted = this.workersTaskRunTime.delete(workerKey)
for (const [key, value] of this.workersTaskRunTime) {