This commit is contained in:
laodaming 2023-08-21 13:19:12 +08:00
parent e2744a54f2
commit 46ce6ded3c
2 changed files with 50 additions and 39 deletions

View File

@ -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,
})
deleteTask := renderImageControlChanItem{
//发送处理并删除任务
ws.deleteRenderTask(renderImageControlChanItem{
Option: 0, //0删除 1添加
TaskId: req.TaskId,
RenderId: renderId,
}
select {
case <-ws.closeChan: //关闭了
RenderNotifyImageUrl: uploadRes.ResourceUrl,
})
return true
case ws.renderProperty.renderImageTaskCtlChan <- deleteTask: //删除对应的需要渲染的图片map
//发送数据到out chan
ws.sendToOutChan(b)
return true
case <-time.After(time.Second * 3): //超时丢弃
return true
}
})
logx.Info("渲染回调成功######################")
return resp.SetStatusWithMessage(basic.CodeOK, "success")

View File

@ -26,7 +26,8 @@ type renderProperty struct {
type renderImageControlChanItem struct {
Option int // 0删除 1添加
TaskId string //map的key
RenderId string // map的val
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