fix: agent ws connect panic

This commit is contained in:
engigu
2026-01-01 14:13:14 +08:00
parent ac44bc450b
commit 96c0b73985
+6
View File
@@ -410,6 +410,7 @@ func (c *AgentController) wsReadPump(ac *services.AgentConnection, agent *models
if r := recover(); r != nil { if r := recover(); r != nil {
logger.Errorf("[AgentWS] Agent #%d wsReadPump panic: %v", agent.ID, r) logger.Errorf("[AgentWS] Agent #%d wsReadPump panic: %v", agent.ID, r)
} }
logger.Infof("[AgentWS] Agent #%d wsReadPump 退出", agent.ID)
c.wsManager.Unregister(agent.ID, ac) c.wsManager.Unregister(agent.ID, ac)
}() }()
@@ -450,6 +451,7 @@ func (c *AgentController) wsWritePump(ac *services.AgentConnection) {
if r := recover(); r != nil { if r := recover(); r != nil {
logger.Errorf("[AgentWS] Agent #%d wsWritePump panic: %v", ac.AgentID, r) logger.Errorf("[AgentWS] Agent #%d wsWritePump panic: %v", ac.AgentID, r)
} }
logger.Infof("[AgentWS] Agent #%d wsWritePump 退出", ac.AgentID)
}() }()
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(30 * time.Second)
@@ -459,12 +461,15 @@ func (c *AgentController) wsWritePump(ac *services.AgentConnection) {
select { select {
case message, ok := <-ac.Send: case message, ok := <-ac.Send:
if !ok { if !ok {
logger.Warnf("[AgentWS] Agent #%d Send channel 已关闭", ac.AgentID)
return return
} }
if ac.IsClosed() { if ac.IsClosed() {
logger.Warnf("[AgentWS] Agent #%d 连接已关闭(write)", ac.AgentID)
return return
} }
if err := ac.WriteMessage(message); err != nil { if err := ac.WriteMessage(message); err != nil {
logger.Warnf("[AgentWS] Agent #%d 写入消息失败: %v", ac.AgentID, err)
return return
} }
case <-ticker.C: case <-ticker.C:
@@ -472,6 +477,7 @@ func (c *AgentController) wsWritePump(ac *services.AgentConnection) {
return return
} }
if err := ac.WritePing(); err != nil { if err := ac.WritePing(); err != nil {
logger.Warnf("[AgentWS] Agent #%d 发送 Ping 失败: %v", ac.AgentID, err)
return return
} }
} }