* Simplify logic https://github.com/sitespeedio/sitespeed.io/issues/4471

* Swallow and log errors
This commit is contained in:
Peter Hedenskog 2025-03-08 15:17:40 +01:00 committed by GitHub
parent 1c6a02f67b
commit e644fac99b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 29 deletions

View File

@ -1,34 +1,18 @@
import { getLogger } from '@sitespeed.io/log';
const log = getLogger('sitespeedio.plugin.s3');
export async function runWithConcurrencyLimit(tasks, limit) {
const running = new Set();
async function runNext() {
if (tasks.length === 0) {
return;
}
const task = tasks.shift();
const promise = task()
.catch(error => {
throw error;
})
.finally(() => {
running.delete(promise);
void runNext();
});
running.add(promise);
if (running.size < limit) {
void runNext();
async function worker() {
while (tasks.length > 0) {
const task = tasks.shift();
try {
await task();
} catch (error) {
log.error('Could not finish upload task', error);
}
}
}
const starters = [];
for (let index = 0; index < limit && tasks.length > 0; index++) {
starters.push(runNext());
}
await Promise.allSettled(starters);
if (running.size > 0) {
await Promise.allSettled(Array.from(running));
}
const workers = Array.from({ length: limit }, () => worker());
await Promise.all(workers);
}