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

47 lines
951 B
Go
Raw Normal View History

2023-09-07 07:54:24 +00:00
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)
}
}
}