AppFolio API 速率限制
概述
AppFolio API 对每个合作伙伴实施速率限制。请实现节流以确保在限制范围内使用。
速率限制处理器
import Bottleneck from "bottleneck";
const limiter = new Bottleneck({
maxConcurrent: 5,
minTime: 200, // 每秒最多 5 个请求
});
async function throttledRequest(client: any, path: string) {
return limiter.schedule(() => client.http.get(path));
}
// 429 重试
async function withRetry(fn: () => Promise<any>, maxRetries = 3) {
for (let i = 1; i <= maxRetries; i++) {
try { return await fn(); }
catch (err: any) {
if (err.response?.status !== 429 || i === maxRetries) throw err;
const delay = Math.min(1000 * Math.pow(2, i), 30000);
await new Promise(r => setTimeout(r, delay));
}
}
}