fix: Pipeline 初始化改为后台线程,不阻塞飞书长连接启动
- Pipeline 预初始化移到后台线程(不阻塞 FeishuLongConnService.__init__) - 消息处理时等待 Pipeline 就绪(最多 10 秒),超时则即时初始化 - Redis socket_connect_timeout 从 2s 降为 1s - 解决 Redis 连接慢导致整个飞书服务卡死的问题
This commit is contained in:
@@ -187,10 +187,11 @@ def create_session_store() -> SessionStore:
|
|||||||
password=config.redis.password,
|
password=config.redis.password,
|
||||||
db=config.redis.db,
|
db=config.redis.db,
|
||||||
decode_responses=True,
|
decode_responses=True,
|
||||||
socket_connect_timeout=2,
|
socket_connect_timeout=1,
|
||||||
socket_timeout=2
|
socket_timeout=1
|
||||||
)
|
)
|
||||||
client.ping()
|
client.ping()
|
||||||
|
logger.info(f"会话存储使用 Redis ({config.redis.host})")
|
||||||
return RedisSessionStore(client)
|
return RedisSessionStore(client)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(f"Redis 不可用({e}),会话存储使用内存")
|
logger.info(f"Redis 不可用({e}),会话存储使用内存")
|
||||||
|
|||||||
@@ -55,13 +55,16 @@ class FeishuLongConnService:
|
|||||||
self._executor = ThreadPoolExecutor(max_workers=5, thread_name_prefix="feishu_msg")
|
self._executor = ThreadPoolExecutor(max_workers=5, thread_name_prefix="feishu_msg")
|
||||||
self._msg_count = 0
|
self._msg_count = 0
|
||||||
|
|
||||||
# 预初始化 Pipeline(避免首条消息时懒加载导致超时)
|
# 预初始化 Pipeline(在后台线程,不阻塞启动)
|
||||||
|
self._pipeline = None
|
||||||
|
def _init_pipeline():
|
||||||
try:
|
try:
|
||||||
self._pipeline = service_manager.get_pipeline()
|
self._pipeline = service_manager.get_pipeline()
|
||||||
logger.info("✅ MessagePipeline 预初始化成功")
|
logger.info("✅ MessagePipeline 预初始化成功")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"❌ MessagePipeline 预初始化失败: {e}", exc_info=True)
|
logger.error(f"❌ MessagePipeline 预初始化失败: {e}", exc_info=True)
|
||||||
self._pipeline = None
|
import threading as _threading
|
||||||
|
_threading.Thread(target=_init_pipeline, daemon=True).start()
|
||||||
|
|
||||||
def _handle_message(self, data: P2ImMessageReceiveV1) -> None:
|
def _handle_message(self, data: P2ImMessageReceiveV1) -> None:
|
||||||
"""收到消息事件,立即提交到线程池,确保 SDK 回调快速返回"""
|
"""收到消息事件,立即提交到线程池,确保 SDK 回调快速返回"""
|
||||||
@@ -176,8 +179,16 @@ class FeishuLongConnService:
|
|||||||
# 使用 Pipeline 统一处理消息
|
# 使用 Pipeline 统一处理消息
|
||||||
session_user_id = f"feishu_{chat_id}_{sender_id}"
|
session_user_id = f"feishu_{chat_id}_{sender_id}"
|
||||||
try:
|
try:
|
||||||
logger.info(f"正在获取 Pipeline...")
|
# 等待 Pipeline 就绪(最多 10 秒)
|
||||||
pipeline = self._pipeline or service_manager.get_pipeline()
|
import time as _wait_time
|
||||||
|
for _ in range(100):
|
||||||
|
if self._pipeline:
|
||||||
|
break
|
||||||
|
_wait_time.sleep(0.1)
|
||||||
|
pipeline = self._pipeline
|
||||||
|
if not pipeline:
|
||||||
|
logger.warning("Pipeline 未就绪,尝试即时初始化")
|
||||||
|
pipeline = service_manager.get_pipeline()
|
||||||
logger.info(f"Pipeline 开始处理: user={session_user_id}")
|
logger.info(f"Pipeline 开始处理: user={session_user_id}")
|
||||||
response_data = pipeline.handle_message(
|
response_data = pipeline.handle_message(
|
||||||
user_id=session_user_id,
|
user_id=session_user_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user