Skip to content

Commit

Permalink
feat(discordTransport): Add in a delay between sequential restful cal…
Browse files Browse the repository at this point in the history
…ls to discord API to work around rate limits (UMAprotocol#3827)

* feat(discordTransport): Add in a delay between sequential restful calls to discord API to work around rate limits

Signed-off-by: chrismaree <[email protected]>

* nit

Signed-off-by: chrismaree <[email protected]>
  • Loading branch information
chrismaree authored Feb 22, 2022
1 parent 0a4ce9a commit 6bd28e5
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/financial-templates-lib/src/logger/DiscordTransport.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { delay } from "../helpers/delay";

import Transport from "winston-transport";

import axios from "axios";
Expand Down Expand Up @@ -44,8 +46,13 @@ export class DiscordTransport extends Transport {
else webHooks = [webHook];
}

// Send webhook request to each of the configured webhooks upstream. This posts the messages on Discord.
if (webHooks.length) await Promise.all(webHooks.map((webHook: string) => axios.post(webHook, body)));
// Send webhook request to each of the configured webhooks upstream. This posts the messages on Discord. Execute
// these sequentially with a soft delay of 2 seconds between calls to avoid hitting the discord rate limit.
if (webHooks.length)
for (const webHook of webHooks) {
await axios.post(webHook, body);
await delay(2);
}
} catch (error) {
console.error("Discord error", error);
}
Expand Down

0 comments on commit 6bd28e5

Please sign in to comment.