Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,35 @@ core.start = async (commander) => {
});

let isReconnecting = false;
let rtmRestartAttempts = 0;
const RTM_RESTART_BASE_DELAY_MS = 5000;
const RTM_RESTART_MAX_DELAY_MS = 60000;

function scheduleRtmRestart() {
const delay = Math.min(
RTM_RESTART_BASE_DELAY_MS * Math.pow(2, rtmRestartAttempts),
RTM_RESTART_MAX_DELAY_MS
);
rtmRestartAttempts++;
console.log(`Retrying RTM connection in ${delay / 1000}s... (attempt ${rtmRestartAttempts})`);
setTimeout(() => {
rtm.start();
}, delay);
}

function isNetworkError(error) {
if (!error) return false;
const msg = error.message || String(error);
return (
error.code === "ENOTFOUND" ||
error.code === "ECONNREFUSED" ||
error.code === "ETIMEDOUT" ||
msg.includes("ENOTFOUND") ||
msg.includes("ECONNREFUSED") ||
msg.includes("ETIMEDOUT") ||
msg.includes("getaddrinfo")
);
}

rtm.on("disconnect", () => {
if (!isReconnecting) {
Expand All @@ -295,11 +324,12 @@ core.start = async (commander) => {
});

rtm.on("authenticated", (rtmStartData) => {
rtmRestartAttempts = 0;
if (isReconnecting) {
console.log("RTM reconnected successfully");
isReconnecting = false;
}

if (!util.startUp) {
console.log(
`Logged in as ${chalk.bold(rtmStartData.self.name)} of team ${chalk.green.bold(rtmStartData.team.name)}, but not yet connected to a channel`
Expand All @@ -311,6 +341,9 @@ core.start = async (commander) => {
rtm.on("unable_to_rtm_start", (error) => {
console.error("Unable to start RTM:", error.message || error);
isReconnecting = false;
if (isNetworkError(error)) {
scheduleRtmRestart();
}
});

rtm.on("message", (message) => {
Expand Down
Loading