This commit is contained in:
laodaming 2023-11-01 15:09:53 +08:00
parent 12795e2115
commit 1948a60c29
2 changed files with 20 additions and 25 deletions

View File

@ -39,24 +39,12 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
GetStatMutex.Lock()
defer GetStatMutex.Unlock()
userStat := make(map[string]interface{})
currentWebsocketConnectCount := 0
currentCombineApiCount := 0
currentUnityHandleCount := 0
combineErrorCount := 0
req.UserKeys = strings.Trim(req.UserKeys, " ")
if req.UserKeys != "" { //指定用户群体
reqUserList := strings.Split(req.UserKeys, ",")
for _, key := range reqUserList {
value, ok := mapUserWsStat.Load(key)
if ok { //存在就累加
if stat, ok := value.(mapUserWsStatItem); ok {
currentWebsocketConnectCount += stat.CurWsConnectCount
currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount
combineErrorCount += stat.CombineErrCount
} else {
logx.Error("断言mapUserWsStatItem错误")
}
userStat[key] = value
} else {
userStat[key] = mapUserWsStatItem{}
@ -64,23 +52,15 @@ func (l *GetStatLogic) GetStat(req *types.GetStatReq, userinfo *auth.UserInfo) (
}
} else { //不指定用户
mapUserWsStat.Range(func(key, value any) bool {
if stat, ok := value.(mapUserWsStatItem); ok {
currentWebsocketConnectCount += stat.CurWsConnectCount
currentCombineApiCount += stat.CurCombineCount
currentUnityHandleCount += stat.CurUnityHandleCount
combineErrorCount += stat.CombineErrCount
} else {
logx.Error("断言mapUserWsStatItem错误")
}
userStat[key.(string)] = value
return true
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetStatRsp{
WsTotalCount: currentWebsocketConnectCount,
CurCombineCount: currentCombineApiCount,
CurUnityHandleCount: currentUnityHandleCount,
CombineErrorCount: combineErrorCount,
WsTotalCount: curWsTotalCount,
CurCombineCount: curCombineTotalCount,
CurUnityHandleCount: curUnityHandleTotalCount,
CombineErrorCount: combineErrTotalCount,
UserWsStat: userStat,
})
}

View File

@ -34,8 +34,16 @@ type mapUserWsStatItem struct {
var (
//用户连接统计
mapUserWsStat = sync.Map{}
//添加or减少连接的控制chan
//消息控制通道的数据结构
websocketStat = make(chan websocketStatData, 1000)
//ws总的连接数
curWsTotalCount int
//合图失败总数
combineErrTotalCount int
//unity正在渲染总数
curUnityHandleTotalCount int
//算法正在合图总数
curCombineTotalCount int
)
// 累增ws连接数计数
@ -48,6 +56,7 @@ func increaseWebsocketConnectCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curWsTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseWebsocketConnectCount 输入管道超时,丢弃消息")
@ -65,6 +74,7 @@ func decreaseWebsocketConnectCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curWsTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseWebsocketConnectCount 输入管道超时,丢弃消息")
@ -82,6 +92,7 @@ func increaseCombineRequestCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curCombineTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestCount 输入管道超时,丢弃消息")
@ -99,6 +110,7 @@ func decreaseCombineRequestCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curCombineTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息")
@ -116,6 +128,7 @@ func increaseCombineRequestErrorCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
combineErrTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestErrorCount 输入管道超时,丢弃消息")
@ -133,6 +146,7 @@ func increaseUnityRequestCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curUnityHandleTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息")
@ -150,6 +164,7 @@ func decreaseUnityRequestCount(userId, guestId int64) {
}
select {
case websocketStat <- data:
curUnityHandleTotalCount += data.Num
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseUnityRequestCount 输入管道超时,丢弃消息")