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

218 lines
5.3 KiB
Go
Raw Normal View History

2023-10-20 04:31:58 +00:00
package logic
import (
"context"
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 03:22:29 +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_UNITY_HANDLE_COUNT websocketStatType = "TYPE_CUR_UNITY_HANDLE_COUNT" //unity处理数
2023-10-20 04:31:58 +00:00
)
type websocketStatItem struct {
2023-11-01 03:22:29 +00:00
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
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"` //当前合图数
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
CurUnityHandleCount int `json:"cur_unity_handle_count"` //当前unity处理数
2023-10-31 07:03:15 +00:00
}
// 统计信息
var (
//用户连接统计
mapUserWsStat = sync.Map{}
//添加or减少连接的控制chan
2023-11-01 03:22:29 +00:00
websocketStat = make(chan websocketStatItem, 1000)
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 03:22:29 +00:00
data := websocketStatItem{
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-10-31 06:23:08 +00:00
Value: 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 03:22:29 +00:00
data := websocketStatItem{
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-10-31 06:23:08 +00:00
Value: -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 03:22:29 +00:00
data := websocketStatItem{
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-10-31 06:23:08 +00:00
Value: 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 03:22:29 +00:00
data := websocketStatItem{
UserId: userId,
GuestId: guestId,
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{
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,
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,
2023-10-31 06:23:08 +00:00
Value: -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
}
// 消费数据
func ConsumeWebsocketStatData(ctx context.Context) {
defer func() {
if err := recover(); err != nil {
logx.Error("ConsumeWebsocketStatData panic:", err)
}
}()
go func() {
select {
case <-ctx.Done():
panic("ConsumeWebsocketStatData ctx deadline")
}
}()
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-10-31 09:31:35 +00:00
if !ok {
2023-10-31 07:03:15 +00:00
mapUserWsStat.Store(key, mapUserWsStatItem{
CurWsConnectCount: data.Value,
})
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
}
stat.CurWsConnectCount += data.Value
2023-10-31 10:00:08 +00:00
//没有连接就删除
if stat.CurWsConnectCount <= 0 {
mapUserWsStat.Delete(key)
continue
}
//保存统计
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-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
}
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 {
2023-10-31 09:31:35 +00:00
logx.Error("断言mapUserWsStatItem错误")
2023-11-01 03:22:29 +00:00
continue
}
stat.CurUnityHandleCount += data.Value
if stat.CurUnityHandleCount <= 0 {
stat.CurUnityHandleCount = 0
2023-10-31 07:03:15 +00:00
}
2023-11-01 03:22:29 +00:00
//保存统计
mapUserWsStat.Store(key, stat)
2023-10-20 04:31:58 +00:00
}
}
}
}