fix: 优化请求超时配置

This commit is contained in:
shaw
2025-09-08 16:34:12 +08:00
parent 399e6b9d8c
commit fec80a16fa
7 changed files with 19 additions and 12 deletions

View File

@@ -34,11 +34,14 @@ CLAUDE_API_VERSION=2023-06-01
CLAUDE_BETA_HEADER=claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14
# 🌐 代理配置
DEFAULT_PROXY_TIMEOUT=60000
DEFAULT_PROXY_TIMEOUT=600000
MAX_PROXY_RETRIES=3
# IP协议族配置true=IPv4, false=IPv6, 默认IPv4兼容性更好
PROXY_USE_IPV4=true
# ⏱️ 请求超时配置
REQUEST_TIMEOUT=600000 # 请求超时设置毫秒默认10分钟
# 📈 使用限制
DEFAULT_TOKEN_LIMIT=1000000

View File

@@ -64,12 +64,15 @@ const config = {
// 🌐 代理配置
proxy: {
timeout: parseInt(process.env.DEFAULT_PROXY_TIMEOUT) || 30000,
timeout: parseInt(process.env.DEFAULT_PROXY_TIMEOUT) || 600000, // 10分钟
maxRetries: parseInt(process.env.MAX_PROXY_RETRIES) || 3,
// IP协议族配置true=IPv4, false=IPv6, 默认IPv4兼容性更好
useIPv4: process.env.PROXY_USE_IPV4 !== 'false' // 默认 true只有明确设置为 'false' 才使用 IPv6
},
// ⏱️ 请求超时配置
requestTimeout: parseInt(process.env.REQUEST_TIMEOUT) || 600000, // 默认 10 分钟
// 📈 使用限制
limits: {
defaultTokenLimit: parseInt(process.env.DEFAULT_TOKEN_LIMIT) || 1000000

View File

@@ -2,6 +2,7 @@ const express = require('express')
const axios = require('axios')
const router = express.Router()
const logger = require('../utils/logger')
const config = require('../../config/config')
const { authenticateApiKey } = require('../middleware/auth')
const unifiedOpenAIScheduler = require('../services/unifiedOpenAIScheduler')
const openaiAccountService = require('../services/openaiAccountService')
@@ -179,7 +180,7 @@ const handleResponses = async (req, res) => {
// 配置请求选项
const axiosConfig = {
headers,
timeout: 60 * 1000 * 10,
timeout: config.requestTimeout || 600000,
validateStatus: () => true
}

View File

@@ -122,7 +122,7 @@ class ClaudeConsoleRelayService {
...filteredHeaders
},
httpsAgent: proxyAgent,
timeout: config.proxy.timeout || 60000,
timeout: config.requestTimeout || 600000,
signal: abortController.signal,
validateStatus: () => true // 接受所有状态码
}
@@ -346,7 +346,7 @@ class ClaudeConsoleRelayService {
...filteredHeaders
},
httpsAgent: proxyAgent,
timeout: config.proxy.timeout || 60000,
timeout: config.requestTimeout || 600000,
responseType: 'stream',
validateStatus: () => true // 接受所有状态码
}

View File

@@ -680,7 +680,7 @@ class ClaudeRelayService {
...finalHeaders
},
agent: proxyAgent,
timeout: config.proxy.timeout
timeout: config.requestTimeout || 600000
}
// 使用统一 User-Agent 或客户端提供的,最后使用默认值
@@ -951,7 +951,7 @@ class ClaudeRelayService {
...finalHeaders
},
agent: proxyAgent,
timeout: config.proxy.timeout
timeout: config.requestTimeout || 600000
}
// 使用统一 User-Agent 或客户端提供的,最后使用默认值
@@ -1456,7 +1456,7 @@ class ClaudeRelayService {
...filteredHeaders
},
agent: proxyAgent,
timeout: config.proxy.timeout
timeout: config.requestTimeout || 600000
}
// 如果客户端没有提供 User-Agent使用默认值

View File

@@ -273,7 +273,7 @@ async function sendGeminiRequest({
'Content-Type': 'application/json'
},
data: requestBody,
timeout: config.requestTimeout || 120000
timeout: config.requestTimeout || 600000
}
// 添加代理配置
@@ -382,7 +382,7 @@ async function getAvailableModels(accessToken, proxy, projectId, location = 'us-
headers: {
Authorization: `Bearer ${accessToken}`
},
timeout: 30000
timeout: config.requestTimeout || 600000
}
const proxyAgent = createProxyAgent(proxy)
@@ -482,7 +482,7 @@ async function countTokens({
'X-Goog-User-Project': projectId || undefined
},
data: requestBody,
timeout: 30000
timeout: config.requestTimeout || 600000
}
// 添加代理配置

View File

@@ -138,7 +138,7 @@ async function refreshAccessToken(refreshToken, proxy = null) {
'Content-Length': requestData.length
},
data: requestData,
timeout: 30000 // 30秒超时
timeout: config.requestTimeout || 600000 // 使用统一的请求超时配置
}
// 配置代理(如果有)