41 lines
889 B
TypeScript
41 lines
889 B
TypeScript
class Limiter {
|
|
private running = 0;
|
|
private queue: (() => void)[] = [];
|
|
|
|
constructor(private max: number) {}
|
|
|
|
async run<T>(fn: () => Promise<T>): Promise<T> {
|
|
await this.acquire();
|
|
try {
|
|
return await fn();
|
|
} finally {
|
|
this.release();
|
|
}
|
|
}
|
|
|
|
private acquire(): Promise<void> {
|
|
if (this.running < this.max) {
|
|
this.running++;
|
|
return Promise.resolve();
|
|
}
|
|
return new Promise(resolve => {
|
|
this.queue.push(() => { this.running++; resolve(); });
|
|
});
|
|
}
|
|
|
|
private release() {
|
|
this.running--;
|
|
const next = this.queue.shift();
|
|
if (next) next();
|
|
}
|
|
|
|
get active() { return this.running; }
|
|
|
|
get queued() { return this.queue.length; }
|
|
}
|
|
|
|
// ElevenLabs recommends max 2-3 concurrent requests
|
|
export const ttsLimiter = new Limiter(2);
|
|
|
|
// OpenRouter concurrent request cap
|
|
export const aiLimiter = new Limiter(4);
|