fix: 修复 media_post 的 KeyError 日志冲突及 video 401 自动换 token
This commit is contained in:
1 parent
d35f71721c
commit
4467a60298
2 files changed
+32
-10
No files matched your search
@@ -1360,6 +1360,21 @@ class VideoService:
|
||||
|
||||
# Process response.
|
||||
if is_stream:
|
||||
try:
|
||||
stream_iter = response.__aiter__()
|
||||
# 诱发初始请求,若发生 401 等异常,将在 completions 的 try 块内被捕捉。
|
||||
first_chunk = await stream_iter.__anext__()
|
||||
except StopAsyncIteration:
|
||||
first_chunk = None
|
||||
|
||||
async def _merged_stream(first, remaining):
|
||||
if first is not None:
|
||||
yield first
|
||||
async for chunk in remaining:
|
||||
yield chunk
|
||||
|
||||
combined_response = _merged_stream(first_chunk, stream_iter)
|
||||
|
||||
processor = VideoStreamProcessor(
|
||||
model,
|
||||
token,
|
||||
@@ -1368,7 +1383,7 @@ class VideoService:
|
||||
idle_timeout_override=None,
|
||||
)
|
||||
return wrap_stream_with_usage(
|
||||
processor.process(response), token_mgr, token, model
|
||||
processor.process(combined_response), token_mgr, token, model
|
||||
)
|
||||
|
||||
result = await VideoCollectProcessor(
|
||||
@@ -1394,10 +1409,14 @@ class VideoService:
|
||||
|
||||
except UpstreamException as e:
|
||||
last_error = e
|
||||
if rate_limited(e):
|
||||
await token_mgr.mark_rate_limited(token)
|
||||
status_code = e.details.get("status") if e.details else getattr(e, "status_code", None)
|
||||
if rate_limited(e) or status_code == 401:
|
||||
if rate_limited(e):
|
||||
await token_mgr.mark_rate_limited(token)
|
||||
else:
|
||||
await token_mgr.record_fail(token, status_code, "video_auth_failed_401")
|
||||
logger.warning(
|
||||
f"Token {_token_tag(token)} rate limited (429), "
|
||||
f"Token {_token_tag(token)} {'rate limited (429)' if rate_limited(e) else 'auth failed (401)'}, "
|
||||
f"trying next token (attempt {attempt + 1}/{max_token_retries})"
|
||||
)
|
||||
continue
|
||||
|
||||
@@ -243,8 +243,9 @@ class MediaPostReverse:
|
||||
if len(content) > 300:
|
||||
content = f"{content[:300]}...(len={len(content)})"
|
||||
logger.error(
|
||||
"MediaPostReverse: Media post create failed, "
|
||||
f"status={response.status_code}, body={content or '-'}",
|
||||
"MediaPostReverse: Media post create failed, status={}, body={}",
|
||||
response.status_code,
|
||||
content or '-',
|
||||
extra={"error_type": "UpstreamException"},
|
||||
)
|
||||
raise UpstreamException(
|
||||
@@ -342,8 +343,9 @@ class MediaPostReverse:
|
||||
if len(content) > 300:
|
||||
content = f"{content[:300]}...(len={len(content)})"
|
||||
logger.error(
|
||||
"MediaPostReverse: Media post get failed, "
|
||||
f"status={response.status_code}, body={content or '-'}",
|
||||
"MediaPostReverse: Media post get failed, status={}, body={}",
|
||||
response.status_code,
|
||||
content or '-',
|
||||
extra={"error_type": "UpstreamException"},
|
||||
)
|
||||
raise UpstreamException(
|
||||
@@ -442,8 +444,9 @@ class MediaPostReverse:
|
||||
if len(content) > 300:
|
||||
content = f"{content[:300]}...(len={len(content)})"
|
||||
logger.error(
|
||||
"MediaPostReverse: Media post create-link failed, "
|
||||
f"status={response.status_code}, body={content or '-'}",
|
||||
"MediaPostReverse: Media post create-link failed, status={}, body={}",
|
||||
response.status_code,
|
||||
content or '-',
|
||||
extra={"error_type": "UpstreamException"},
|
||||
)
|
||||
raise UpstreamException(
|
||||
|
||||
Reference in new issue
Block a user