Skip to content

Commit

Permalink
Start draupnir bots in batches when running in appservice mode (#569)
Browse files Browse the repository at this point in the history
* Start draupnir bots in batches when running in appservice mode

* Simplify and add clarity.

---------

Co-authored-by: gnuxie <Gnuxie@protonmail.com>
  • Loading branch information
MTRNord and Gnuxie authored Sep 20, 2024
1 parent 5ab32f9 commit ee1fad5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/appservice/AppServiceDraupnirManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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))
);
}
}
}
Expand Down

0 comments on commit ee1fad5

Please sign in to comment.