Reintentos cliente
Cuándo reintentar
| Código | ¿Reintentar? | Estrategia |
|---|---|---|
2xx | No (éxito) | — |
400 | ❌ No | Corregir el body |
401 | ❌ No | Verificar la API key |
403 | ❌ No | Solicitar el scope |
404 | ❌ No | Verificar la clave del comprobante |
409 | ❌ No | Idempotency-Key conflictiva |
413 | ❌ No | Reducir el body |
429 | ✅ Sí | Respetar Retry-After |
500 | ✅ Sí | Backoff exponencial |
502 | ✅ Sí | Backoff exponencial |
503 | ✅ Sí | Backoff agresivo (sistema caído) |
504 | ✅ Sí | Backoff exponencial |
| Timeout cliente | ✅ Sí | Con Idempotency-Key |
Algoritmo de backoff exponencial con jitter
async function backoff(intento) {
const base = Math.min(1000 * Math.pow(2, intento), 30000); // max 30s
const jitter = Math.random() * 1000;
await sleep(base + jitter);
}
for (let i = 0; i < 5; i++) {
try {
return await llamarAPI();
} catch (err) {
if (esTransitorio(err) && i < 4) {
await backoff(i);
continue;
}
throw err;
}
}