fix: 优化 HTTP 超时配置,使用细粒度的 httpx.Timeout 对象#63
Open
leeight wants to merge 3 commits intofuergaosi233:mainfrom
Open
fix: 优化 HTTP 超时配置,使用细粒度的 httpx.Timeout 对象#63leeight wants to merge 3 commits intofuergaosi233:mainfrom
leeight wants to merge 3 commits intofuergaosi233:mainfrom
Conversation
问题: - 频繁出现 httpx.ReadTimeout 错误 - 原有的单一超时配置 (90秒) 对流式响应不够灵活 - 没有区分连接超时和读取超时 优化方案: 1. 引入细粒度超时配置: - CONNECT_TIMEOUT: 连接超时 (默认 10秒) - 快速检测连接失败 - READ_TIMEOUT: 读取超时 (默认 600秒) - 允许长时间的流式响应 - WRITE_TIMEOUT: 写入超时 (默认 10秒) - 防止慢速请求上传 - POOL_TIMEOUT: 连接池超时 (默认 10秒) - 避免连接池饥饿 2. 使用 httpx.Timeout 对象替代单一超时值 3. 保留 REQUEST_TIMEOUT 用于向后兼容 4. 更新配置文件和帮助信息 影响文件: - src/core/config.py: 添加新的超时环境变量 - src/core/client.py: 使用 httpx.Timeout 对象 - src/api/endpoints.py: 传递新的超时参数 - .env.example: 添加新的超时配置示例 - src/main.py: 更新帮助信息和启动日志 此优化应该能够有效解决 Claude API 长时间流式响应导致的超时问题。
问题:
- 请求失败时缺少详细的错误信息
- 难以追踪和分析生产环境中的错误
改进内容:
1. **src/core/client.py** - OpenAI 客户端错误日志:
- 添加 logger 和 traceback 导入
- 为所有异常类型添加详细日志:
* 认证错误 (AuthenticationError)
* 速率限制错误 (RateLimitError)
* 错误请求 (BadRequestError)
* API 错误 (APIError)
* 未知错误 (Exception)
- 记录内容包括:
* 请求 ID
* 错误类型和详细信息
* 请求模型和消息数量
* 完整的请求数据(DEBUG 级别)
* 完整的堆栈跟踪
- 区分流式和非流式请求的日志([Stream] 标记)
2. **src/api/endpoints.py** - API 端点错误日志:
- 在请求开始时记录请求摘要(request_id, model, stream, messages_count)
- 增强流式响应错误日志
- 增强通用错误处理日志
- 为 count_tokens 端点添加错误日志
- 为 test_connection 端点增强错误日志
- 所有日志都带有 request_id 便于追踪
3. **src/conversion/response_converter.py** - 响应转换错误日志:
- 为 convert_openai_to_claude_response 添加详细错误日志
- 增强流式转换器的错误日志
- 记录取消请求的信息
- 添加 [Converter] 标记便于识别
日志信息包括:
- ✅ 请求 ID(便于追踪完整的请求生命周期)
- ✅ 错误类型(Exception 类名)
- ✅ 请求详细信息(模型、消息数、参数等)
- ✅ 完整的堆栈跟踪
- ✅ 区分不同来源的错误([Stream], [Converter] 等)
预期效果:
- 更容易定位生产环境中的错误
- 提供足够的上下文信息用于问题分析
- 便于追踪请求的完整生命周期
- 方便后续优化和改进
问题: - 之前日志只输出到控制台,无法持久化保存 - 生产环境中难以查看历史日志和分析问题 新增功能: 1. **日志文件写入**: - 默认日志文件路径:`logs/proxy.log` - 自动创建 logs 目录 - 使用 UTF-8 编码 2. **日志轮转**: - 使用 RotatingFileHandler 实现自动日志轮转 - 单个日志文件最大 10MB(可配置) - 保留 5 个备份文件(可配置) - 文件命名:proxy.log, proxy.log.1, proxy.log.2 等 3. **灵活配置**(通过环境变量): - `LOG_FILE_PATH`: 日志文件路径(默认:logs/proxy.log) - `LOG_FILE_MAX_BYTES`: 单个文件最大字节数(默认:10485760 = 10MB) - `LOG_FILE_BACKUP_COUNT`: 备份文件数量(默认:5) - `LOG_TO_CONSOLE`: 是否同时输出到控制台(默认:true) 4. **双输出模式**: - 默认同时输出到文件和控制台 - 可通过 LOG_TO_CONSOLE=false 关闭控制台输出 修改的文件: - **src/core/config.py**: 添加日志配置参数 - **src/core/logging.py**: 实现文件日志和轮转功能 - **.env.example**: 添加日志配置示例和说明 - **.gitignore**: 忽略 logs/ 目录和 *.log 文件 使用示例: ```bash # 使用默认配置(日志写入 logs/proxy.log) python src/main.py # 自定义日志路径 export LOG_FILE_PATH="/var/log/claude-proxy/proxy.log" python src/main.py # 只输出到文件,不输出到控制台 export LOG_TO_CONSOLE=false python src/main.py ``` 预期效果: - ✅ 日志自动持久化到文件 - ✅ 文件大小自动管理(10MB 轮转) - ✅ 历史日志自动备份 - ✅ 方便生产环境问题分析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题:
优化方案:
引入细粒度超时配置:
使用 httpx.Timeout 对象替代单一超时值
保留 REQUEST_TIMEOUT 用于向后兼容
更新配置文件和帮助信息
影响文件:
此优化应该能够有效解决 Claude API 长时间流式响应导致的超时问题。