From 415144c121cf82cc5b30d57b85c7d04ad955edf5 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Wed, 4 Dec 2024 20:33:20 +0000 Subject: [PATCH] Add fastpriorityqueue to concurrencyController so it scales better --- frontend/src/lib/utils/concurrencyController.ts | 11 ++++++----- package.json | 1 + pnpm-lock.yaml | 7 +++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/frontend/src/lib/utils/concurrencyController.ts b/frontend/src/lib/utils/concurrencyController.ts index 47683b954be86..941af92f33b74 100644 --- a/frontend/src/lib/utils/concurrencyController.ts +++ b/frontend/src/lib/utils/concurrencyController.ts @@ -1,5 +1,5 @@ +import FastPriorityQueue from 'fastpriorityqueue' import { promiseResolveReject } from 'lib/utils' - class ConcurrencyControllerItem { _debugTag?: string _runFn: () => Promise @@ -52,7 +52,9 @@ export class ConcurrencyController { _concurrencyLimit: number _current: ConcurrencyControllerItem[] = [] - private _queue: ConcurrencyControllerItem[] = [] + private _queue: FastPriorityQueue> = new FastPriorityQueue( + (a, b) => a._priority < b._priority + ) constructor(concurrencyLimit: number) { this._concurrencyLimit = concurrencyLimit @@ -79,7 +81,7 @@ export class ConcurrencyController { }): Promise => { const item = new ConcurrencyControllerItem(this, fn, abortController, priority, debugTag) - this._queue.push(item) + this._queue.add(item) this._tryRunNext() @@ -87,8 +89,7 @@ export class ConcurrencyController { } _runNext(): void { - this._queue.sort((a, b) => a._priority - b._priority) - const next = this._queue.shift() + const next = this._queue.poll() if (next) { next._runFn() .catch(() => { diff --git a/package.json b/package.json index fc88d73076ccf..ca06eb6507b3d 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ "expr-eval": "^2.0.2", "express": "^4.17.1", "fast-deep-equal": "^3.1.3", + "fastpriorityqueue": "^0.7.5", "fflate": "^0.7.4", "fs-extra": "^10.0.0", "fuse.js": "^6.6.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 46b9d8ad2f29c..30cdc4508d7f0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -217,6 +217,9 @@ dependencies: fast-deep-equal: specifier: ^3.1.3 version: 3.1.3 + fastpriorityqueue: + specifier: ^0.7.5 + version: 0.7.5 fflate: specifier: ^0.7.4 version: 0.7.4 @@ -12767,6 +12770,10 @@ packages: engines: {node: '>= 4.9.1'} dev: true + /fastpriorityqueue@0.7.5: + resolution: {integrity: sha512-3Pa0n9gwy8yIbEsT3m2j/E9DXgWvvjfiZjjqcJ+AdNKTAlVMIuFYrYG5Y3RHEM8O6cwv9hOpOWY/NaMfywoQVA==} + dev: false + /fastq@1.13.0: resolution: {integrity: sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==} dependencies: