Parcourir la source

修改记录的统计来源

kobe6258 il y a 3 semaines
Parent
commit
fa518a0fab
3 fichiers modifiés avec 8 ajouts et 3 suppressions
  1. 0 2
      utils/ws/latency_measurer.go
  2. 4 1
      utils/ws/session.go
  3. 4 0
      utils/ws/session_manager.go

+ 0 - 2
utils/ws/latency_measurer.go

@@ -2,7 +2,6 @@ package ws
 
 import (
 	"errors"
-	"fmt"
 	"github.com/gorilla/websocket"
 	"sync"
 	"time"
@@ -87,7 +86,6 @@ func (lm *LatencyMeasurer) GetLatency() time.Duration {
 func SetupLatencyMeasurement(conn *websocket.Conn) *LatencyMeasurer {
 	lm := NewLatencyMeasurer(5) // 使用最近5次测量的滑动窗口
 	conn.SetPongHandler(func(appData string) error {
-		fmt.Println("Pong received")
 		lm.CalculateLatency()
 		return nil
 	})

+ 4 - 1
utils/ws/session.go

@@ -52,6 +52,7 @@ func (s *Session) readPump() {
 		if err = manager.HandleMessage(s.UserId, s.Id, message); err != nil {
 			//写应答
 			_ = s.writeWithTimeout(err.Error())
+			_ = s.writeWithTimeout("<EOF/>")
 		}
 	}
 }
@@ -77,6 +78,7 @@ func (s *Session) writeWithTimeout(msg string) error {
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	if s.Conn == nil {
+		utils.FileLog.Error("写入消息失败,connection已关闭")
 		return errors.New("connection closed")
 	}
 	// 设置写超时
@@ -110,6 +112,7 @@ func (s *Session) writePump() {
 	}
 }
 func handleCloseError(err error) {
+	utils.FileLog.Error("websocket错误关闭 %s closed", err.Error())
 	if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
 		var wsErr *websocket.CloseError
 		if !errors.As(err, &wsErr) {
@@ -122,7 +125,7 @@ func handleCloseError(err error) {
 				utils.FileLog.Info("正常关闭连接")
 			default:
 				fmt.Printf("websocket关闭代码 %d:%s", wsErr.Code, wsErr.Text)
-				utils.FileLog.Error("关闭代码:%d:%s", wsErr.Code, wsErr.Text)
+				utils.FileLog.Error(":%d:%s", wsErr.Code, wsErr.Text)
 			}
 		}
 	}

+ 4 - 0
utils/ws/session_manager.go

@@ -74,6 +74,7 @@ func (manager *ConnectionManager) HandleMessage(userID int, sessionID string, me
 	var userMessage Message
 	err := json.Unmarshal(message, &userMessage)
 	if err != nil {
+		utils.FileLog.Error(fmt.Sprintf("消息格式错误:%s", string(message)))
 		fmt.Printf("消息格式错误:%s", string(message))
 		return errors.New("消息格式错误:" + err.Error())
 	}
@@ -101,14 +102,17 @@ func (manager *ConnectionManager) HandleMessage(userID int, sessionID string, me
 		}
 	}()
 	if resp == nil {
+		utils.FileLog.Error("知识库问答失败: 无应答")
 		return errors.New("知识库问答失败: 无应答")
 	}
 	if err != nil {
+		utils.FileLog.Error(fmt.Sprintf("知识库问答失败: httpCode:%d,错误信息:%s", resp.StatusCode, http.StatusText(resp.StatusCode)))
 		err = errors.New(fmt.Sprintf("知识库问答失败: httpCode:%d,错误信息:%s", resp.StatusCode, http.StatusText(resp.StatusCode)))
 		return err
 	}
 
 	if resp.StatusCode != http.StatusOK {
+		utils.FileLog.Error(fmt.Sprintf("知识库问答失败: httpCode:%d,错误信息:%s", resp.StatusCode, http.StatusText(resp.StatusCode)))
 		err = errors.New(fmt.Sprintf("知识库问答失败: httpCode:%d,错误信息:%s", resp.StatusCode, http.StatusText(resp.StatusCode)))
 		return err
 	}