diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index 81cecdd9..6a3b206f 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -45,14 +45,19 @@ var ( // 每个连接的连接属性 type wsConnectItem struct { - conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan - isClose bool //是否已经关闭 - flag string - inChan chan []byte //接受消息缓冲通道 - outChan chan []byte //发送回客户端的消息 - mutex sync.Mutex - renderImage map[string]struct{} //需要渲染的图片 + conn *websocket.Conn //websocket的连接 + closeChan chan struct{} //关闭chan + isClose bool //是否已经关闭 + flag string + inChan chan []byte //接受消息缓冲通道 + outChan chan []byte //发送回客户端的消息 + mutex sync.Mutex + renderImage map[string]struct{} //需要渲染的图片 + renderImageControlChan chan renderImageControlChanItem +} +type renderImageControlChanItem struct { + Option int // 0删除 1添加 + Key string //map的key } func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) { @@ -105,13 +110,14 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp //生成连接唯一标识 flag := uuid.New().String() + time.Now().Format("20060102150405") ws := wsConnectItem{ - conn: conn, - flag: flag, - closeChan: make(chan struct{}, 1), - inChan: make(chan []byte, 100), - outChan: make(chan []byte, 100), - renderImage: make(map[string]struct{}), - isClose: false, + conn: conn, + flag: flag, + closeChan: make(chan struct{}, 1), + inChan: make(chan []byte, 100), + outChan: make(chan []byte, 100), + renderImage: make(map[string]struct{}), + renderImageControlChan: make(chan renderImageControlChanItem, 100), + isClose: false, } //保存连接 mapConnPool.Store(flag, ws) @@ -127,6 +133,8 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp go ws.writeLoop() //推消息到云渲染 go ws.sendLoop() + //操作连接中渲染任务的增加/删除 + go ws.operationRenderTask() //心跳 ws.heartbeat() } @@ -165,12 +173,28 @@ func (w *wsConnectItem) close() { if !w.isClose { w.isClose = true close(w.closeChan) - close(w.outChan) - close(w.inChan) } logx.Info("websocket:", w.flag, " is closed") } +// 操作连接中渲染任务的增加/删除 +func (w *wsConnectItem) operationRenderTask() { + for { + select { + case <-w.closeChan: + return + case data := <-w.renderImageControlChan: + switch data.Option { + case 0: //删除 + delete(w.renderImage, data.Key) + case 1: //新增 + w.renderImage[data.Key] = struct{}{} + default: + + } + } + } +} func (w *wsConnectItem) writeLoop() { for { select { @@ -244,15 +268,6 @@ func (w *wsConnectItem) sendToOutChan(data [][]byte) { } } -// 删除对应需要渲染的map值 -func (w *wsConnectItem) deleteRenderMapVal(keys []string) { - w.mutex.Lock() - defer w.mutex.Unlock() - for _, v := range keys { - delete(w.renderImage, v) - } -} - // 获取需要渲染图片的map key func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 22fff393..aa504999 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -64,7 +64,6 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi return false } dataList := make([][]byte, 0, len(req.NotifyList)) - deleteRenderKey := make([]string, 0, len(req.NotifyList)) //遍历数据 for _, notifyItem := range req.NotifyList { renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) @@ -81,10 +80,12 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi } b, _ := json.Marshal(responseData) dataList = append(dataList, b) - deleteRenderKey = append(deleteRenderKey, renderKey) + //删除对应的需要渲染的图片map + ws.renderImageControlChan <- renderImageControlChanItem{ + Option: 0, + Key: renderKey, + } } - //删除对应的需要渲染的图片map - ws.deleteRenderMapVal(deleteRenderKey) //发送数据 ws.sendToOutChan(dataList) return true diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 2428cd17..46c742ba 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -14,11 +14,12 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { return } logx.Info("收到请求云渲染图片数据:", renderImageData) - w.mutex.Lock() - defer w.mutex.Unlock() //把需要渲染的图片加进去 for _, v := range renderImageData { key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImage[key] = struct{}{} + w.renderImageControlChan <- renderImageControlChanItem{ + Option: 1, + Key: key, + } } }