This commit is contained in:
laodaming 2023-07-26 15:01:59 +08:00
parent e40a4d43f4
commit 918b5903d7
3 changed files with 50 additions and 33 deletions

View File

@ -53,6 +53,11 @@ type wsConnectItem struct {
outChan chan []byte //发送回客户端的消息 outChan chan []byte //发送回客户端的消息
mutex sync.Mutex mutex sync.Mutex
renderImage map[string]struct{} //需要渲染的图片 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) { func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) {
@ -111,6 +116,7 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
inChan: make(chan []byte, 100), inChan: make(chan []byte, 100),
outChan: make(chan []byte, 100), outChan: make(chan []byte, 100),
renderImage: make(map[string]struct{}), renderImage: make(map[string]struct{}),
renderImageControlChan: make(chan renderImageControlChanItem, 100),
isClose: false, isClose: false,
} }
//保存连接 //保存连接
@ -127,6 +133,8 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
go ws.writeLoop() go ws.writeLoop()
//推消息到云渲染 //推消息到云渲染
go ws.sendLoop() go ws.sendLoop()
//操作连接中渲染任务的增加/删除
go ws.operationRenderTask()
//心跳 //心跳
ws.heartbeat() ws.heartbeat()
} }
@ -165,12 +173,28 @@ func (w *wsConnectItem) close() {
if !w.isClose { if !w.isClose {
w.isClose = true w.isClose = true
close(w.closeChan) close(w.closeChan)
close(w.outChan)
close(w.inChan)
} }
logx.Info("websocket:", w.flag, " is closed") 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() { func (w *wsConnectItem) writeLoop() {
for { for {
select { 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 // 获取需要渲染图片的map key
func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string { func (w *wsConnectItem) getRenderImageMapKey(productId, sizeId, templateId int64) string {
return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId) return fmt.Sprintf("%d-%d-%d", productId, sizeId, templateId)

View File

@ -64,7 +64,6 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi
return false return false
} }
dataList := make([][]byte, 0, len(req.NotifyList)) dataList := make([][]byte, 0, len(req.NotifyList))
deleteRenderKey := make([]string, 0, len(req.NotifyList))
//遍历数据 //遍历数据
for _, notifyItem := range req.NotifyList { for _, notifyItem := range req.NotifyList {
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId) 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) b, _ := json.Marshal(responseData)
dataList = append(dataList, b) dataList = append(dataList, b)
deleteRenderKey = append(deleteRenderKey, renderKey)
}
//删除对应的需要渲染的图片map //删除对应的需要渲染的图片map
ws.deleteRenderMapVal(deleteRenderKey) ws.renderImageControlChan <- renderImageControlChanItem{
Option: 0,
Key: renderKey,
}
}
//发送数据 //发送数据
ws.sendToOutChan(dataList) ws.sendToOutChan(dataList)
return true return true

View File

@ -14,11 +14,12 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) {
return return
} }
logx.Info("收到请求云渲染图片数据:", renderImageData) logx.Info("收到请求云渲染图片数据:", renderImageData)
w.mutex.Lock()
defer w.mutex.Unlock()
//把需要渲染的图片加进去 //把需要渲染的图片加进去
for _, v := range renderImageData { for _, v := range renderImageData {
key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId)
w.renderImage[key] = struct{}{} w.renderImageControlChan <- renderImageControlChanItem{
Option: 1,
Key: key,
}
} }
} }