优化长连接,等待逻辑

This commit is contained in:
liuwei
2026-02-04 09:42:05 +08:00
parent 5ce286bdb8
commit db90896775

View File

@@ -7,6 +7,7 @@ from pathlib import Path
from typing import Dict, Any, Tuple, Optional, List
import aiohttp
from aiohttp import ClientTimeout
from loguru import logger
from base.plugin_common.message_plugin_interface import MessagePluginInterface
@@ -174,7 +175,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
max_length = 2000
if len(summary) > max_length:
summary = summary[:max_length] + "\n\n... (内容过长,已截断)"
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id, summary)
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 30)
self.LOG.info(f"图片生成失败,已发送文本总结到群 {group_id}")
@@ -185,7 +186,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
"❌ 生成总结失败,请稍后再试!")
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 5)
return False
except Exception as e:
self.LOG.error(f"异步生成总结失败: {e}")
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id,
@@ -233,7 +234,9 @@ class MessageSummaryPlugin(MessagePluginInterface):
}
try:
async with aiohttp.ClientSession() as session:
custom_timeout = ClientTimeout(total=None, connect=10, sock_read=300)
conn = aiohttp.TCPConnector(keepalive_timeout=60) # 保持连接活跃
async with aiohttp.ClientSession(connector=conn, timeout=custom_timeout) as session:
async with session.post(self._api_url, headers=headers, json=data) as response:
response.raise_for_status() # 检查请求是否成功
response_data = await response.json()
@@ -304,7 +307,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
yesterday_start = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
yesterday_end = yesterday.replace(hour=23, minute=59, second=59, microsecond=999999)
self.LOG.info(f"总结时间范围: {yesterday_start.strftime('%Y-%m-%d %H:%M:%S')}{yesterday_end.strftime('%Y-%m-%d %H:%M:%S')}")
self.LOG.info(
f"总结时间范围: {yesterday_start.strftime('%Y-%m-%d %H:%M:%S')}{yesterday_end.strftime('%Y-%m-%d %H:%M:%S')}")
# 获取所有启用了群机器人的群聊
all_groups = GroupBotManager.get_group_list()
@@ -361,7 +365,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.LOG.info(f"{group_id} 昨天聊天记录为空,跳过总结")
continue
self.LOG.info(f"开始为群 {group_name} 生成总结,消息数量: {message_count},内容长度: {len(chat_content)}")
self.LOG.info(
f"开始为群 {group_name} 生成总结,消息数量: {message_count},内容长度: {len(chat_content)}")
# 发送提示消息
await self.bot.send_text_message(