Improve media cleanup performance with bounded concurrency and aggregated failure reporting

This commit is contained in:
Yusuf İpek
2026-02-24 17:44:23 +03:00
parent 6e14d4d19f
commit 7f07a271a4
4 changed files with 85 additions and 37 deletions
+29
View File
@@ -0,0 +1,29 @@
/**
* Execute async work with a bounded number of in-flight tasks.
*/
export async function runWithConcurrency<T>(
items: T[],
limit: number,
worker: (item: T) => Promise<void>
): Promise<void> {
if (items.length === 0) return;
const concurrency = Math.max(1, Math.floor(limit));
let nextIndex = 0;
async function runWorker(): Promise<void> {
while (true) {
const currentIndex = nextIndex;
nextIndex += 1;
if (currentIndex >= items.length) {
return;
}
await worker(items[currentIndex]);
}
}
const workerCount = Math.min(concurrency, items.length);
await Promise.all(Array.from({ length: workerCount }, () => runWorker()));
}