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}))
//发送累加统计连接数
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")
}

View File

@ -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,
})
}

View File

@ -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,

View File

@ -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

View File

@ -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 {

View File

@ -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"` //用户连接统计
}