Skip to content

Commit

Permalink
perf: 更新WebSocketCrawler的日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnserf-Seed committed Nov 15, 2024
1 parent 0863635 commit b0e9a77
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions f2/crawlers/base_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,21 @@ async def receive_messages(self):
)

timeout_count = 0

while True:
try:
message = await asyncio.wait_for(
self.websocket.recv(), timeout=self.timeout
)
# 为wss连接设置10秒超时机制
timestamp = timestamp_2_str(time.time(), "%Y-%m-%d %H:%M:%S")
logger.info(
_("[ReceiveMessages] | [⏳ 接收消息 {0}]").format(
timestamp_2_str(time.time(), "%Y-%m-%d %H:%M:%S")
)
_("[ReceiveMessages] | [⏳ 接收消息 {0}]").format(timestamp)
)

timeout_count = 0 # 重置超时计数
await self.on_message(message)

except asyncio.TimeoutError:
timeout_count += 1
logger.warning(
Expand All @@ -543,11 +545,18 @@ async def receive_messages(self):
)
)
if timeout_count >= 3:
logger.warning(_("[ReceiveMessages] [❌ 多次超时,关闭连接]"))
logger.warning(
_(
"[ReceiveMessages] [❌ 超时关闭连接] | "
"[超时次数:{0}] [连接状态:{1}]"
).format(timeout_count, self.websocket.closed)
)
return "closed"
if self.websocket.closed:
logger.warning(
_("[ReceiveMessages] [🔒 服务器关闭] | [WebSocket 连接结束]")
_(
"[ReceiveMessages] [🔒 远程服务器关闭] | [WebSocket 连接结束]"
)
)
return "closed"
except ConnectionClosedError as exc:
Expand All @@ -556,11 +565,13 @@ async def receive_messages(self):
_("[ReceiveMessages] [🔌 连接关闭] | [原因:{0}]").format(exc)
)
return "closed"

except ConnectionClosedOK:
logger.info(
_("[ReceiveMessages] [✔️ 正常关闭] | [WebSocket 连接正常关闭]")
)
return "closed"

except Exception as exc:
logger.debug(traceback.format_exc())
logger.error(
Expand Down

0 comments on commit b0e9a77

Please sign in to comment.