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'
/**
* Worker virtual task timestamp.
* @typeParam Response - Type of response of execution. This can only be serializable data.
*/
export class FairShareWorkerChoiceStrategy<
- 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
}
/**
WorkerVirtualTaskTimestamp
> = new Map<number, WorkerVirtualTaskTimestamp>()
- /** {@inheritDoc} */
+ /** @inheritDoc */
public reset (): boolean {
this.workerLastVirtualTaskTimestamp.clear()
return true
}
- /** {@inheritDoc} */
+ /** @inheritDoc */
public choose (): number {
let minWorkerVirtualTaskEndTimestamp = Infinity
let chosenWorkerKey!: number
return chosenWorkerKey
}
+ /** @inheritDoc */
+ public remove (workerKey: number): boolean {
+ const workerDeleted = this.workerLastVirtualTaskTimestamp.delete(workerKey)
+ for (const [key, value] of this.workerLastVirtualTaskTimestamp.entries()) {
+ if (key > workerKey) {
+ this.workerLastVirtualTaskTimestamp.set(key - 1, value)
+ }
+ }
+ return workerDeleted
+ }
+
/**
* Computes worker last virtual task timestamp.
*