Honour Retry-After when a webhook receiver asks us to wait
Node · Node · intermediate · modification
Some receivers answer `429` or `503` with a `Retry-After` header, and we kept retrying on our own schedule, sometimes seconds after being told to wait ten minutes. This adds `src/webhooks/retryAfter.js`, whose `parseRetryAfter(value, nowMs)` turns the header into a delay in milliseconds (or `null` when it is missing or not usable), and uses it in `src/webhooks/deliver.js`: on a 429 or 503 the retry waits for the longer of the receiver's delay and our backoff, capped at one hour, the ceiling the backoff already has. `deliver` gains a `now` option for the clock. New unit tests in `test/webhooks/retryAfter.test.js`; `test/webhooks/deliver.test.js` gains the Retry-After cases.
Webhook delivery service on Node 22 (CommonJS), using the built-in `fetch` and `node:test` (`node --test test/`). Existing and unchanged: `src/webhooks/worker.js` claims due jobs from the `webhook_jobs` table one at a time, calls `deliver(job)` with `job.attempt` the 1-based number of this attempt (an integer from 1 to `MAX_ATTEMPTS`), and stores the outcome: `delivered` and `failed` are final, `retry` sets `next_attempt_at` to its clock plus `delayMs` and `attempt` to `attempt + 1`. `job.url` (an HTTPS URL checked when the customer registered it), `job.body` (a JSON string) and `job.signature` are set when the event is enqueued. Receivers are customers' endpoints: some rate-limit with 429 and some answer 503 during maintenance, sometimes with `Retry-After`. Our servers and the receivers keep NTP time, so a date is compared with our clock as is. In the last 90 days of delivery logs every `Retry-After` was delta-seconds or an IMF-fixdate, so the team chose to treat the obsolete date forms as unusable rather than parse them.
Requirements
- On a `429` or `503` response that carries a usable `Retry-After`, the next attempt waits for the longer of the receiver's delay and the normal backoff for that attempt (30 s, doubling per attempt), and never more than 1 hour: a receiver's delay above an hour schedules the retry at 1 hour. Every other retryable outcome (another 5xx, a network error or timeout) keeps the normal backoff, whatever headers it carries.
- Usable means one of two RFC 9110 forms. Delta-seconds: ASCII digits only, so `-5`, `+5`, `1.5` and `1e3` are not usable. Or an IMF-fixdate (`Sun, 06 Nov 1994 08:49:37 GMT`), measured from the injected clock; a date in the past counts as zero. Spaces and tabs around the value are ignored. Not usable, so the response gets the normal backoff: an empty value, the obsolete RFC 850 and asctime date forms, an impossible calendar date (`31 Feb`), a time outside 00:00:00 to 23:59:59, and two `Retry-After` values on one response (fetch hands them over joined, as `600, 30`). The day name is not checked against the date.
- `Retry-After` never adds attempts: `MAX_ATTEMPTS` stays 8 and the eighth attempt fails whatever the header says. As before, a 2xx is delivered, any other 4xx (and a 3xx, since redirects are not followed) fails at once, and the response body is not read.
- `deliver` takes the clock as an option (`now`, default `Date.now`) so tests can pin it; the worker's existing call `deliver(job)` does not change.
Files touched
- src/webhooks/retryAfter.js
- src/webhooks/deliver.js
- test/webhooks/retryAfter.test.js
- test/webhooks/deliver.test.js