diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index e14983d4..5fe8d1e4 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -1,14 +1,10 @@ package logic import ( - "fusenapi/constants" + "context" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/file" - "fusenapi/utils/websocket_data" - "time" - - "context" "fusenapi/server/websocket/internal/svc" "fusenapi/server/websocket/internal/types" @@ -77,34 +73,13 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq, userinfo *a logx.Error("渲染回调断言websocket连接失败") return true } - //关闭标识 - if ws.isClose { - return true - } - //查询有无该渲染任务 - renderId, ok := ws.renderProperty.renderImageTask[req.TaskId] - if !ok { - return true - } - b := ws.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ - RenderId: renderId, - Image: uploadRes.ResourceUrl, + //发送处理并删除任务 + ws.deleteRenderTask(renderImageControlChanItem{ + Option: 0, //0删除 1添加 + TaskId: req.TaskId, + RenderNotifyImageUrl: uploadRes.ResourceUrl, }) - deleteTask := renderImageControlChanItem{ - Option: 0, //0删除 1添加 - TaskId: req.TaskId, - RenderId: renderId, - } - select { - case <-ws.closeChan: //关闭了 - return true - case ws.renderProperty.renderImageTaskCtlChan <- deleteTask: //删除对应的需要渲染的图片map - //发送数据到out chan - ws.sendToOutChan(b) - return true - case <-time.After(time.Second * 3): //超时丢弃 - return true - } + return true }) logx.Info("渲染回调成功######################") return resp.SetStatusWithMessage(basic.CodeOK, "success") diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index db3816aa..d293eee9 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -24,9 +24,10 @@ type renderProperty struct { // 渲染任务新增移除的控制通道的数据 type renderImageControlChanItem struct { - Option int // 0删除 1添加 - TaskId string //map的key - RenderId string // map的val + Option int // 0删除 1添加 + TaskId string //map的key + RenderId string // map的val(增加任务时候传) + RenderNotifyImageUrl string //渲染回调数据(删除任务时候传) } // 发送到渲染缓冲池 @@ -137,11 +138,11 @@ func (w *wsConnectItem) consumeRenderCache(data []byte) { } //########################################### //把需要渲染的图片任务加进去 - w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ + w.createRenderTask(renderImageControlChanItem{ Option: 1, //0删除 1添加 TaskId: taskId, RenderId: renderImageData.RenderId, - } + }) //组装数据 if err = w.assembleRenderData(taskId, renderImageData); err != nil { logx.Error("组装数据失败:", err) @@ -292,7 +293,35 @@ func (w *wsConnectItem) assembleRenderData(taskId string, info websocket_data.Re return nil } -// 操作连接中渲染任务的增加/删除 +// 增加渲染任务 +func (w *wsConnectItem) createRenderTask(data renderImageControlChanItem) { + //强制设为增加 + data.Option = 1 + select { + case <-w.closeChan: //关闭 + return + case w.renderProperty.renderImageTaskCtlChan <- data: + return + case <-time.After(time.Second * 3): + return + } +} + +// 渲染回调处理并删除渲染任务 +func (w *wsConnectItem) deleteRenderTask(data renderImageControlChanItem) { + //强制设为删除 + data.Option = 0 + select { + case <-w.closeChan: //关闭 + return + case w.renderProperty.renderImageTaskCtlChan <- data: + return + case <-time.After(time.Second * 3): + return + } +} + +// 操作连接中渲染任务的增加/删除(任务map不能读写并发,所以放在chan里面串行执行) func (w *wsConnectItem) operationRenderTask() { for { select { @@ -300,7 +329,14 @@ func (w *wsConnectItem) operationRenderTask() { return case data := <-w.renderProperty.renderImageTaskCtlChan: switch data.Option { - case 0: //删除任务 + case 0: //渲染结果回调,删除任务 + //存在任务,则发送渲染结果给前端 + if renderId, ok := w.renderProperty.renderImageTask[data.TaskId]; ok { + w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_RENDER_IMAGE, websocket_data.RenderImageRspMsg{ + RenderId: renderId, + Image: data.RenderNotifyImageUrl, + })) + } delete(w.renderProperty.renderImageTask, data.TaskId) case 1: //新增任务 w.renderProperty.renderImageTask[data.TaskId] = data.RenderId