From 888d9591fe9730b529e1c355d71f41e7ec9b479d Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 26 Dec 2025 16:30:03 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=AF=E6=8C=81=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=98=9F=E5=88=97=EF=BC=8C=E9=81=BF=E5=85=8D=E5=90=8C=E4=B8=80?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=A7=A6=E5=8F=91=E6=B5=81=E6=B0=B4=E7=BA=BF?= =?UTF-8?q?=E5=A4=AA=E5=A4=9A=E5=AF=BC=E8=87=B4=E8=A2=AB=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pipeline/service/executor-queue.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 packages/ui/certd-server/src/modules/pipeline/service/executor-queue.ts diff --git a/packages/ui/certd-server/src/modules/pipeline/service/executor-queue.ts b/packages/ui/certd-server/src/modules/pipeline/service/executor-queue.ts new file mode 100644 index 000000000..18fdd4bf4 --- /dev/null +++ b/packages/ui/certd-server/src/modules/pipeline/service/executor-queue.ts @@ -0,0 +1,54 @@ +import { logger } from "@certd/basic"; + +export type TaskItem = { + task: ()=>Promise; +} + +export class ExecutorQueue{ + pendingQueue: TaskItem[] = []; + runningQueue: TaskItem[] = []; + maxRunningCount: number = 10; + + + setMaxRunningCount(count: number) { + this.maxRunningCount = count; + } + + addTask(task: TaskItem) { + this.pendingQueue.push(task); + this.runTask(); + } + + runTask() { + logger.info(`当前运行队列:${this.runningQueue.length}, 等待队列:${this.pendingQueue.length}`); + if (this.runningQueue.length >= this.maxRunningCount) { + logger.info(`当前运行队列已满,等待队列:${this.pendingQueue.length}`); + return; + } + if (this.pendingQueue.length === 0) { + return; + } + const task = this.pendingQueue.shift(); + if (!task) { + return; + } + // 执行任务 + this.runningQueue.push(task); + const call = async ()=>{ + try{ + await task.task(); + }finally{ + // 任务执行完成,从运行队列中移除 + const index = this.runningQueue.indexOf(task); + if (index > -1) { + this.runningQueue.splice(index, 1); + } + // 继续执行下一个任务 + this.runTask(); + } + } + call() + } +} + +export const executorQueue = new ExecutorQueue(); \ No newline at end of file