Skip to content

Commit

Permalink
Retry after being ratelimited
Browse files Browse the repository at this point in the history
  • Loading branch information
reivilibre committed Feb 3, 2024
1 parent c887f65 commit c150b2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/commands/InviteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { logMessage } from "../LogProxy";
import { IPerson, Role } from "../models/schedule";
import { ConferenceMatrixClient } from "../ConferenceMatrixClient";
import { IConfig } from "../config";
import { sleep } from "../utils";

export class InviteCommand implements ICommand {
public readonly prefixes = ["invite", "inv"];
Expand Down Expand Up @@ -118,12 +119,20 @@ export class InviteCommand implements ICommand {
for (const target of people) {
if (target.mxid && effectiveJoinedUserIds.includes(target.mxid)) continue;
if (emailInvitePersonIds.includes(target.person.id)) continue;
try {
await invitePersonToRoom(this.client, target, roomId, this.config);
++invitesSent;
} catch (e) {
LogService.error("InviteCommand", e);
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${target.mxid}/${target.emails} / ${target.person.id} to ${roomId} - ignoring: ${e.message ?? e.statusMessage ?? '(see logs)'}`, this.client);
for (let attempt = 0; attempt < 3; ++attempt) {
try {
await invitePersonToRoom(this.client, target, roomId, this.config);
++invitesSent;
} catch (e) {
if (e.statusCode === 429) {
// HACK Retry after ratelimits
await sleep(301_000);
continue;
}
LogService.error("InviteCommand", e);
await logMessage(LogLevel.ERROR, "InviteCommand", `Error inviting ${target.mxid}/${target.emails} / ${target.person.id} to ${roomId} - ignoring: ${e.message ?? e.statusMessage ?? '(see logs)'}`, this.client);
}
break;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,7 @@ export function jsonReplacerMapToObject(_key: any, input: any): any {
}
return input;
}

export function sleep(millis: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, millis));
}

0 comments on commit c150b2b

Please sign in to comment.