From ee1fad567c6dd93cb416d96fa34053a9dd382a48 Mon Sep 17 00:00:00 2001 From: Marcel Date: Fri, 20 Sep 2024 09:44:44 +0200 Subject: [PATCH] Start draupnir bots in batches when running in appservice mode (#569) * Start draupnir bots in batches when running in appservice mode * Simplify and add clarity. --------- Co-authored-by: gnuxie --- src/appservice/AppServiceDraupnirManager.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/appservice/AppServiceDraupnirManager.ts b/src/appservice/AppServiceDraupnirManager.ts index ea9b4fc7..68a80b30 100644 --- a/src/appservice/AppServiceDraupnirManager.ts +++ b/src/appservice/AppServiceDraupnirManager.ts @@ -378,8 +378,16 @@ export class AppServiceDraupnirManager { * Used at startup to create all the ManagedMjolnir instances and start them so that they will respond to users. */ public async startDraupnirs(mjolnirRecords: MjolnirRecord[]): Promise { - for (const mjolnirRecord of mjolnirRecords) { - await this.startDraupnirFromRecord(mjolnirRecord); + // Start the bots in small batches instead of sequentially. + // This is to avoid a thundering herd of bots all starting at once. + // It also is to avoid that others have to wait for a single bot to start. + const chunkSize = 5; + for (let i = 0; i < mjolnirRecords.length; i += chunkSize) { + const batch = mjolnirRecords.slice(i, i + chunkSize); + await Promise.all( + // `startDraupnirFromRecord` handles errors for us and adds the draupnir to the list of unstarted draupnir. + batch.map((record) => this.startDraupnirFromRecord(record)) + ); } } }