Skip to content

Commit 4aeaad6

Browse files
committed
Stability update for safe IMAP reconnection
1 parent 91a1948 commit 4aeaad6

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

apps/worker/lib/imap/imap-client.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,33 @@ import { db, decryptAdminSecrets, identities, smtpAccountSecrets } from "@db";
22
import { eq } from "drizzle-orm";
33
import { ImapFlow } from "imapflow";
44

5+
const retryCounts = new Map<string, number>();
6+
const MAX_RETRIES = 3;
7+
58
function safeReconnect(
69
identityId: string,
710
imapInstances: Map<string, ImapFlow>,
811
) {
12+
const currentRetries = retryCounts.get(identityId) ?? 0;
13+
14+
if (currentRetries >= MAX_RETRIES) {
15+
console.error(`[IMAP:${identityId}] Max retries reached. Giving up.`);
16+
return;
17+
}
18+
19+
retryCounts.set(identityId, currentRetries + 1);
20+
921
const existing = imapInstances.get(identityId);
1022
if (existing) {
1123
try {
1224
existing.logout();
1325
} catch {}
1426
imapInstances.delete(identityId);
1527
}
16-
setTimeout(
17-
() => initSmtpClient(identityId, imapInstances).catch(console.error),
18-
5000,
19-
);
28+
29+
setTimeout(() => {
30+
initSmtpClient(identityId, imapInstances).catch(console.error);
31+
}, 5000);
2032
}
2133

2234
export const initSmtpClient = async (

0 commit comments

Comments
 (0)