31 lines
854 B
JavaScript
31 lines
854 B
JavaScript
// Bounded scheduler for best-effort stream prewarming. Requests beyond the
|
|
// active limit wait in a short FIFO instead of being silently discarded.
|
|
export function createWarmQueue({ concurrency = 2, maxQueued = 8, run }) {
|
|
const queued = [];
|
|
const known = new Set();
|
|
let active = 0;
|
|
|
|
function pump() {
|
|
while (active < concurrency && queued.length) {
|
|
const id = queued.shift();
|
|
active++;
|
|
Promise.resolve().then(() => run(id)).catch(() => {}).finally(() => {
|
|
active--;
|
|
known.delete(id);
|
|
pump();
|
|
});
|
|
}
|
|
}
|
|
|
|
function enqueue(id) {
|
|
if (known.has(id)) return true;
|
|
if (active >= concurrency && queued.length >= maxQueued) return false;
|
|
known.add(id);
|
|
queued.push(id);
|
|
pump();
|
|
return true;
|
|
}
|
|
|
|
return { enqueue, get pending() { return queued.length + active; } };
|
|
}
|