shuzhiren-comfyui/任务队列后端/utils/capacityGuard.js

34 lines
619 B
JavaScript

class CapacityGuard {
constructor() {
this.updateLock = false;
this.pendingUpdates = [];
}
async acquireLock() {
while (this.updateLock) {
await new Promise(resolve => setTimeout(resolve, 10));
}
this.updateLock = true;
}
releaseLock() {
this.updateLock = false;
if (this.pendingUpdates.length > 0) {
const nextUpdate = this.pendingUpdates.shift();
nextUpdate();
}
}
async executeWithLock(fn) {
await this.acquireLock();
try {
return await fn();
} finally {
this.releaseLock();
}
}
}
export default new CapacityGuard();