feat: add IP info request retry mechanism to reduce network errors

This commit is contained in:
wonfen 2025-04-20 10:34:43 +08:00
parent 541b78ba6f
commit 4f15bdf74d
2 changed files with 37 additions and 15 deletions

View File

@ -21,6 +21,7 @@
#### 优化了:
- 系统代理 Bypass 设置
- Windows 下使用 Startup 文件夹的方式实现开机自启,解决管理员模式下开机自启的各种问题
- 增加 IP 信息请求重试机制,减少 Network Error 发生的情况
## v2.2.3

View File

@ -321,8 +321,17 @@ export const gc = async () => {
// Get current IP and geolocation information
export const getIpInfo = async () => {
// 添加重试机制
const maxRetries = 3;
const retryDelay = 1500;
const timeout = 5000;
let lastError;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
// 使用axios直接请求IP.sb的API不通过clash代理
const response = await axios.get("https://api.ip.sb/geoip");
const response = await axios.get("https://api.ip.sb/geoip", { timeout });
return response.data as {
ip: string;
country_code: string;
@ -336,4 +345,16 @@ export const getIpInfo = async () => {
latitude: number;
timezone: string;
};
} catch (error) {
console.log(`获取IP信息失败尝试 ${attempt + 1}/${maxRetries}`, error);
lastError = error;
// 如果不是最后一次尝试,则等待后重试
if (attempt < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
}
throw lastError;
};