Retry design
The fetch task gives up on the first network error, and most of the failures in CI last week were a single dropped connection. #design #fetch
Proposal
Retry once, after a short pause, and only for errors that a second try can fix.
flowchart TD
fetch([fetch]) --> ok{2xx?}
ok -- yes --> done([return the body])
ok -- no --> retry{retryable?}
retry -- "timeout, 502, 503" --> again([pause, fetch again]) --> done
retry -- 4xx --> fail([throw])| Error | Retry? | Why |
|---|---|---|
| Timeout | yes | The far end may just be slow |
| 502, 503 | yes | A proxy restarting |
| 4xx | no | The request itself is wrong |
export async function retryOnce<T>(attempt: () => Promise<T>): Promise<T> {
try {
return await attempt();
} catch (error) {
if (!isRetryable(error)) throw error;
await sleep(PAUSE_MS);
return attempt();
}
}Open questions
- Does a retry need its own timeout? No: the pause is short.
- Should the pause grow with each attempt? Only once we retry more than once.
- Log the first failure, or only the second?
The current implementation: . The series sent for review: