From 7172f3ab04551ba4ba9822406ee0e241cda2b8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AC=E5=8F=B8git?= <240241002@qq.com> Date: Mon, 2 Feb 2026 17:05:31 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(config):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E8=B6=85=E6=97=B6=E9=85=8D=E7=BD=AE=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了多种场景的代理超时配置: - 默认超时:60秒 - 短任务超时:30秒(轮询、小图片下载) - 长任务超时:300秒(视频下载、大文件) - 生成任务超时:600秒(AI生成请求) refactor(services): 统一使用配置化的代理超时参数 将硬编码的超时值替换为配置文件中的常量: - 文件服务使用默认超时 - 聊天生成功能使用长任务超时 - 图片下载使用短任务超时 - 视频生成功能使用相应的默认、短、长超时配置 ``` --- config.py | 6 ++++++ services/file_service.py | 2 +- services/generation_service.py | 2 +- services/task_service.py | 10 +++++----- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/config.py b/config.py index 1c51c0f..1461265 100644 --- a/config.py +++ b/config.py @@ -70,3 +70,9 @@ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlDx4KdOtOQE+tBq6jHKKFenRaRe2gbBnleBk # Proxy Config USE_PROXY = True # 是否开启代理 PROXY_URL = "https://nas.4x4g.com:10011/api/tool/proxy/?url=" + + # 代理超时配置 (单位:秒) + PROXY_TIMEOUT_DEFAULT = 60 # 默认超时 + PROXY_TIMEOUT_SHORT = 30 # 短任务超时 (轮询、小图片下载) + PROXY_TIMEOUT_LONG = 300 # 长任务超时 (5分钟,视频下载、大文件) + PROXY_TIMEOUT_GENERATION = 600 # 生成任务超时 (10分钟,AI 生成请求) diff --git a/services/file_service.py b/services/file_service.py index 20232e7..d35b638 100644 --- a/services/file_service.py +++ b/services/file_service.py @@ -23,7 +23,7 @@ def handle_file_uploads(files): def get_remote_file_stream(url): """获取远程文件的流""" - req = requests.get(get_proxied_url(url), stream=True, timeout=60) + req = requests.get(get_proxied_url(url), stream=True, timeout=Config.PROXY_TIMEOUT_DEFAULT) req.raise_for_status() headers = {} diff --git a/services/generation_service.py b/services/generation_service.py index 7690e37..3aa73e4 100644 --- a/services/generation_service.py +++ b/services/generation_service.py @@ -89,7 +89,7 @@ def handle_chat_generation_sync(user_id, api_key, model_value, prompt, use_trial "messages": [{"role": "user", "content": prompt}] } try: - resp = requests.post(get_proxied_url(Config.CHAT_API), json=chat_payload, headers=headers, timeout=120) + resp = requests.post(get_proxied_url(Config.CHAT_API), json=chat_payload, headers=headers, timeout=Config.PROXY_TIMEOUT_LONG) if resp.status_code != 200: if use_trial: refund_points(user_id, cost) diff --git a/services/task_service.py b/services/task_service.py index 7442e39..84cdf8a 100644 --- a/services/task_service.py +++ b/services/task_service.py @@ -23,7 +23,7 @@ def sync_images_background(app, record_id, raw_urls): success = False for attempt in range(3): # 3 次重试机制 try: - img_resp = requests.get(get_proxied_url(raw_url), timeout=30) + img_resp = requests.get(get_proxied_url(raw_url), timeout=Config.PROXY_TIMEOUT_SHORT) if img_resp.status_code == 200: content = img_resp.content ext = ".png" @@ -99,7 +99,7 @@ def process_image_generation(app, user_id, task_id, payload, api_key, target_api try: headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} # 使用较长的超时时间 (10分钟),确保长耗时任务不被中断 - resp = requests.post(get_proxied_url(target_api), json=payload, headers=headers, timeout=1000) + resp = requests.post(get_proxied_url(target_api), json=payload, headers=headers, timeout=Config.PROXY_TIMEOUT_GENERATION) if resp.status_code != 200: if use_trial: @@ -152,7 +152,7 @@ def sync_video_background(app, record_id, raw_url, internal_task_id=None): for attempt in range(3): try: # 增加了流式下载,处理大视频文件 - with requests.get(get_proxied_url(raw_url), stream=True, timeout=120) as r: + with requests.get(get_proxied_url(raw_url), stream=True, timeout=Config.PROXY_TIMEOUT_LONG) as r: r.raise_for_status() content_type = r.headers.get('content-type', 'video/mp4') ext = ".mp4" @@ -210,7 +210,7 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c try: headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"} # 1. 提交任务 - submit_resp = requests.post(get_proxied_url(Config.VIDEO_GEN_API), json=payload, headers=headers, timeout=60) + submit_resp = requests.post(get_proxied_url(Config.VIDEO_GEN_API), json=payload, headers=headers, timeout=Config.PROXY_TIMEOUT_DEFAULT) if submit_resp.status_code != 200: raise Exception(f"视频任务提交失败: {submit_resp.text}") @@ -234,7 +234,7 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c time.sleep(10) poll_url = Config.VIDEO_POLL_API.format(task_id=remote_task_id) - poll_resp = requests.get(get_proxied_url(poll_url), headers=headers, timeout=30) + poll_resp = requests.get(get_proxied_url(poll_url), headers=headers, timeout=Config.PROXY_TIMEOUT_SHORT) if poll_resp.status_code != 200: continue