fix(fetchers): honor Retry-After header on 429/503 responses
Summary
The LLM and embedding fetchers in apps/memos-local-plugin classified HTTP 429 as transient and retried with exponential backoff, but never read the Retry-After header. Under provider rate limiting, retries fired before the upstream-requested cooldown, extending throttling and wasting paid API calls.
This PR parses Retry-After per RFC 7231 §7.1.3 (delta-seconds or HTTP-date), caps the value to a sane maximum, and prefers it over exponential backoff on 429/503. Falls back to the existing backoff when the header is absent or unparseable.
Changes
- New shared helper
apps/memos-local-plugin/core/util/retry-after.tsexportingparseRetryAfterMs(resp, capMs). apps/memos-local-plugin/core/llm/fetcher.ts: parse Retry-After on 429/503 and use it as the sleep duration; small refactor splitbackoff()intobackoffMs()+sleep()so both code paths share one timer.apps/memos-local-plugin/core/embedding/fetcher.ts: same treatment, mirroring the LLM fetcher.- Tests added/extended:
tests/unit/util/retry-after.test.ts— covers integer seconds, HTTP-date, capping, malformed, missing, past-date, and negative inputs.tests/unit/llm/fetcher.test.tsandtests/unit/embedding/fetcher.test.ts— three new cases each: integer Retry-After is honored, HTTP-date is honored, malformed value falls back to backoff.
Behavior
Retry-After: 5→ sleep ~5s instead of next backoff step.Retry-After: Wed, 21 Oct 2025 07:28:00 GMT→ sleep ~(date - now), capped.- Header absent or malformed → falls back to existing exponential backoff (no behavior change).
- Capped at 60s (
MAX_RETRY_AFTER_MS) to prevent hostile or buggy servers from pinning the client indefinitely. - Only 429 and 503 honor Retry-After. Other 5xx still use exponential backoff (the header is rarely set on those, and we want to keep retrying quickly on non-rate-limit failures).
Notes
- Could not run
tsc --noEmitlocally (nonode_modulesin this worktree), so types were verified by inspection. The new helper has explicit return typenumber | nulland matches the import-style (.jsextension,Bundlerresolution) used elsewhere in the codebase. - Tests use the existing real-timer pattern in
tests/unit/embedding/fetcher.test.ts; sleep upper bounds are generous (≥900 ms forRetry-After: 1) to avoid flakes on slow CI.
Fixes #1620
Thanks for the review — addressed in the latest commit:
retry-after.ts:11: Updated the JSDoc to clarify that0(retry immediately) is a valid value; only negative or unparseable values returnnull. Tests already covered the0case and continue to pass.llm/fetcher.ts:17/embedding/fetcher.ts:15: AppliedMath.min(sleepMs, MAX_RETRY_AFTER_MS)to the final sleep duration in both fetchers, so the 60s cap applies whether the value came fromRetry-AfterorbackoffMs(). Previously amaxRetries: 10config could let exponential backoff exceed 60s.
Diff is small and behavior-only-on-the-margin (only changes anything when attempt >= ~6 in a long retry chain, or when Retry-After: 0 arrives).
This PR has been automatically marked as stale due to inactivity.