Fix huge usage bandwidth and performance issue of pinned message banner. (#37)

* Return only the first 100 pinned messages

* Execute pinned message 10 by 10
This commit is contained in:
Florian Duros 2024-09-13 09:47:22 +02:00 committed by GitHub
parent 5740bdbd38
commit 6b384fe9c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 100 additions and 11 deletions

View file

@ -40,3 +40,18 @@ export async function retry<T, E extends Error>(
}
throw lastErr;
}
/**
* Batch promises into groups of a given size.
* Execute the promises in parallel, but wait for all promises in a batch to resolve before moving to the next batch.
* @param funcs - The promises to batch
* @param batchSize - The number of promises to execute in parallel
*/
export async function batch<T>(funcs: Array<() => Promise<T>>, batchSize: number): Promise<T[]> {
const results: T[] = [];
for (let i = 0; i < funcs.length; i += batchSize) {
const batch = funcs.slice(i, i + batchSize);
results.push(...(await Promise.all(batch.map((f) => f()))));
}
return results;
}