Límites de velocidad de AppFolio
Descripción general
La API de AppFolio impone límites de velocidad por partner. Implementa aceleración para mantenerse dentro de los límites.
Manejador de límites de velocidad
import Bottleneck from "bottleneck";
const limitador = new Bottleneck({
maxConcurrent: 5,
minTime: 200, // máximo 5 solicitudes/segundo
});
async function solicitudAcelerada(cliente: any, ruta: string) {
return limitador.schedule(() => cliente.http.get(ruta));
}
// Reintento en 429
async function conReintento(fn: () => Promise<any>, maxReintentos = 3) {
for (let i = 1; i <= maxReintentos; i++) {
try { return await fn(); }
catch (err: any) {
if (err.response?.status !== 429 || i === maxReintentos) throw err;
const retraso = Math.min(1000 * Math.pow(2, i), 30000);
await new Promise(r => setTimeout(r, retraso));
}
}
}