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{} mapConnPool = sync.Map{}
) )
// 每个连接的连接属性 // 每个连接的连接基本属性
type wsConnectItem struct { type wsConnectItem struct {
conn *websocket.Conn //websocket的连接 conn *websocket.Conn //websocket的连接
closeChan chan struct{} //关闭chan closeChan chan struct{} //关闭chan
isClose bool //是否已经关闭 isClose bool //是否已经关闭
flag string flag string //ws连接唯一标识
inChan chan []byte //接受消息缓冲通道 inChan chan []byte //接受消息缓冲通道
outChan chan []byte //发送回客户端的消息 outChan chan []byte //发送回客户端的消息
mutex sync.Mutex mutex sync.Mutex //互斥锁
renderImageTask map[string]struct{} //需要渲染的图片 renderProperty renderProperty //扩展云渲染属性
renderImageTaskCtlChan 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) {
@ -124,13 +119,15 @@ func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.Resp
//生成连接唯一标识 //生成连接唯一标识
flag := uuid.New().String() + time.Now().Format("20060102150405") flag := uuid.New().String() + time.Now().Format("20060102150405")
ws := wsConnectItem{ ws := wsConnectItem{
conn: conn, conn: conn,
flag: flag, flag: flag,
closeChan: make(chan struct{}, 1), closeChan: make(chan struct{}, 1),
inChan: make(chan []byte, 100), inChan: make(chan []byte, 100),
outChan: make(chan []byte, 100), outChan: make(chan []byte, 100),
renderImageTask: make(map[string]struct{}), renderProperty: renderProperty{
renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100), renderImageTask: make(map[string]struct{}),
renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100),
},
} }
//保存连接 //保存连接
mapConnPool.Store(flag, ws) mapConnPool.Store(flag, ws)

View File

@ -67,7 +67,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi
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)
//查询有无该渲染任务 //查询有无该渲染任务
_, ok = ws.renderImageTask[renderKey] _, ok = ws.renderProperty.renderImageTask[renderKey]
if !ok { if !ok {
continue continue
} }
@ -82,7 +82,7 @@ func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basi
} }
b, _ := json.Marshal(rspData) b, _ := json.Marshal(rspData)
//删除对应的需要渲染的图片map //删除对应的需要渲染的图片map
ws.renderImageTaskCtlChan <- renderImageControlChanItem{ ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 0, //0删除 1添加 Option: 0, //0删除 1添加
Key: renderKey, Key: renderKey,
} }

View File

@ -6,6 +6,18 @@ import (
"github.com/zeromicro/go-zero/core/logx" "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) { func (w *wsConnectItem) SendToCloudRender(data []byte) {
var renderImageData []types.RenderImageReqMsg var renderImageData []types.RenderImageReqMsg
@ -22,7 +34,7 @@ func (w *wsConnectItem) SendToCloudRender(data []byte) {
default: default:
//加入渲染任务 //加入渲染任务
key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId) key := w.getRenderImageMapKey(v.ProductId, v.SizeId, v.TemplateId)
w.renderImageTaskCtlChan <- renderImageControlChanItem{ w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
Option: 1, //0删除 1添加 Option: 1, //0删除 1添加
Key: key, Key: key,
} }
@ -37,12 +49,12 @@ func (w *wsConnectItem) operationRenderTask() {
select { select {
case <-w.closeChan: case <-w.closeChan:
return return
case data := <-w.renderImageTaskCtlChan: case data := <-w.renderProperty.renderImageTaskCtlChan:
switch data.Option { switch data.Option {
case 0: //删除任务 case 0: //删除任务
delete(w.renderImageTask, data.Key) delete(w.renderProperty.renderImageTask, data.Key)
case 1: //新增任务 case 1: //新增任务
w.renderImageTask[data.Key] = struct{}{} w.renderProperty.renderImageTask[data.Key] = struct{}{}
default: default:
} }