Skip to content

feat(preview): limit cache-miss previews and retry empties sooner - #24

Merged
phatpham9 merged 1 commit into
mainfrom
feat/preview-rate-limit-negative-cache
Nov 13, 2026
Merged

phatpham9 merged 1 commit into
mainfrom
feat/preview-rate-limit-negative-cache

Conversation

@phatpham9

Copy link
Copy Markdown
Member

Summary

  • Adds a per-IP fixed-window limiter for /api/preview cache misses (20 per minute). Cached previews bypass it, so normal browsing stays unrestricted while a client cannot rapidly enumerate stored articles and burn Workers AI / Browser Run quota.
  • The limiter uses the Cache API, is per-colo and fail-open, and returns 429 with reason: "rate_limited" and Retry-After: 60. It logs preview_rate_limited.
  • Empty no_content previews now cache for 5 minutes instead of 30, so a transient AI/Browser Run failure can retry. preview_empty logs cache_ttl_seconds.
  • No new environment variables; the limit and TTLs are constants in previewRateLimit.ts / previewCachePolicy.ts.
  • Updates README/ARCHITECTURE/DEPLOYMENT/RUNBOOK, including a note that the in-Worker limiter is per-colo and a zone-level Cloudflare Rate Limiting rule is the option for a global limit.

Verification

  • npm run typecheck
  • npx vitest run --exclude '.claude/**' — 18 files, 145 tests
  • npm run db:migrate:local
  • wrangler deploy --dry-run — 371.79 KiB / 98.61 KiB gzip
  • Live local check with the limit temporarily set to 1:
    • first cache miss → 200 in 1.3s
    • second cache miss → 429 in 9ms, reason: "rate_limited", preview_rate_limited logged
    • repeat of the first URL from cache → 200 in 3.6ms, bypassing the limiter
  • Cache-policy tests cover 0 blocks → 300s, >0 blocks → 1800s.

Notes

  • The limiter is an abuse speed bump, not a global quota: it is per-colo, per-IP, fail-open, and does not protect against distributed clients. Use a Cloudflare Rate Limiting rule if a zone-level limit is needed.
  • /api/personalize is not included in this PR; it can get the same treatment separately.

🤖 Generated with Claude Code

Add a per-IP fixed-window limiter for /api/preview cache misses (20
per minute). Cached previews bypass it, so normal browsing stays
unrestricted while a client cannot rapidly enumerate stored articles
and burn the Workers AI / Browser Run quotas. The limiter uses the
Cache API, is per-colo and fail-open, and returns 429 with
Retry-After: 60 and reason "rate_limited".

Empty no_content previews now cache for 5 minutes instead of 30 so a
transient AI/Browser Run failure can retry. Add preview_rate_limited
logging and cache_ttl_seconds on preview_empty.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🔎 Cloudflare preview: https://671c4964-feedreader.phatpham9.workers.dev

Uploaded from 5cd8cf9c59222160025ed4fd3309e83c1ccf1613. This is a Worker version preview — it shares the production D1 database, so it reads/writes real data; it does not receive cron-triggered refreshes.

@phatpham9
phatpham9 merged commit 811e952 into main Nov 13, 2026
3 checks passed
@phatpham9
phatpham9 deleted the feat/preview-rate-limit-negative-cache branch November 13, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant