From d5ff15849d8467c0f4915488f8126713030e4b3f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 26 Jul 2023 17:06:53 +0800 Subject: [PATCH] fix --- .../internal/logic/datatransferlogic.go | 39 +++++++++---------- .../internal/logic/rendernotifylogic.go | 4 +- .../internal/logic/ws_render_image_logic.go | 20 ++++++++-- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go index c7f05b3a..78ff3004 100644 --- a/server/websocket/internal/logic/datatransferlogic.go +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -57,21 +57,16 @@ var ( mapConnPool = sync.Map{} ) -// 每个连接的连接属性 +// 每个连接的连接基本属性 type wsConnectItem struct { - conn *websocket.Conn //websocket的连接 - closeChan chan struct{} //关闭chan - isClose bool //是否已经关闭 - flag string - inChan chan []byte //接受消息缓冲通道 - outChan chan []byte //发送回客户端的消息 - mutex sync.Mutex - renderImageTask map[string]struct{} //需要渲染的图片 - renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 -} -type renderImageControlChanItem struct { - Option int // 0删除 1添加 - Key string //map的key + conn *websocket.Conn //websocket的连接 + closeChan chan struct{} //关闭chan + isClose bool //是否已经关闭 + flag string //ws连接唯一标识 + inChan chan []byte //接受消息缓冲通道 + outChan chan []byte //发送回客户端的消息 + mutex sync.Mutex //互斥锁 + renderProperty renderProperty //扩展云渲染属性 } func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) { @@ -124,13 +119,15 @@ 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), - renderImageTask: make(map[string]struct{}), - renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), + conn: conn, + flag: flag, + closeChan: make(chan struct{}, 1), + inChan: make(chan []byte, 100), + outChan: make(chan []byte, 100), + renderProperty: renderProperty{ + renderImageTask: make(map[string]struct{}), + renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), + }, } //保存连接 mapConnPool.Store(flag, ws) diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go index 97570811..c203443e 100644 --- a/server/websocket/internal/logic/rendernotifylogic.go +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -67,7 +67,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi for _, notifyItem := range req.NotifyList { renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) //查询有无该渲染任务 - _, ok = ws.renderImageTask[renderKey] + _, ok = ws.renderProperty.renderImageTask[renderKey] if !ok { continue } @@ -82,7 +82,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi } b, _ := json.Marshal(rspData) //删除对应的需要渲染的图片map - ws.renderImageTaskCtlChan <- renderImageControlChanItem{ + ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 0, //0删除 1添加 Key: renderKey, } diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go index 3fca11ca..b93e4b67 100644 --- a/server/websocket/internal/logic/ws_render_image_logic.go +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -6,6 +6,18 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) +// 云渲染属性 +type renderProperty struct { + renderImageTask map[string]struct{} //需要渲染的图片任务 + renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道 +} + +// 渲染任务新增移除的控制通道的数据 +type renderImageControlChanItem struct { + Option int // 0删除 1添加 + Key string //map的key +} + // 渲染请求数据处理发送云渲染服务处理 func (w *wsConnectItem) SendToCloudRender(data []byte) { var renderImageData []types.RenderImageReqMsg @@ -22,7 +34,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) { default: //加入渲染任务 key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) - w.renderImageTaskCtlChan <- renderImageControlChanItem{ + w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{ Option: 1, //0删除 1添加 Key: key, } @@ -37,12 +49,12 @@ func (w *wsConnectItem) operationRenderTask() { select { case <-w.closeChan: return - case data := <-w.renderImageTaskCtlChan: + case data := <-w.renderProperty.renderImageTaskCtlChan: switch data.Option { case 0: //删除任务 - delete(w.renderImageTask, data.Key) + delete(w.renderProperty.renderImageTask, data.Key) case 1: //新增任务 - w.renderImageTask[data.Key] = struct{}{} + w.renderProperty.renderImageTask[data.Key] = struct{}{} default: }