perf: 执行队列数量支持设置

This commit is contained in:
xiaojunnuo
2025-12-26 18:17:05 +08:00
parent 888d9591fe
commit cd944882c3
9 changed files with 125 additions and 60 deletions
@@ -2,3 +2,4 @@ export * from './service/plus-service.js';
export * from './service/file-service.js';
export * from './service/encryptor.js';
export * from './service/ocr-service.js';
export * from './service/executor-queue.js';
@@ -0,0 +1,79 @@
import { logger } from "@certd/basic";
export type TaskItem = {
task: ()=>Promise<void>;
}
export class UserTaskQueue{
userId: number;
pendingQueue: TaskItem[] = [];
runningQueue: TaskItem[] = [];
getMaxRunningCount: ()=>number ;
constructor(req: { userId: number ,getMaxRunningCount: ()=>number }) {
this.userId = req.userId;
this.getMaxRunningCount = req.getMaxRunningCount ;
}
addTask(task: TaskItem) {
this.pendingQueue.push(task);
this.runTask();
}
runTask() {
logger.info(`[user_${this.userId}]当前运行队列:${this.runningQueue.length}, 等待队列:${this.pendingQueue.length},最大运行队列:${this.getMaxRunningCount()}`);
if (this.runningQueue.length >= this.getMaxRunningCount()) {
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();
}
}
logger.info(`[user_${this.userId}]执行任务,当前运行队列:${this.runningQueue.length}, 等待队列:${this.pendingQueue.length}`);
call()
}
}
export class ExecutorQueue{
queues: Record<number, UserTaskQueue> = {};
maxRunningCount: number = 8;
setMaxRunningCount(count: number) {
this.maxRunningCount = count;
}
getUserQueue(userId: number) {
const userQueue = this.queues[userId];
if (!userQueue) {
this.queues[userId] = new UserTaskQueue({ userId, getMaxRunningCount: ()=>this.maxRunningCount });
}
return this.queues[userId];
}
addTask(userId: number, task: TaskItem) {
const userQueue = this.getUserQueue(userId);
userQueue.addTask(task);
}
}
export const executorQueue = new ExecutorQueue();
@@ -43,12 +43,16 @@ export class SysPublicSettings extends BaseSettings {
//流水线是否启用有效期
pipelineValidTimeEnabled?: boolean = false;
//证书域名添加到监控
certDomainAddToMonitorEnabled?: boolean = false;
// 固定证书有效期天数,0表示不固定
fixedCertExpireDays?: number;
//默认到期前更新天数
defaultCertRenewDays?: number;
// 第三方OAuth配置
oauthEnabled?: boolean = false;
oauthProviders: Record<string, {
@@ -73,6 +77,8 @@ export class SysPrivateSettings extends BaseSettings {
httpRequestTimeout?: number = 30;
pipelineMaxRunningCount?: number;
sms?: {
type?: string;
config?: any;
@@ -8,6 +8,7 @@ import { BaseService } from '../../../basic/index.js';
import { cache, logger, setGlobalProxy } from '@certd/basic';
import * as dns from 'node:dns';
import {mergeUtils} from "@certd/basic";
import { executorQueue } from '../../basic/service/executor-queue.js';
const {merge} = mergeUtils;
/**
* 设置
@@ -140,6 +141,10 @@ export class SysSettingsService extends BaseService<SysSettingsEntity> {
if (bean.dnsResultOrder) {
dns.setDefaultResultOrder(bean.dnsResultOrder as any);
}
if (bean.pipelineMaxRunningCount){
executorQueue.setMaxRunningCount(bean.pipelineMaxRunningCount);
}
}
async updateByKey(key: string, setting: any) {