This commit is contained in:
laodaming 2023-10-31 14:23:08 +08:00
parent 12b0be2f6d
commit 7f85a0adeb
6 changed files with 51 additions and 23 deletions

View File

@ -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})) ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, websocket_data.ConnectSuccessMsg{Wid: uniqueId}))
//发送累加统计连接数 //发送累加统计连接数
increaseWebsocketConnectCount() increaseWebsocketConnectCount(userInfo.UserId, userInfo.GuestId)
return ws, nil return ws, nil
} }
@ -336,7 +336,7 @@ func (w *wsConnectItem) close() {
//删除用户级索引 //删除用户级索引
deleteUserConnPoolElement(w.userId, w.guestId, w.uniqueId) 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") logx.Info("###websocket:", w.uniqueId, " uid:", w.userId, " gid:", w.guestId, " is closed")
} }

View File

@ -33,9 +33,15 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
if req.Password != "fusen1314" { if req.Password != "fusen1314" {
return resp.SetStatusWithMessage(basic.CodeOK, "你干嘛,哎哟") 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{ return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{
WsTotalCount: currentWebsocketConnectCount, WsTotalCount: currentWebsocketConnectCount,
CurRequestCombineCount: currentRequestCombineApiCount, CurRequestCombineCount: currentRequestCombineApiCount,
UserWsStat: userStat,
}) })
} }

View File

@ -15,7 +15,6 @@ import (
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm" "gorm.io/gorm"
"strconv" "strconv"
"strings"
"time" "time"
) )
@ -210,9 +209,9 @@ func (w *wsConnectItem) renderImage(renderImageData websocket_data.RenderImageRe
return return
} }
//累增websocket请求合图数 //累增websocket请求合图数
increaseCombineRequestCount() increaseCombineRequestCount(w.userId, w.guestId)
//请求完要释放 //请求完要释放
defer decreaseCombineRequestCount() defer decreaseCombineRequestCount(w.userId, w.guestId)
//获取刀版图 //获取刀版图
combineReq := repositories.LogoCombineReq{ combineReq := repositories.LogoCombineReq{
UserId: renderImageData.RenderData.UserId, UserId: renderImageData.RenderData.UserId,

View File

@ -2,7 +2,9 @@ package logic
import ( import (
"context" "context"
"fmt"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"sync"
) )
// 统计信息 // 统计信息
@ -11,6 +13,8 @@ var (
currentWebsocketConnectCount = 0 currentWebsocketConnectCount = 0
//当前合图进行的请求总数 //当前合图进行的请求总数
currentRequestCombineApiCount = 0 currentRequestCombineApiCount = 0
//用户连接统计
mapUserWsStat = sync.Map{}
//添加or减少连接的控制chan //添加or减少连接的控制chan
websocketStat = make(chan websocketStatItem, 20) websocketStat = make(chan websocketStatItem, 20)
) )
@ -23,39 +27,49 @@ const (
) )
type websocketStatItem struct { type websocketStatItem struct {
Type websocketStatType `json:"type"` //类型 UserId int64 `json:"user_id"`
Value int `json:"value"` //数值 GuestId int64 `json:"guest_id"`
Type websocketStatType `json:"type"` //类型
Value int `json:"value"` //数值
} }
// 累增ws连接数计数 // 累增ws连接数计数
func increaseWebsocketConnectCount() { func increaseWebsocketConnectCount(userId, guestId int64) {
websocketStat <- websocketStatItem{ websocketStat <- websocketStatItem{
Type: TYPE_CONNECT_COUNT, UserId: userId,
Value: 1, GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Value: 1,
} }
} }
// 减少ws连接数计数 // 减少ws连接数计数
func decreaseWebsocketConnectCount() { func decreaseWebsocketConnectCount(userId, guestId int64) {
websocketStat <- websocketStatItem{ websocketStat <- websocketStatItem{
Type: TYPE_CONNECT_COUNT, UserId: userId,
Value: -1, GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Value: -1,
} }
} }
// 累增合图请求数计数 // 累增合图请求数计数
func increaseCombineRequestCount() { func increaseCombineRequestCount(userId, guestId int64) {
websocketStat <- websocketStatItem{ websocketStat <- websocketStatItem{
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, UserId: userId,
Value: 1, GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: 1,
} }
} }
// 减少合图请求数计数 // 减少合图请求数计数
func decreaseCombineRequestCount() { func decreaseCombineRequestCount(userId, guestId int64) {
websocketStat <- websocketStatItem{ websocketStat <- websocketStatItem{
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT, UserId: userId,
Value: -1, GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: -1,
} }
} }
@ -77,6 +91,13 @@ func ConsumeWebsocketStatData(ctx context.Context) {
case data := <-websocketStat: case data := <-websocketStat:
switch data.Type { switch data.Type {
case TYPE_CONNECT_COUNT: //ws连接计数 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 currentWebsocketConnectCount += data.Value
case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数 case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数
currentRequestCombineApiCount += data.Value currentRequestCombineApiCount += data.Value

View File

@ -34,8 +34,9 @@ type GetStatReq struct {
} }
type GetStatRsp struct { type GetStatRsp struct {
WsTotalCount int `json:"ws_total_count"` //ws连接数 WsTotalCount int `json:"ws_total_count"` //ws连接总数
CurRequestCombineCount int `json:"cur_request_combine_count"` CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数
UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计
} }
type Request struct { type Request struct {

View File

@ -55,6 +55,7 @@ type GetStatReq {
Password string `form:"password"` Password string `form:"password"`
} }
type GetStatRsp { type GetStatRsp {
WsTotalCount int `json:"ws_total_count"` //ws连接数 WsTotalCount int `json:"ws_total_count"` //ws连接总数
CurRequestCombineCount int `json:"cur_request_combine_count"` CurRequestCombineCount int `json:"cur_request_combine_count"` //合图任务数
UserWsStat interface{} `json:"user_ws_stat"` //用户连接统计
} }