- Optimize worker storage in pool.
- Optimize worker alive status check.
+- Optimize `LESS_RECENTLY_USED` worker choice strategy.
### Fixed
/** {@inheritDoc} */
public getWorkerRunningTasks (worker: Worker): number | undefined {
- return this.workers.get(this.getWorkerKey(worker) as number)?.tasksUsage
- ?.running
+ return this.getWorkerTasksUsage(worker)?.running
+ }
+
+ /** {@inheritDoc} */
+ public getWorkerRunTasks (worker: Worker): number | undefined {
+ return this.getWorkerTasksUsage(worker)?.run
}
/** {@inheritDoc} */
public getWorkerAverageTasksRunTime (worker: Worker): number | undefined {
- return this.workers.get(this.getWorkerKey(worker) as number)?.tasksUsage
- ?.avgRunTime
+ return this.getWorkerTasksUsage(worker)?.avgRunTime
}
/** {@inheritDoc} */
Response = unknown
> extends IPool<Data, Response> {
/**
- * Map of workers.
+ * Pool workers map.
*/
readonly workers: Map<number, WorkerType<Worker>>
*/
getWorkerRunningTasks: (worker: Worker) => number | undefined
+ /**
+ * Gets worker run tasks.
+ *
+ * @param worker - The worker.
+ * @returns The number of tasks run on the worker.
+ */
+ getWorkerRunTasks: (worker: Worker) => number | undefined
+
/**
* Gets worker average tasks runtime.
*
/** {@inheritDoc} */
public choose (): Worker {
- let minNumberOfRunningTasks = Infinity
+ let minNumberOfTasks = Infinity
// A worker is always found because it picks the one with fewer tasks
let lessRecentlyUsedWorker!: Worker
for (const value of this.pool.workers.values()) {
const worker = value.worker
- const workerRunningTasks = this.pool.getWorkerRunningTasks(
- worker
- ) as number
- if (!this.isDynamicPool && workerRunningTasks === 0) {
+ const workerTasks =
+ (this.pool.getWorkerRunTasks(worker) as number) +
+ (this.pool.getWorkerRunningTasks(worker) as number)
+ if (!this.isDynamicPool && workerTasks === 0) {
return worker
- } else if (workerRunningTasks < minNumberOfRunningTasks) {
+ } else if (workerTasks < minNumberOfTasks) {
+ minNumberOfTasks = workerTasks
lessRecentlyUsedWorker = worker
- minNumberOfRunningTasks = workerRunningTasks
}
}
return lessRecentlyUsedWorker