From 539ea9215b0702bd3d9a5f8fa5d6a3fc8bc73745 Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Mon, 10 Mar 2025 15:00:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E9=9C=B2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 9 ++------- service/tool.go | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index f861093..c05f9c7 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -195,13 +195,11 @@ func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) { count := 0 //定时器,发送计数器 tCount := 0 - var buf []byte + buf := make([]byte, 1024) //计算帧率 for { - //将buf置空 - buf = nil //从service获取当前帧 - c := service.GetDeviceCurrentFrameV5(deviceId, count, &buf) + c := service.GetDeviceCurrentFrameV5(deviceId, count, buf) if c != count { count = c err2 := ws.WriteMessage(websocket.BinaryMessage, buf) @@ -233,9 +231,6 @@ func subscribeAndHandleMessagesV5(ws *websocket.Conn, deviceId int) { worker.SetRedisWithExpire(strconv.Itoa(deviceId)+"_is_play", "1", time.Minute*5) log.Printf("device_id: %d has set is_play to 0", deviceId) } - - //垃圾回收 - runtime.GC() } func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) { diff --git a/service/tool.go b/service/tool.go index d7d433a..c4ce062 100644 --- a/service/tool.go +++ b/service/tool.go @@ -92,7 +92,7 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error { DeviceFrameCount[deviceId] = frameCount return nil } -func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf *[]byte) (cnt int) { +func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf []byte) (cnt int) { defer func() { if err := recover(); err != nil { log.Printf("设备:%d 错误: %v\n", deviceId, err) @@ -143,7 +143,7 @@ func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf *[]byte) (cnt int) } // 避免直接返回大数组,可考虑复制必要的数据 if len(tempBuf) > 0 { - copy(*buf, tempBuf) + copy(buf, tempBuf) // 释放临时变量 tempBuf = nil }