This commit is contained in:
laodaming
2023-11-01 11:22:29 +08:00
parent 042b23c647
commit c387f5d17b
7 changed files with 138 additions and 44 deletions

View File

@@ -39,7 +39,8 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
defer GetStatMutex.Unlock()
userStat := make(map[string]interface{})
currentWebsocketConnectCount := 0
currentRequestCombineApiCount := 0
currentCombineApiCount := 0
currentUnityHandleCount := 0
mapUserWsStat.Range(func(key, value any) bool {
statData, ok := mapUserWsStat.Load(key)
if ok { //存在就累加
@@ -49,7 +50,8 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
return true
}
currentWebsocketConnectCount += stat.CurWsConnectCount
currentRequestCombineApiCount += stat.CurCombineCount
currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount
} else {
logx.Error("断言mapUserWsStatItem错误")
}
@@ -58,9 +60,10 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
return true
})
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{
WsTotalCount: currentWebsocketConnectCount,
CurRequestCombineCount: currentRequestCombineApiCount,
UserWsStat: userStat,
WsTotalCount: currentWebsocketConnectCount,
CurCombineCount: currentCombineApiCount,
CurUnityHandleCount: currentUnityHandleCount,
UserWsStat: userStat,
})
}

View File

@@ -49,6 +49,8 @@ func (w *wsConnectItem) sendAssembleRenderDataStepResponseMessage(requestId stri
// 发送组装数据到unity完毕阶段通知消息
func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(requestId string) {
//统计unity处理数
increaseUnityRequestCount(w.userId, w.guestId)
if w.debug == nil {
return
}
@@ -57,6 +59,8 @@ func (w *wsConnectItem) sendRenderDataToUnityStepResponseMessage(requestId strin
// 发送渲染最终结果数据到前端
func (w *wsConnectItem) sendRenderResultData(data websocket_data.RenderImageRspMsg) {
//统计unity处理数
decreaseUnityRequestCount(w.userId, w.guestId)
//没开启debug
if w.debug == nil {
data.RenderProcessTime = websocket_data.RenderProcessTime{}

View File

@@ -5,24 +5,28 @@ import (
"fmt"
"github.com/zeromicro/go-zero/core/logx"
"sync"
"time"
)
type websocketStatType string
const (
TYPE_CONNECT_COUNT websocketStatType = "connect_count" //ws连接数
TYPE_COMBINE_IMAGE_REQUEST_COUNT websocketStatType = "combine_count"
TYPE_CUR_CONNECT_COUNT websocketStatType = "TYPE_CUR_CONNECT_COUNT" //ws连接数
TYPE_CUR_COMBINE_IMAGE_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_COUNT" //合图数
TYPE_CUR_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数
)
type websocketStatItem struct {
UserId int64 `json:"user_id"`
GuestId int64 `json:"guest_id"`
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"` //数值
UnityRenderLogId int64 `json:"unity_render_log_id"` //unity渲染的日志id
}
type mapUserWsStatItem struct {
CurCombineCount int `json:"cur_combine_count"` //当前合图数
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
CurCombineCount int `json:"cur_combine_count"` //当前合图数
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数
}
// 统计信息
@@ -30,47 +34,109 @@ var (
//用户连接统计
mapUserWsStat = sync.Map{}
//添加or减少连接的控制chan
websocketStat = make(chan websocketStatItem, 20)
websocketStat = make(chan websocketStatItem, 1000)
)
// 累增ws连接数计数
func increaseWebsocketConnectCount(userId, guestId int64) {
websocketStat <- websocketStatItem{
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Type: TYPE_CUR_CONNECT_COUNT,
Value: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseWebsocketConnectCount 输入管道超时,丢弃消息")
return
}
}
// 减少ws连接数计数
func decreaseWebsocketConnectCount(userId, guestId int64) {
websocketStat <- websocketStatItem{
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Type: TYPE_CUR_CONNECT_COUNT,
Value: -1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseWebsocketConnectCount 输入管道超时,丢弃消息")
return
}
}
// 累增合图请求数计数
func increaseCombineRequestCount(userId, guestId int64) {
websocketStat <- websocketStatItem{
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Type: TYPE_CUR_COMBINE_IMAGE_COUNT,
Value: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestCount 输入管道超时,丢弃消息")
return
}
}
// 减少合图请求数计数
func decreaseCombineRequestCount(userId, guestId int64) {
websocketStat <- websocketStatItem{
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Type: TYPE_CUR_COMBINE_IMAGE_COUNT,
Value: -1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息")
return
}
}
// 累增unity请求数计数
func increaseUnityRequestCount(userId, guestId int64) {
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_UNITY_HANDLE_COUNT,
Value: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息")
return
}
}
// 减少unity请求数计数
func decreaseUnityRequestCount(userId, guestId int64) {
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_UNITY_HANDLE_COUNT,
Value: -1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseUnityRequestCount 输入管道超时,丢弃消息")
return
}
}
// 消费数据
@@ -90,9 +156,9 @@ func ConsumeWebsocketStatData(ctx context.Context) {
select {
case data := <-websocketStat:
key := fmt.Sprintf("%d_%d", data.UserId, data.GuestId)
statData, ok := mapUserWsStat.Load(key)
switch data.Type {
case TYPE_CONNECT_COUNT: //ws连接计数
statData, ok := mapUserWsStat.Load(key)
case TYPE_CUR_CONNECT_COUNT: //ws连接计数
if !ok {
mapUserWsStat.Store(key, mapUserWsStatItem{
CurWsConnectCount: data.Value,
@@ -112,22 +178,39 @@ func ConsumeWebsocketStatData(ctx context.Context) {
}
//保存统计
mapUserWsStat.Store(key, stat)
case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数
statData, ok := mapUserWsStat.Load(key)
case TYPE_CUR_COMBINE_IMAGE_COUNT: //请求算法合图计数
//不存在
if !ok {
continue
}
//存在
if stat, ok := statData.(mapUserWsStatItem); ok {
stat.CurCombineCount += data.Value
if stat.CurCombineCount < 0 {
stat.CurCombineCount = 0
}
mapUserWsStat.Store(key, stat)
} else {
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
stat.CurCombineCount += data.Value
if stat.CurCombineCount < 0 {
stat.CurCombineCount = 0
}
//保存统计
mapUserWsStat.Store(key, stat)
case TYPE_CUR_UNITY_HANDLE_COUNT: //unity处理计数
if !ok {
continue
}
//存在
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
stat.CurUnityHandleCount += data.Value
if stat.CurUnityHandleCount <= 0 {
stat.CurUnityHandleCount = 0
}
//保存统计
mapUserWsStat.Store(key, stat)
}
}
}