diff --git a/src/lib/import/imap-utils.ts b/src/lib/import/imap-utils.ts index c956088b1..513dbb051 100644 --- a/src/lib/import/imap-utils.ts +++ b/src/lib/import/imap-utils.ts @@ -49,6 +49,19 @@ export function isPermanentImapError(message: string): boolean { return /login|auth|credential|not allowed|unreadable|no longer exists/i.test(message); } +/** The server refused or dropped the connection, as opposed to failing on the messages themselves. */ +export function isImapConnectionError(message: string): boolean { + return /stream was cancelled|connection closed unexpectedly|did not send an OK greeting|connection (?:reset|refused)|timed? ?out/i.test(message); +} + +/** Connection failures tolerated at the same batch size before it shrinks too: a throttled host needs time, a batch that is too heavy needs to be smaller. */ +export const IMAP_CONNECTION_KEEP_SIZE_FAILURES = 3; + +/** Whether a connection failure should retry at the same batch size, given how many failed in a row. */ +export function keepsImapChunkSize(failures: number): boolean { + return failures <= IMAP_CONNECTION_KEEP_SIZE_FAILURES; +} + /** A folder the server says does not exist or cannot be selected, such as Gmail's `[Gmail]` parent. */ export function isMissingImapFolderError(message: string): boolean { return /NONEXISTENT|unknown mailbox|mailbox (?:does not|doesn't) exist|nonexistent namespace|NOSELECT/i.test(message); diff --git a/src/lib/import/jobs.ts b/src/lib/import/jobs.ts index e50d87eec..220b50a1e 100644 --- a/src/lib/import/jobs.ts +++ b/src/lib/import/jobs.ts @@ -4,7 +4,7 @@ import { importJobs, mailboxes } from "@/db/schema"; import { newId } from "@/lib/ids"; import { getImportMessageUserId, parseImportDestination } from "./destination"; import { fetchImapMessages } from "./imap"; -import { getImapChunkSize, growImapChunkSize, isMissingImapFolderError, isPermanentImapError } from "./imap-utils"; +import { getImapChunkSize, growImapChunkSize, isImapConnectionError, isMissingImapFolderError, isPermanentImapError, keepsImapChunkSize } from "./imap-utils"; import { deleteStagedImport, importBatchKey, stageImportMessages, unpackImportMessages } from "./staging"; import { importMessagesToMailbox } from "./service"; import type { @@ -24,6 +24,7 @@ const MAX_STORED_ERRORS = 20; const IMAP_CHUNK_SIZE = 50; /** Consecutive failed IMAP chunks, each retried smaller, before the job gives up. */ const MAX_IMAP_FAILURES = 4; +const MAX_IMAP_CONNECTION_FAILURES = 10; const STALE_AFTER_MS = 30 * 60 * 1000; const KEEP_FINISHED_MS = 7 * 24 * 60 * 60 * 1000; @@ -46,8 +47,8 @@ function parseJson(value: string, fallback: T): T { } } -async function enqueue(env: CloudflareEnv, message: ImportJobMessage): Promise { - await env.OUTBOUND_QUEUE.send(message); +async function enqueue(env: CloudflareEnv, message: ImportJobMessage, delaySeconds?: number): Promise { + await env.OUTBOUND_QUEUE.send(message, delaySeconds ? { delaySeconds } : undefined); } export function toImportJobSummary(row: JobRow): ImportJobSummary { @@ -295,16 +296,22 @@ async function retryImapChunkSmaller( if (isPermanentImapError(reason)) return false; const cursor = parseJson(job.cursor, { source: 0, done: 0, counted: false }); const failures = (cursor.failures ?? 0) + 1; - if (failures >= MAX_IMAP_FAILURES) return false; + // A refused or dropped connection says nothing about the batch: hosts such as cPanel throttle repeated + // logins, so back off and keep the size rather than shrinking it and hammering the server again at once. + const refused = isImapConnectionError(reason); + if (failures >= (refused ? MAX_IMAP_CONNECTION_FAILURES : MAX_IMAP_FAILURES)) return false; const spec = parseJson(job.spec, null); const base = spec ? imapBaseChunkSize(spec, cursor) : IMAP_CHUNK_SIZE; - const next: ImapImportCursor = { ...cursor, failures, size: Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)) }; + // The first few connection failures keep the size (a throttled host just needs time); later ones also + // shrink it, since a batch that is too heavy can drop the connection too. The delay keeps growing either way. + const size = refused && keepsImapChunkSize(failures) ? cursor.size : Math.max(1, Math.floor(getImapChunkSize(base, cursor.size) / 2)); + const next: ImapImportCursor = { ...cursor, failures, size }; const recorded = await getDb(env) .update(importJobs) .set({ cursor: JSON.stringify(next), updatedAt: new Date() }) .where(and(eq(importJobs.id, job.id), inArray(importJobs.status, [...ACTIVE]))) .returning({ id: importJobs.id }); - if (recorded.length) await enqueue(env, message); + if (recorded.length) await enqueue(env, message, refused ? Math.min(60 * failures, 600) : undefined); return true; } diff --git a/tests/imap-chunk-size.test.mjs b/tests/imap-chunk-size.test.mjs index 5a91f79bc..597d3926c 100644 --- a/tests/imap-chunk-size.test.mjs +++ b/tests/imap-chunk-size.test.mjs @@ -1,7 +1,7 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError } from "../src/lib/import/imap-utils.ts"; +import { getImapChunkSize, planImapFetchGroups, growImapChunkSize, isPermanentImapError, isMissingImapFolderError, isImapConnectionError, keepsImapChunkSize } from "../src/lib/import/imap-utils.ts"; test("chunk size defaults to the base and never exceeds it", () => { assert.equal(getImapChunkSize(50, undefined), 50); @@ -40,3 +40,16 @@ test("a folder the server cannot open is detected so the import can skip it", () assert.equal(isMissingImapFolderError("IMAP command failed: A0002 NO [NONEXISTENT] Unknown Mailbox: [Gmail] (Failure)"), true); assert.equal(isMissingImapFolderError("IMAP connection closed unexpectedly"), false); }); + +test("connection refusals are told apart from batch failures", () => { + assert.equal(isImapConnectionError("Stream was cancelled."), true); + assert.equal(isImapConnectionError("IMAP connection closed unexpectedly"), true); + assert.equal(isImapConnectionError("IMAP fetch failed: A0005 NO [SERVERBUG]"), false); + assert.equal(isImapConnectionError("IMAP command failed: A0001 NO [AUTHENTICATIONFAILED]"), false); +}); + +test("connection failures keep the batch size only for the first few attempts", () => { + assert.equal(keepsImapChunkSize(1), true); + assert.equal(keepsImapChunkSize(3), true); + assert.equal(keepsImapChunkSize(4), false); +});