优化长连接,等待逻辑

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 from typing import Dict, Any, Tuple, Optional, List
import aiohttp import aiohttp
from aiohttp import ClientTimeout
from loguru import logger from loguru import logger
from base.plugin_common.message_plugin_interface import MessagePluginInterface from base.plugin_common.message_plugin_interface import MessagePluginInterface
@@ -233,7 +234,9 @@ class MessageSummaryPlugin(MessagePluginInterface):
} }
try: 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: async with session.post(self._api_url, headers=headers, json=data) as response:
response.raise_for_status() # 检查请求是否成功 response.raise_for_status() # 检查请求是否成功
response_data = await response.json() 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_start = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
yesterday_end = yesterday.replace(hour=23, minute=59, second=59, microsecond=999999) 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() all_groups = GroupBotManager.get_group_list()
@@ -361,7 +365,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.LOG.info(f"{group_id} 昨天聊天记录为空,跳过总结") self.LOG.info(f"{group_id} 昨天聊天记录为空,跳过总结")
continue 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( await self.bot.send_text_message(