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

129 lines
3.0 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-10-20 04:31:58 +00:00
)
type websocketStatType string
const (
TYPE_CONNECT_COUNT websocketStatType = "connect_count" //ws连接数
TYPE_COMBINE_IMAGE_REQUEST_COUNT websocketStatType = "combine_count"
)
type websocketStatItem struct {
2023-10-31 06:23:08 +00:00
UserId int64 `json:"user_id"`
GuestId int64 `json:"guest_id"`
Type websocketStatType `json:"type"` //类型
Value int `json:"value"` //数值
2023-10-20 04:31:58 +00:00
}
2023-10-31 07:03:15 +00:00
type mapUserWsStatItem struct {
CurCombineCount int `json:"cur_combine_count"` //当前合图数
CurWsConnectCount int `json:"cur_ws_connect_count"` //当前连接数
}
// 统计信息
var (
//用户连接统计
mapUserWsStat = sync.Map{}
//添加or减少连接的控制chan
websocketStat = make(chan websocketStatItem, 20)
)
2023-10-20 04:31:58 +00:00
// 累增ws连接数计数
2023-10-31 06:23:08 +00:00
func increaseWebsocketConnectCount(userId, guestId int64) {
2023-10-20 04:31:58 +00:00
websocketStat <- websocketStatItem{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Value: 1,
2023-10-20 04:31:58 +00:00
}
}
// 减少ws连接数计数
2023-10-31 06:23:08 +00:00
func decreaseWebsocketConnectCount(userId, guestId int64) {
2023-10-20 04:31:58 +00:00
websocketStat <- websocketStatItem{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_CONNECT_COUNT,
Value: -1,
2023-10-20 04:31:58 +00:00
}
}
// 累增合图请求数计数
2023-10-31 06:23:08 +00:00
func increaseCombineRequestCount(userId, guestId int64) {
2023-10-20 04:31:58 +00:00
websocketStat <- websocketStatItem{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: 1,
2023-10-20 04:31:58 +00:00
}
}
// 减少合图请求数计数
2023-10-31 06:23:08 +00:00
func decreaseCombineRequestCount(userId, guestId int64) {
2023-10-20 04:31:58 +00:00
websocketStat <- websocketStatItem{
2023-10-31 06:23:08 +00:00
UserId: userId,
GuestId: guestId,
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: -1,
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-10-20 04:31:58 +00:00
switch data.Type {
case TYPE_CONNECT_COUNT: //ws连接计数
2023-10-31 07:03:15 +00:00
statData, ok := mapUserWsStat.Load(key)
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 09:53:49 +00:00
mapUserWsStat.Store(key, stat)
2023-10-20 04:31:58 +00:00
case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数
2023-10-31 07:03:15 +00:00
statData, ok := mapUserWsStat.Load(key)
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-10-31 09:31:35 +00:00
if stat, ok := statData.(mapUserWsStatItem); ok {
stat.CurCombineCount += data.Value
if stat.CurCombineCount < 0 {
stat.CurCombineCount = 0
}
mapUserWsStat.Store(key, stat)
} else {
logx.Error("断言mapUserWsStatItem错误")
2023-10-31 07:03:15 +00:00
}
2023-10-20 04:31:58 +00:00
}
}
}
}