diff --git a/server/websocket/internal/logic/ws_render_image.go b/server/websocket/internal/logic/ws_render_image.go index e2307fa9..44e5ae87 100644 --- a/server/websocket/internal/logic/ws_render_image.go +++ b/server/websocket/internal/logic/ws_render_image.go @@ -3,6 +3,7 @@ package logic //处理websocket云渲染任务数据 import ( "bytes" + "context" "encoding/json" "errors" "fmt" @@ -66,6 +67,7 @@ func (w *wsConnectItem) consumeRenderImageData() { //限制并发 limitChan := make(chan struct{}, renderChanConcurrency) defer close(limitChan) + ctlCtx ,cancel:= context.WithCancel(w.logic.ctx) for { select { case <-w.closeChan: //已关闭 @@ -73,15 +75,19 @@ func (w *wsConnectItem) consumeRenderImageData() { case data := <-w.extendRenderProperty.renderChan: //消费数据 if data.RenderData.TemplateTag != w.extendRenderProperty.templateTag{ logx.Info("由于模板切换了,丢弃该渲染消息") + cancel() + ctlCtx ,cancel= context.WithCancel(w.logic.ctx) continue } if data.RenderData.TemplateTagColor.SelectedColorIndex != w.extendRenderProperty.selectColorIndex{ logx.Info("由于模板切换了颜色,丢弃该渲染消息") + cancel() + ctlCtx ,cancel= context.WithCancel(w.logic.ctx) continue } - logx.Info("***************用户id:",w.userId," 游客id:",w.guestId," 当前模板标签:",w.extendRenderProperty.templateTag, " 消息模板标签:",data.RenderData.TemplateTag ," 当前颜色索引:",w.extendRenderProperty.selectColorIndex," 消息颜色索引:",data.RenderData.TemplateTagColor.SelectedColorIndex) + logx.Info("用户id:",w.userId," 游客id:",w.guestId," 当前模板标签:",w.extendRenderProperty.templateTag, " 消息模板标签:",data.RenderData.TemplateTag ," 当前颜色索引:",w.extendRenderProperty.selectColorIndex," 消息颜色索引:",data.RenderData.TemplateTagColor.SelectedColorIndex) limitChan <- struct{}{} - go func(d websocket_data.RenderImageReqMsg) { + go func(ctx context.Context,d websocket_data.RenderImageReqMsg) { defer func() { if err := recover(); err != nil { logx.Error("func renderImage err:", err) @@ -90,8 +96,14 @@ func (w *wsConnectItem) consumeRenderImageData() { defer func() { <-limitChan }() + go func() { + select { + case <-ctx.Done() : + panic("渲染截止") + } + }() w.renderImage(d) - }(data) + }(ctlCtx,data) } } }