perf: 优化宝塔网站证书在并发部署时导致nginx配置文件错乱的问题

This commit is contained in:
xiaojunnuo
2025-11-24 23:18:56 +08:00
parent d75034deae
commit 51cc08411f
4 changed files with 82 additions and 166 deletions

View File

@@ -1,46 +1,17 @@
import { logger, utils } from './index.js';
import { logger } from "./index.js";
// @ts-ignore
import AsyncLock from "async-lock";
export class Locker {
locked: Record<string, any> = {};
private asyncLocker: AsyncLock;
async execute(lockStr: string, callback: any) {
await this.lock(lockStr);
const timeoutId = setTimeout(() => {
logger.warn('Lock timeout,自动解锁', lockStr);
this.unlock(lockStr);
}, 20000);
try {
return await callback();
} finally {
clearTimeout(timeoutId);
this.unlock(lockStr);
}
constructor() {
this.asyncLocker = new AsyncLock();
}
async lock(str: string) {
const isLocked = this.isLocked(str);
if (isLocked) {
let count = 0;
while (true) {
await utils.sleep(100);
if (!this.isLocked(str)) {
break;
}
count++;
if (count > 20) {
throw new Error('Lock timeout');
}
}
}
this.locked[str] = true;
}
unlock(str: string) {
delete this.locked[str];
}
isLocked(str: string) {
return this.locked[str] ?? false;
async execute(lockStr: string, callback: any, options?: { timeout?: number }) {
const timeout = options?.timeout ?? 20000;
return this.asyncLocker.acquire(lockStr, callback, { timeout });
}
}