fusenapi/server/websocket/internal/logic/ws_statistics.go

285 lines
7.2 KiB
Go
Raw Normal View History

2023-10-20 04:31:58 +00:00
package logic
import (
2023-10-31 06:23:08 +00:00
"fmt"
2023-10-20 04:31:58 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-10-31 06:23:08 +00:00
"sync"
2023-11-01 03:22:29 +00:00
"time"
2023-10-20 04:31:58 +00:00
)
type websocketStatType string
const (
2023-11-01 06:35:37 +00:00
TYPE_CUR_CONNECT_COUNT websocketStatType = "TYPE_CUR_CONNECT_COUNT" //ws连接数
TYPE_CUR_COMBINE_IMAGE_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_COUNT" //合图数
TYPE_CUR_COMBINE_IMAGE_ERR_COUNT websocketStatType = "TYPE_CUR_COMBINE_IMAGE_ERR_COUNT" //合图失败数
TYPE_CUR_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数
2023-11-01 07:43:25 +00:00
TYPE_UNITY_ERR_COUNT websocketStatType = "TYPE_UNITY_ERR_COUNT" //unity错误处理数
2023-10-20 04:31:58 +00:00
)
2023-11-01 04:18:49 +00:00
type websocketStatData struct {
UserId int64 `json:"user_id"` //用户id
GuestId int64 `json:"guest_id"` //游客id
Type websocketStatType `json:"type"` //类型
Num int `json:"num"` //数值
2023-10-20 04:31:58 +00:00
}
2023-10-31 07:03:15 +00:00
type mapUserWsStatItem struct {
2023-11-01 03:22:29 +00:00
CurCombineCount int `json:"cur_combine_count"` //当前合图数
2023-11-01 06:35:37 +00:00
CombineErrCount int `json:"combine_err_count"` //合图失败数
2023-11-01 03:22:29 +00:00
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数
2023-11-01 07:43:25 +00:00
UnityErrCount int `json:"unity_err_count"` //unity处理错误数
2023-10-31 07:03:15 +00:00
}
// 统计信息
var (
2023-11-02 04:15:52 +00:00
// 服务启动时间
serverStartTime = time.Now().UTC().UnixMilli()
2023-10-31 07:03:15 +00:00
//用户连接统计
mapUserWsStat = sync.Map{}
2023-11-01 07:09:53 +00:00
//消息控制通道的数据结构
2023-11-01 04:18:49 +00:00
websocketStat = make(chan websocketStatData, 1000)
2023-11-01 07:09:53 +00:00
//ws总的连接数
curWsTotalCount int
//合图失败总数
combineErrTotalCount int
//unity正在渲染总数
curUnityHandleTotalCount int
2023-11-01 07:43:25 +00:00
//unity错误统计数
unityErrTotalCount int
2023-11-01 07:09:53 +00:00
//算法正在合图总数
curCombineTotalCount int
2023-10-31 07:03:15 +00:00
)
2023-10-20 04:31:58 +00:00
// 累增ws连接数计数
2023-10-31 06:23:08 +00:00
func increaseWebsocketConnectCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
2023-11-01 03:22:29 +00:00
Type: TYPE_CUR_CONNECT_COUNT,
2023-11-01 04:18:49 +00:00
Num: 1,
2023-10-20 04:31:58 +00:00
}
2023-11-01 03:22:29 +00:00
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseWebsocketConnectCount 输入管道超时,丢弃消息")
return
}
2023-10-20 04:31:58 +00:00
}
// 减少ws连接数计数
2023-10-31 06:23:08 +00:00
func decreaseWebsocketConnectCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
2023-11-01 03:22:29 +00:00
Type: TYPE_CUR_CONNECT_COUNT,
2023-11-01 04:18:49 +00:00
Num: -1,
2023-10-20 04:31:58 +00:00
}
2023-11-01 03:22:29 +00:00
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseWebsocketConnectCount 输入管道超时,丢弃消息")
return
}
2023-10-20 04:31:58 +00:00
}
// 累增合图请求数计数
2023-10-31 06:23:08 +00:00
func increaseCombineRequestCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
2023-11-01 03:22:29 +00:00
Type: TYPE_CUR_COMBINE_IMAGE_COUNT,
2023-11-01 04:18:49 +00:00
Num: 1,
2023-10-20 04:31:58 +00:00
}
2023-11-01 03:22:29 +00:00
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestCount 输入管道超时,丢弃消息")
return
}
2023-10-20 04:31:58 +00:00
}
// 减少合图请求数计数
2023-10-31 06:23:08 +00:00
func decreaseCombineRequestCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-11-01 03:22:29 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_COMBINE_IMAGE_COUNT,
2023-11-01 04:18:49 +00:00
Num: -1,
2023-11-01 03:22:29 +00:00
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseCombineRequestCount 输入管道超时,丢弃消息")
return
}
}
2023-11-01 06:35:37 +00:00
// 累增合图失败数计数
func increaseCombineRequestErrorCount(userId, guestId int64) {
data := websocketStatData{
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_COMBINE_IMAGE_ERR_COUNT,
Num: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseCombineRequestErrorCount 输入管道超时,丢弃消息")
return
}
}
2023-11-01 03:22:29 +00:00
// 累增unity请求数计数
func increaseUnityRequestCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
2023-11-01 03:22:29 +00:00
Type: TYPE_CUR_UNITY_HANDLE_COUNT,
2023-11-01 04:18:49 +00:00
Num: 1,
2023-11-01 03:22:29 +00:00
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
2023-11-01 07:43:25 +00:00
logx.Error("increaseUnityRequestCount 输入管道超时,丢弃消息")
2023-11-01 03:22:29 +00:00
return
}
}
// 减少unity请求数计数
func decreaseUnityRequestCount(userId, guestId int64) {
2023-11-01 04:18:49 +00:00
data := websocketStatData{
2023-11-01 03:22:29 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_CUR_UNITY_HANDLE_COUNT,
2023-11-01 04:18:49 +00:00
Num: -1,
2023-10-20 04:31:58 +00:00
}
2023-11-01 03:22:29 +00:00
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("decreaseUnityRequestCount 输入管道超时,丢弃消息")
return
}
2023-10-20 04:31:58 +00:00
}
2023-11-01 07:43:25 +00:00
// 累曾unity错误统计
func increaseUnityErrorCount(userId, guestId int64) {
data := websocketStatData{
UserId: userId,
GuestId: guestId,
Type: TYPE_UNITY_ERR_COUNT,
Num: 1,
}
select {
case websocketStat <- data:
return
case <-time.After(time.Millisecond * 200):
logx.Error("increaseUnityErrorCount 输入管道超时,丢弃消息")
return
}
}
2023-10-20 04:31:58 +00:00
// 消费数据
2023-11-02 07:00:37 +00:00
func ConsumeWebsocketStatData() {
2023-10-20 04:31:58 +00:00
defer func() {
if err := recover(); err != nil {
logx.Error("ConsumeWebsocketStatData panic:", err)
}
}()
for {
select {
case data := <-websocketStat:
2023-10-31 07:03:15 +00:00
key := fmt.Sprintf("%d_%d", data.UserId, data.GuestId)
2023-11-01 03:22:29 +00:00
statData, ok := mapUserWsStat.Load(key)
2023-10-20 04:31:58 +00:00
switch data.Type {
2023-11-01 03:22:29 +00:00
case TYPE_CUR_CONNECT_COUNT: //ws连接计数
2023-11-02 07:21:12 +00:00
curWsTotalCount += data.Num
2023-10-31 09:31:35 +00:00
if !ok {
2023-10-31 07:03:15 +00:00
mapUserWsStat.Store(key, mapUserWsStatItem{
2023-11-01 04:18:49 +00:00
CurWsConnectCount: data.Num,
2023-10-31 07:03:15 +00:00
})
2023-10-31 09:53:49 +00:00
continue
2023-10-31 06:23:08 +00:00
}
2023-10-31 09:31:35 +00:00
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
2023-11-01 04:18:49 +00:00
stat.CurWsConnectCount += data.Num
2023-11-02 07:09:47 +00:00
//没有连接就删除(先不删除)
/*if stat.CurWsConnectCount <= 0 {
2023-10-31 10:00:08 +00:00
mapUserWsStat.Delete(key)
continue
2023-11-02 07:09:47 +00:00
}*/
2023-10-31 10:00:08 +00:00
//保存统计
2023-10-31 09:53:49 +00:00
mapUserWsStat.Store(key, stat)
2023-11-01 03:22:29 +00:00
case TYPE_CUR_COMBINE_IMAGE_COUNT: //请求算法合图计数
2023-11-13 01:57:52 +00:00
curCombineTotalCount += data.Num
2023-10-31 09:46:15 +00:00
//不存在
2023-10-31 09:31:35 +00:00
if !ok {
2023-10-31 09:53:49 +00:00
continue
2023-10-31 09:31:35 +00:00
}
2023-10-31 09:46:15 +00:00
//存在
2023-11-01 03:22:29 +00:00
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
2023-11-01 04:18:49 +00:00
stat.CurCombineCount += data.Num
2023-11-01 03:22:29 +00:00
//保存统计
mapUserWsStat.Store(key, stat)
case TYPE_CUR_UNITY_HANDLE_COUNT: //unity处理计数
2023-11-13 01:57:52 +00:00
curUnityHandleTotalCount += data.Num
2023-11-01 03:22:29 +00:00
if !ok {
continue
}
//存在
stat, ok := statData.(mapUserWsStatItem)
if !ok {
2023-10-31 09:31:35 +00:00
logx.Error("断言mapUserWsStatItem错误")
2023-11-01 03:22:29 +00:00
continue
}
2023-11-01 04:18:49 +00:00
stat.CurUnityHandleCount += data.Num
2023-11-01 03:22:29 +00:00
//保存统计
mapUserWsStat.Store(key, stat)
2023-11-01 06:35:37 +00:00
case TYPE_CUR_COMBINE_IMAGE_ERR_COUNT: //合图失败计数
2023-11-13 01:57:52 +00:00
combineErrTotalCount += data.Num
2023-11-01 06:35:37 +00:00
if !ok {
continue
}
//存在
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
stat.CombineErrCount += data.Num
//保存统计
mapUserWsStat.Store(key, stat)
2023-11-01 07:43:25 +00:00
case TYPE_UNITY_ERR_COUNT: //unity错误信息
2023-11-13 01:57:52 +00:00
unityErrTotalCount += data.Num
2023-11-01 07:43:25 +00:00
if !ok {
continue
}
//存在
stat, ok := statData.(mapUserWsStatItem)
if !ok {
logx.Error("断言mapUserWsStatItem错误")
continue
}
stat.UnityErrCount += data.Num
//保存统计
mapUserWsStat.Store(key, stat)
2023-10-20 04:31:58 +00:00
}
}
}
}