From ea1204510bb6fd9c6ec215c264edf511534a970b Mon Sep 17 00:00:00 2001 From: Phat Pham Date: Fri, 13 Nov 2026 22:44:09 +0700 Subject: [PATCH] feat: rate-limit personalization and retry preview 503s Generalize the cache-miss limiter into rateLimit.ts with separate namespaces. /api/preview keeps its 20/min bucket; cache-miss /api/personalize requests now get a 10/min bucket before any embedding or LLM polish work. Cache hits bypass both. Personalize 429s log personalize_rate_limited and return Retry-After: 60. Retry a preview request once after a 503 (Cloudflare CPU limit) with a 500ms delay. Do not retry 429s, whose Retry-After is 60 seconds. Bump app.js to v110 and the PWA shell cache to v144. Co-Authored-By: Claude Code --- README.md | 6 +- core/render.ts | 2 +- core/test/render.spec.ts | 2 +- docs/ARCHITECTURE.md | 6 +- docs/DEPLOYMENT.md | 3 +- docs/RUNBOOK.md | 4 +- platforms/cloudflare/src/index.ts | 38 +++++++++- platforms/cloudflare/src/previewRateLimit.ts | 61 ---------------- platforms/cloudflare/src/rateLimit.ts | 73 +++++++++++++++++++ ...iewRateLimit.spec.ts => rateLimit.spec.ts} | 67 +++++++++++++---- web-static/service-worker.js | 4 +- web-static/static/app.js | 27 ++++++- 12 files changed, 199 insertions(+), 94 deletions(-) delete mode 100644 platforms/cloudflare/src/previewRateLimit.ts create mode 100644 platforms/cloudflare/src/rateLimit.ts rename platforms/cloudflare/test/{previewRateLimit.spec.ts => rateLimit.spec.ts} (50%) diff --git a/README.md b/README.md index 3ab743a..6278c9b 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,11 @@ Returns a reader view for a stored feed item: rich text segments, image blocks w - `url` — required; must exactly match the URL of an item currently stored in D1 -The Worker fetches the article with a 10-second timeout and a 400 KB HTML cap, rejects non-public hosts, and never returns upstream markup. When the built-in extractor finds no readable blocks or the response is a document (PDF/Office), the Worker can convert it through Workers AI's Markdown conversion with a 5 MB cap; if an HTML page still has no readable content, it can render the page through Browser Run. Set `FEEDREADER_AI_PREVIEW_FALLBACK=false` or `FEEDREADER_BROWSER_PREVIEW_FALLBACK=false` to disable either fallback. Inline targets and images are restricted to validated public http(s) URLs. Responses are edge-cached for 30 minutes (empty `no_content` results for 5 minutes); cache-miss previews are limited to 20 per minute per IP, while cached previews are unrestricted. Returns `404` for unknown URLs and `502` when the upstream page cannot be previewed. +The Worker fetches the article with a 10-second timeout and a 400 KB HTML cap, rejects non-public hosts, and never returns upstream markup. When the built-in extractor finds no readable blocks or the response is a document (PDF/Office), the Worker can convert it through Workers AI's Markdown conversion with a 5 MB cap; if an HTML page still has no readable content, it can render the page through Browser Run. Set `FEEDREADER_AI_PREVIEW_FALLBACK=false` or `FEEDREADER_BROWSER_PREVIEW_FALLBACK=false` to disable either fallback. Inline targets and images are restricted to validated public http(s) URLs. Responses are edge-cached for 30 minutes (empty `no_content` results for 5 minutes); cache-miss previews are limited to 20 per minute per IP, while cached previews are unrestricted. The client retries a transient `503` once. Returns `404` for unknown URLs and `502` when the upstream page cannot be previewed. + +### `POST /api/personalize` + +Returns a `For You` ranking for an interests string. Cache-miss requests are limited to 10 per minute per IP; cached rankings are unrestricted. ### `POST /api/refresh` diff --git a/core/render.ts b/core/render.ts index f6e43dc..94b4414 100644 --- a/core/render.ts +++ b/core/render.ts @@ -195,7 +195,7 @@ export function renderIndexPage(data: PageData): string { - +