Skip to content

Build your personal knowledge base with Trilium Notes

refactor(import): drop Retry-After handling for OneNote throttling

Elian Dorande99784f166f
diff --git a/apps/server/src/services/import/onenote/graph.spec.ts b/apps/server/src/services/import/onenote/graph.spec.ts
index b7661e9ad9..effdfe0a0c 100644
--- a/apps/server/src/services/import/onenote/graph.spec.ts
+++ b/apps/server/src/services/import/onenote/graph.spec.ts
@@ -1,21 +1,16 @@
import { describe, expect, it } from "vitest";
-import { retryDelayMs } from "./graph.js";
+import { backoffDelayMs } from "./graph.js";
-describe("retryDelayMs", () => {
- it("honours a numeric Retry-After header (seconds → ms)", () => {
- expect(retryDelayMs("3", 0)).toBe(3000);
- });
-
- it("falls back to exponential backoff when the header is absent or non-numeric", () => {
- expect(retryDelayMs(null, 0)).toBe(2000);
- expect(retryDelayMs(null, 1)).toBe(4000);
- expect(retryDelayMs("soon", 2)).toBe(8000);
+describe("backoffDelayMs", () => {
+ it("doubles the delay with each attempt", () => {
+ expect(backoffDelayMs(0)).toBe(2000);
+ expect(backoffDelayMs(1)).toBe(4000);
+ expect(backoffDelayMs(2)).toBe(8000);
+ expect(backoffDelayMs(3)).toBe(16000);
});
it("caps the delay at the maximum", () => {
- // Both a huge Retry-After and a far-out backoff attempt clamp to the 30s ceiling.
- expect(retryDelayMs("3600", 0)).toBe(30_000);
- expect(retryDelayMs(null, 10)).toBe(30_000);
+ expect(backoffDelayMs(10)).toBe(30_000);
});
});
diff --git a/apps/server/src/services/import/onenote/graph.ts b/apps/server/src/services/import/onenote/graph.ts
index 3262add92a..e7df7435b6 100644
--- a/apps/server/src/services/import/onenote/graph.ts
+++ b/apps/server/src/services/import/onenote/graph.ts
@@ -16,11 +16,11 @@ const GRAPH_BASE = "https://graph.microsoft.com/v1.0";
// be large and slow. Allow a generous per-request budget instead.
const GRAPH_TIMEOUT_MS = 60_000;
-// Graph throttles aggressively under load (HTTP 429, occasionally 503). Retry those, honouring the
-// Retry-After header when present and falling back to exponential backoff, before giving up —
-// otherwise a large import would silently drop most of its pages/resources once throttling kicks in.
+// Graph throttles aggressively under load (HTTP 429, occasionally 503). OneNote's 429s carry no
+// Retry-After header, so retry with exponential backoff before giving up — otherwise a large import
+// would silently drop most of its pages/resources once throttling kicks in.
const MAX_RETRIES = 8;
-const DEFAULT_RETRY_DELAY_MS = 2000;
+const BASE_RETRY_DELAY_MS = 2000;
const MAX_RETRY_DELAY_MS = 30_000;
// Shared throttle gate. Graph throttles per app/tenant, so a 429 on one request means every other
@@ -58,17 +58,15 @@ async function graphFetch(accessToken: string, url: string): Promise<Response> {
// Extend the shared gate (Math.max: simultaneous 429s converge on one window rather than
// stacking). The wait itself happens at the top of the next iteration, shared across the pool.
- const waitMs = retryDelayMs(response.headers.get("Retry-After"), attempt);
+ const waitMs = backoffDelayMs(attempt);
throttledUntilMs = Math.max(throttledUntilMs, Date.now() + waitMs);
getLog().info(`OneNote import: Graph throttled (HTTP ${response.status}) on ${url}; retry ${attempt + 1}/${MAX_RETRIES} after ${waitMs}ms`);
}
}
-/** Resolves the wait before a throttling retry: the Retry-After header (seconds), else exponential backoff. */
-export function retryDelayMs(retryAfter: string | null, attempt: number): number {
- const headerSeconds = retryAfter ? Number(retryAfter) : NaN;
- const delayMs = Number.isFinite(headerSeconds) ? headerSeconds * 1000 : DEFAULT_RETRY_DELAY_MS * 2 ** attempt;
- return Math.min(delayMs, MAX_RETRY_DELAY_MS);
+/** Exponential backoff before a throttling retry, capped at {@link MAX_RETRY_DELAY_MS}. */
+export function backoffDelayMs(attempt: number): number {
+ return Math.min(BASE_RETRY_DELAY_MS * 2 ** attempt, MAX_RETRY_DELAY_MS);
}
function delay(ms: number): Promise<void> {