Skip to content

Commit e644fac

Browse files
authored
Simplify logic #4471 (#4472)
* Simplify logic #4471 * Swallow and log errors
1 parent 1c6a02f commit e644fac

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

lib/plugins/s3/limit.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
export async function runWithConcurrencyLimit(tasks, limit) {
2-
const running = new Set();
3-
4-
async function runNext() {
5-
if (tasks.length === 0) {
6-
return;
7-
}
8-
9-
const task = tasks.shift();
10-
const promise = task()
11-
.catch(error => {
12-
throw error;
13-
})
14-
.finally(() => {
15-
running.delete(promise);
16-
void runNext();
17-
});
1+
import { getLogger } from '@sitespeed.io/log';
2+
const log = getLogger('sitespeedio.plugin.s3');
183

19-
running.add(promise);
20-
if (running.size < limit) {
21-
void runNext();
4+
export async function runWithConcurrencyLimit(tasks, limit) {
5+
async function worker() {
6+
while (tasks.length > 0) {
7+
const task = tasks.shift();
8+
try {
9+
await task();
10+
} catch (error) {
11+
log.error('Could not finish upload task', error);
12+
}
2213
}
2314
}
2415

25-
const starters = [];
26-
for (let index = 0; index < limit && tasks.length > 0; index++) {
27-
starters.push(runNext());
28-
}
29-
30-
await Promise.allSettled(starters);
31-
if (running.size > 0) {
32-
await Promise.allSettled(Array.from(running));
33-
}
16+
const workers = Array.from({ length: limit }, () => worker());
17+
await Promise.all(workers);
3418
}

0 commit comments

Comments
 (0)