```
feat(config): 添加代理超时配置选项 添加了多种场景的代理超时配置: - 默认超时:60秒 - 短任务超时:30秒(轮询、小图片下载) - 长任务超时:300秒(视频下载、大文件) - 生成任务超时:600秒(AI生成请求) refactor(services): 统一使用配置化的代理超时参数 将硬编码的超时值替换为配置文件中的常量: - 文件服务使用默认超时 - 聊天生成功能使用长任务超时 - 图片下载使用短任务超时 - 视频生成功能使用相应的默认、短、长超时配置 ```
This commit is contained in:
parent
1b7fcd603c
commit
7172f3ab04
@ -70,3 +70,9 @@ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlDx4KdOtOQE+tBq6jHKKFenRaRe2gbBnleBk
|
|||||||
# Proxy Config
|
# Proxy Config
|
||||||
USE_PROXY = True # 是否开启代理
|
USE_PROXY = True # 是否开启代理
|
||||||
PROXY_URL = "https://nas.4x4g.com:10011/api/tool/proxy/?url="
|
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 生成请求)
|
||||||
|
|||||||
@ -23,7 +23,7 @@ def handle_file_uploads(files):
|
|||||||
|
|
||||||
def get_remote_file_stream(url):
|
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()
|
req.raise_for_status()
|
||||||
|
|
||||||
headers = {}
|
headers = {}
|
||||||
|
|||||||
@ -89,7 +89,7 @@ def handle_chat_generation_sync(user_id, api_key, model_value, prompt, use_trial
|
|||||||
"messages": [{"role": "user", "content": prompt}]
|
"messages": [{"role": "user", "content": prompt}]
|
||||||
}
|
}
|
||||||
try:
|
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 resp.status_code != 200:
|
||||||
if use_trial:
|
if use_trial:
|
||||||
refund_points(user_id, cost)
|
refund_points(user_id, cost)
|
||||||
|
|||||||
@ -23,7 +23,7 @@ def sync_images_background(app, record_id, raw_urls):
|
|||||||
success = False
|
success = False
|
||||||
for attempt in range(3): # 3 次重试机制
|
for attempt in range(3): # 3 次重试机制
|
||||||
try:
|
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:
|
if img_resp.status_code == 200:
|
||||||
content = img_resp.content
|
content = img_resp.content
|
||||||
ext = ".png"
|
ext = ".png"
|
||||||
@ -99,7 +99,7 @@ def process_image_generation(app, user_id, task_id, payload, api_key, target_api
|
|||||||
try:
|
try:
|
||||||
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
||||||
# 使用较长的超时时间 (10分钟),确保长耗时任务不被中断
|
# 使用较长的超时时间 (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 resp.status_code != 200:
|
||||||
if use_trial:
|
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):
|
for attempt in range(3):
|
||||||
try:
|
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()
|
r.raise_for_status()
|
||||||
content_type = r.headers.get('content-type', 'video/mp4')
|
content_type = r.headers.get('content-type', 'video/mp4')
|
||||||
ext = ".mp4"
|
ext = ".mp4"
|
||||||
@ -210,7 +210,7 @@ def process_video_generation(app, user_id, internal_task_id, payload, api_key, c
|
|||||||
try:
|
try:
|
||||||
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
|
||||||
# 1. 提交任务
|
# 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:
|
if submit_resp.status_code != 200:
|
||||||
raise Exception(f"视频任务提交失败: {submit_resp.text}")
|
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)
|
time.sleep(10)
|
||||||
poll_url = Config.VIDEO_POLL_API.format(task_id=remote_task_id)
|
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:
|
if poll_resp.status_code != 200:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user