From 7f85a0adeb74ba5c4a97ea20627445c3b43f41a5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 31 Oct 2023 14:23:08 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 4 +- .../websocket/internal/logic/getstatlogic.go | 6 +++ .../internal/logic/ws_render_image.go | 5 +- .../websocket/internal/logic/ws_statistics.go | 49 +++++++++++++------ server/websocket/internal/types/types.go | 5 +- server_api/websocket.api | 5 +- 6 files changed, 51 insertions(+), 23 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 848368a0..d9dbfde8 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -253,7 +253,7 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use } ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, websocket_data.ConnectSuccessMsg{Wid: uniqueId})) //发送累加统计连接数 - increaseWebsocketConnectCount() + increaseWebsocketConnectCount(userInfo.UserId, userInfo.GuestId) return ws, nil } @@ -336,7 +336,7 @@ func (w *wsConnectItem) close() { //删除用户级索引 deleteUserConnPoolElement(w.userId, w.guestId, w.uniqueId) //减少连接数统计 - decreaseWebsocketConnectCount() + decreaseWebsocketConnectCount(w.userId, w.guestId) } logx.Info("###websocket:", w.uniqueId, " uid:", w.userId, " gid:", w.guestId, " is closed") } diff --git a/server/websocket/internal/logic/getstatlogic.go b/server/websocket/internal/logic/getstatlogic.go index b36029ee..c7a21d1f 100644 --- a/server/websocket/internal/logic/getstatlogic.go +++ b/server/websocket/internal/logic/getstatlogic.go @@ -33,9 +33,15 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) ( if req.Password != "fusen1314" { return resp.SetStatusWithMessage(basic.CodeOK, "你干嘛,哎哟") } + userStat := make(map[interface{}]int) + mapUserWsStat.Range(func(key, value any) bool { + userStat[key] = value.(int) + return true + }) return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{ WsTotalCount: currentWebsocketConnectCount, CurRequestCombineCount: currentRequestCombineApiCount, + UserWsStat: userStat, }) } diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index f1fa2164..b1dfa1c3 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -15,7 +15,6 @@ import ( "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" "strconv" - "strings" "time" ) @@ -210,9 +209,9 @@ func (w *wsConnectItem) renderImage(renderImageData websocket_data.RenderImageRe return } //累增websocket请求合图数 - increaseCombineRequestCount() + increaseCombineRequestCount(w.userId, w.guestId) //请求完要释放 - defer decreaseCombineRequestCount() + defer decreaseCombineRequestCount(w.userId, w.guestId) //获取刀版图 combineReq := repositories.LogoCombineReq{ UserId: renderImageData.RenderData.UserId, diff --git a/server/websocket/internal/logic/ws_statistics.go b/server/websocket/internal/logic/ws_statistics.go index 88f1e32d..9914df2e 100644 --- a/server/websocket/internal/logic/ws_statistics.go +++ b/server/websocket/internal/logic/ws_statistics.go @@ -2,7 +2,9 @@ package logic import ( "context" + "fmt" "github.com/zeromicro/go-zero/core/logx" + "sync" ) // 统计信息 @@ -11,6 +13,8 @@ var ( currentWebsocketConnectCount = 0 //当前合图进行的请求总数 currentRequestCombineApiCount = 0 + //用户连接统计 + mapUserWsStat = sync.Map{} //添加or减少连接的控制chan websocketStat = make(chan websocketStatItem, 20) ) @@ -23,39 +27,49 @@ const ( ) type websocketStatItem struct { - Type websocketStatType `json:"type"` //类型 - Value int `json:"value"` //数值 + UserId int64 `json:"user_id"` + GuestId int64 `json:"guest_id"` + Type websocketStatType `json:"type"` //类型 + Value int `json:"value"` //数值 } // 累增ws连接数计数 -func increaseWebsocketConnectCount() { +func increaseWebsocketConnectCount(userId, guestId int64) { websocketStat <- websocketStatItem{ - Type: TYPE_CONNECT_COUNT, - Value: 1, + UserId: userId, + GuestId: guestId, + Type: TYPE_CONNECT_COUNT, + Value: 1, } } // 减少ws连接数计数 -func decreaseWebsocketConnectCount() { +func decreaseWebsocketConnectCount(userId, guestId int64) { websocketStat <- websocketStatItem{ - Type: TYPE_CONNECT_COUNT, - Value: -1, + UserId: userId, + GuestId: guestId, + Type: TYPE_CONNECT_COUNT, + Value: -1, } } // 累增合图请求数计数 -func increaseCombineRequestCount() { +func increaseCombineRequestCount(userId, guestId int64) { websocketStat <- websocketStatItem{ - Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, - Value: 1, + UserId: userId, + GuestId: guestId, + Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Value: 1, } } // 减少合图请求数计数 -func decreaseCombineRequestCount() { +func decreaseCombineRequestCount(userId, guestId int64) { websocketStat <- websocketStatItem{ - Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, - Value: -1, + UserId: userId, + GuestId: guestId, + Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, + Value: -1, } } @@ -77,6 +91,13 @@ func ConsumeWebsocketStatData(ctx context.Context) { case data := <-websocketStat: switch data.Type { case TYPE_CONNECT_COUNT: //ws连接计数 + key := fmt.Sprintf("%d_%d", data.UserId, data.GuestId) + statCount, ok := mapUserWsStat.Load(key) + if ok { //存在就累加 + mapUserWsStat.Store(key, statCount.(int)+data.Value) + } else { //不存在就赋值 + mapUserWsStat.Store(key, statCount.(int)) + } currentWebsocketConnectCount += data.Value case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数 currentRequestCombineApiCount += data.Value diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index 35707fc1..e01e0b13 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -34,8 +34,9 @@ type GetStatReq struct { } type GetStatRsp struct { - WsTotalCount int `json:"ws_total_count"` //ws连接数 - CurRequestCombineCount int `json:"cur_request_combine_count"` + WsTotalCount int `json:"ws_total_count"` //ws连接总数 + CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数 + UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 } type Request struct { diff --git a/server_api/websocket.api b/server_api/websocket.api index 02e8b3c6..fa4056e1 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -55,6 +55,7 @@ type GetStatReq { Password string `form:"password"` } type GetStatRsp { - WsTotalCount int `json:"ws_total_count"` //ws连接数 - CurRequestCombineCount int `json:"cur_request_combine_count"` + WsTotalCount int `json:"ws_total_count"` //ws连接总数 + CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数 + UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计 } \ No newline at end of file