This commit is contained in:
laodaming 2023-10-20 12:31:58 +08:00
parent 9b1db4740d
commit 94a503f95d
3 changed files with 87 additions and 47 deletions

View File

@ -1,46 +0,0 @@
package logic
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
)
var (
//当前ws连接数
currentWebsocketConnectCount = 0
//添加or减少连接的控制chan
websocketConnectCountCtlChan = make(chan int, 20)
)
// 累增计数
func increaseWebsocketConnectCount() {
websocketConnectCountCtlChan <- 1
}
// 减少计数
func decreaseWebsocketConnectCount() {
websocketConnectCountCtlChan <- -1
}
// 消费数据
func ConsumeWebsocketConnectCountCtlChanData(ctx context.Context) {
defer func() {
if err := recover(); err != nil {
logx.Error("ConsumeWebsocketConnectCountCtlChanData panic:", err)
}
}()
go func() {
select {
case <-ctx.Done():
panic("ConsumeWebsocketConnectCountCtlChanData ctx deadline")
}
}()
var num int
for {
select {
case num = <-websocketConnectCountCtlChan:
currentWebsocketConnectCount += num
//logx.Info("当前websocket连接总数:", currentWebsocketConnectCount)
}
}
}

View File

@ -0,0 +1,86 @@
package logic
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
)
// 统计信息
var (
//当前ws连接数
currentWebsocketConnectCount = 0
//当前合图进行的请求总数
currentRequestCombineApiCount = 0
//添加or减少连接的控制chan
websocketStat = make(chan websocketStatItem, 20)
)
type websocketStatType string
const (
TYPE_CONNECT_COUNT websocketStatType = "connect_count" //ws连接数
TYPE_COMBINE_IMAGE_REQUEST_COUNT websocketStatType = "combine_count"
)
type websocketStatItem struct {
Type websocketStatType `json:"type"` //类型
Value int `json:"value"` //数值
}
// 累增ws连接数计数
func increaseWebsocketConnectCount() {
websocketStat <- websocketStatItem{
Type: TYPE_CONNECT_COUNT,
Value: 1,
}
}
// 减少ws连接数计数
func decreaseWebsocketConnectCount() {
websocketStat <- websocketStatItem{
Type: TYPE_CONNECT_COUNT,
Value: -1,
}
}
// 累增合图请求数计数
func increaseCombineRequestCount() {
websocketStat <- websocketStatItem{
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: 1,
}
}
// 减少合图请求数计数
func decreaseCombineRequestCount() {
websocketStat <- websocketStatItem{
Type: TYPE_COMBINE_IMAGE_REQUEST_COUNT,
Value: -1,
}
}
// 消费数据
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:
switch data.Type {
case TYPE_CONNECT_COUNT: //ws连接计数
currentWebsocketConnectCount += data.Value
case TYPE_COMBINE_IMAGE_REQUEST_COUNT: //请求算法合图计数
currentRequestCombineApiCount += data.Value
}
}
}
}

View File

@ -38,7 +38,7 @@ func main() {
//消费用户索引创建/删除/发送消息中的任务数据
go logic.ConsumeUserConnPoolCtlChanData(ctx1)
//消费连接统计信息
go logic.ConsumeWebsocketConnectCountCtlChanData(ctx1)
go logic.ConsumeWebsocketStatData(ctx1)
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
server.Start()
}