mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 09:22:45 +00:00
refactor(core): unify bounded concurrency runner
This commit is contained in:
48
src/utils/run-with-concurrency.ts
Normal file
48
src/utils/run-with-concurrency.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
export type ConcurrencyErrorMode = "continue" | "stop";
|
||||
|
||||
export async function runTasksWithConcurrency<T>(params: {
|
||||
tasks: Array<() => Promise<T>>;
|
||||
limit: number;
|
||||
errorMode?: ConcurrencyErrorMode;
|
||||
onTaskError?: (error: unknown, index: number) => void;
|
||||
}): Promise<{ results: T[]; firstError: unknown; hasError: boolean }> {
|
||||
const { tasks, limit, onTaskError } = params;
|
||||
const errorMode = params.errorMode ?? "continue";
|
||||
if (tasks.length === 0) {
|
||||
return { results: [], firstError: undefined, hasError: false };
|
||||
}
|
||||
|
||||
const resolvedLimit = Math.max(1, Math.min(limit, tasks.length));
|
||||
const results: T[] = Array.from({ length: tasks.length });
|
||||
let next = 0;
|
||||
let firstError: unknown = undefined;
|
||||
let hasError = false;
|
||||
|
||||
const workers = Array.from({ length: resolvedLimit }, async () => {
|
||||
while (true) {
|
||||
if (errorMode === "stop" && hasError) {
|
||||
return;
|
||||
}
|
||||
const index = next;
|
||||
next += 1;
|
||||
if (index >= tasks.length) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
results[index] = await tasks[index]();
|
||||
} catch (error) {
|
||||
if (!hasError) {
|
||||
firstError = error;
|
||||
hasError = true;
|
||||
}
|
||||
onTaskError?.(error, index);
|
||||
if (errorMode === "stop") {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.allSettled(workers);
|
||||
return { results, firstError, hasError };
|
||||
}
|
||||
Reference in New Issue
Block a user