Minh Nguyen
@minh_nguyen • 2 months ago
Turn a raw diff into a Conventional Commits message and a structured PR description, with questions for anything the diff doesn't explain.
diffcontext{{diff}}{{context}}context: PAY-412. Merchants report missing order.paid webhooks when their servers return brief 502s during deploys.
diff:
```diff
--- a/src/webhooks/send.ts
+++ b/src/webhooks/send.ts
@@ -1,18 +1,33 @@
import { fetch } from "undici"
+import { setTimeout as sleep } from "node:timers/promises"
+
+const RETRYABLE = new Set([408, 429, 500, 502, 503, 504])
+const MAX_ATTEMPTS = 4
export async function sendWebhook(url: string, payload: unknown, secret: string) {
const body = JSON.stringify(payload)
const signature = sign(body, secret)
- const res = await fetch(url, {
- method: "POST",
- headers: { "content-type": "application/json", "x-signature": signature },
- body,
- signal: AbortSignal.timeout(5000),
- })
- if (!res.ok) throw new Error(`Webhook failed: ${res.status}`)
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
+ const res = await fetch(url, {
+ method: "POST",
+ headers: { "content-type": "application/json", "x-signature": signature },
+ body,
+ })
+ if (res.ok) return
+ if (!RETRYABLE.has(res.status) || attempt === MAX_ATTEMPTS) {
+ throw new Error(`Webhook failed after ${attempt} attempt(s): ${res.status}`)
+ }
+ await sleep(2 ** attempt * 250)
+ }
}
`````order.paid events. Non-retryable responses fail on the first attempt, as before.src/webhooks/send.ts)MAX_ATTEMPTS = 4 and backoff of 2^attempt × 250 ms (500, 1000, 2000 ms).AbortSignal.timeout(5000)) is removed.order.paid event.fetch throw, and those aren't retried. Only HTTP statuses are. Is that deliberate?Retry-After header instead of the fixed backoff?git blame two years later. The standout rule is "don't invent a reason, put it under Questions for the author". Left alone, models write confident explanations for every line, including the removed timeout here, which was probably an accident. Turning unexplained changes into questions makes the PR description a review aid. Numbered test steps that a reviewer can actually follow beat "tested locally".