Revert "refactor(WS): 使用连接池上下文管理器简化连接管理"
This reverts commit c851b49db9.
This commit is contained in:
63
core/WS.py
63
core/WS.py
@@ -127,14 +127,17 @@ class WS:
|
||||
while True:
|
||||
try:
|
||||
# 从连接池获取一个连接
|
||||
# 使用 connection 上下文管理器确保释放
|
||||
async with self.pool.connection() as conn:
|
||||
try:
|
||||
# 监听连接上的消息
|
||||
async for message in conn.conn:
|
||||
await self._handle_message(message, conn)
|
||||
except Exception as e:
|
||||
self.logger.error(f"连接 {conn.conn_id} 监听异常: {e}")
|
||||
conn = await self.pool.get_connection()
|
||||
|
||||
try:
|
||||
# 监听连接上的消息
|
||||
async for message in conn.conn:
|
||||
await self._handle_message(message, conn)
|
||||
except Exception as e:
|
||||
self.logger.error(f"连接 {conn.conn_id} 监听异常: {e}")
|
||||
finally:
|
||||
# 释放连接回连接池
|
||||
await self.pool.release_connection(conn)
|
||||
except Exception as e:
|
||||
self.logger.error(f"连接池监听循环异常: {e}")
|
||||
await asyncio.sleep(self.reconnect_interval)
|
||||
@@ -321,33 +324,27 @@ class WS:
|
||||
)
|
||||
|
||||
# 从连接池获取一个连接
|
||||
conn = await self.pool.get_connection()
|
||||
try:
|
||||
async with self.pool.connection() as conn:
|
||||
echo_id = str(uuid.uuid4())
|
||||
payload = {"action": action, "params": params or {}, "echo": echo_id}
|
||||
echo_id = str(uuid.uuid4())
|
||||
payload = {"action": action, "params": params or {}, "echo": echo_id}
|
||||
|
||||
await conn.send(orjson.dumps(payload))
|
||||
await conn.send(orjson.dumps(payload))
|
||||
|
||||
# 在当前连接上等待特定 echo 的响应,并设置超时
|
||||
try:
|
||||
async def wait_for_response():
|
||||
async for message in conn.conn:
|
||||
data = orjson.loads(message)
|
||||
|
||||
# 检查是否是我们要的响应
|
||||
if data.get("echo") == echo_id:
|
||||
return data
|
||||
|
||||
# 如果不是,可能是事件,需要分发
|
||||
if "post_type" in data:
|
||||
asyncio.create_task(self.on_event(data))
|
||||
|
||||
return await asyncio.wait_for(wait_for_response(), timeout=30.0)
|
||||
# 在当前连接上等待特定 echo 的响应,并设置超时
|
||||
try:
|
||||
async def wait_for_response():
|
||||
async for message in conn.conn:
|
||||
data = orjson.loads(message)
|
||||
if data.get("echo") == echo_id:
|
||||
return data
|
||||
|
||||
return await asyncio.wait_for(wait_for_response(), timeout=30.0)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
raise # 重新抛出超时异常
|
||||
except Exception as e:
|
||||
raise WebSocketError(f"在等待API响应时连接出错: {e}")
|
||||
except asyncio.TimeoutError:
|
||||
raise # 重新抛出超时异常
|
||||
except Exception as e:
|
||||
raise WebSocketError(f"在等待API响应时连接出错: {e}")
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
self.logger.warning(f"API 调用超时: action={action}, params={params}")
|
||||
@@ -363,6 +360,9 @@ class WS:
|
||||
message=f"API调用异常: {str(e)}",
|
||||
data={"action": action, "params": params}
|
||||
)
|
||||
finally:
|
||||
# 释放连接回连接池
|
||||
await self.pool.release_connection(conn)
|
||||
else:
|
||||
# 单连接模式
|
||||
if not self.ws:
|
||||
@@ -409,3 +409,4 @@ class WS:
|
||||
message=f"API调用异常: {str(e)}",
|
||||
data={"action": action, "params": params}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user