This commit is contained in:
laodaming 2023-07-26 17:06:53 +08:00
parent 436ed9ae4f
commit d5ff15849d
3 changed files with 36 additions and 27 deletions

View File

@ -57,21 +57,16 @@ var (
mapConnPool = sync.Map{}
)
// 每个连接的连接属性
// 每个连接的连接基本属性
type wsConnectItem struct {
conn *websocket.Conn //websocket的连接
closeChan chan struct{} //关闭chan
isClose bool //是否已经关闭
flag string
flag string //ws连接唯一标识
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
mutex sync.Mutex //互斥锁
renderProperty renderProperty //扩展云渲染属性
}
func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) {
@ -129,8 +124,10 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
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)

View File

@ -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,
}

View File

@ -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:
}