测试内存泄露问题

This commit is contained in:
junleea 2025-03-10 15:13:30 +08:00
parent 539ea9215b
commit fd4e0c0495
1 changed files with 49 additions and 0 deletions

View File

@ -185,6 +185,55 @@ func GetRealTimeImage(c *gin.Context) {
subscribeAndHandleMessagesV5(ws, deviceIdInt) subscribeAndHandleMessagesV5(ws, deviceIdInt)
} }
func subscribeAndHandleMessagesV6(ws *websocket.Conn, deviceId int) {
// 生成唯一连接 uuid
con_id := uuid.New().String()
online_conn_key := "device_" + strconv.Itoa(deviceId) + "_online_conn_ids"
// 加入设备在线连接集合
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
//图片计数器
count := 0
//定时器,发送计数器
tCount := 0
var buf []byte
//计算帧率
for {
//从service获取当前帧
c := service.DeviceFrameCount[deviceId]
if c != count {
count = c
buf = service.Device1CurrentFrame.ToBytes()
err2 := ws.WriteMessage(websocket.BinaryMessage, buf)
if err2 != nil {
log.Printf("send message to client err:%v", err2)
worker.SetRedisSetRemove(online_conn_key, con_id)
break
}
c = count
} else {
//每秒发送一次心跳检测
if tCount%10 == 0 {
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
if err != nil {
log.Printf("Connection check failed:%v", err)
worker.SetRedisSetRemove(online_conn_key, con_id)
break
} else {
log.Printf("Connection check success")
}
}
}
time.Sleep(100 * time.Millisecond)
tCount++
}
// 查看是否还有其他连接,没有则设置 is_play 为 0
if worker.IsContainKey(online_conn_key) == false {
worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5)
log.Printf("device_id: %d has set is_play to 0", deviceId)
}
}
func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) { func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) {
// 生成唯一连接 uuid // 生成唯一连接 uuid
con_id := uuid.New().String() con_id := uuid.New().String()