status_code=429, 当前分组上游负载已饱和,请稍后再试 (request id: 20261003172108678753602EGFEfSrh)
错误信息
status_code=429, 当前分组上游负载已饱和,请稍后再试 (request id: 20261003172108678753602EGFEfSrh)
AI 解决方案
原文:status_code=429, 当前分组上游负载已饱和,请稍后再试
译文:状态码429,当前分组上游负载已饱和,请稍后再试
🎯 原因分析
💡 解决方案
// 方案1:增加请求间隔(指数退避重试)
async function safeRequest(url, retries = 3) {
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
for (let i = 0; i < retries; i++) {
const response = await fetch(url);
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After') || 1000;
await delay(retryAfter * Math.pow(2, i)); // 指数退避
continue;
}
return response;
}
throw new Error('重试次数已达上限');
}
// 方案2:实现请求限流(令牌桶算法)
class RateLimiter {
constructor(maxRequests, interval) {
this.maxRequests = maxRequests;
this.interval = interval;
this.tokens = maxRequests;
}
async acquire() {
if (this.tokens <= 0) {
await new Promise(r => setTimeout(r, this.interval / this.maxRequests));
}
this.tokens--;
}
}
// 使用限流器包装请求
const limiter = new RateLimiter(10, 1000); // 每秒最多10次
await limiter.acquire();
const response = await fetch(url);