*/
export class WeightedRoundRobinWorkerChoiceStrategy<
Worker extends IPoolWorker,
- Data,
- Response
+ Data = unknown,
+ Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
- /** {@inheritDoc} */
+ implements IWorkerChoiceStrategy {
+ /** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
avgRunTime: true
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) {