Simplify logic https://github.com/sitespeedio/sitespeed.io/issues/4471 (#4472)
* Simplify logic https://github.com/sitespeedio/sitespeed.io/issues/4471 * Swallow and log errors
This commit is contained in:
parent
1c6a02f67b
commit
e644fac99b
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue