This commit is contained in:
laodaming 2023-07-25 17:41:38 +08:00
parent 17aac7ad66
commit d0fc91b61b
5 changed files with 54 additions and 2 deletions

View File

@ -13,3 +13,6 @@ const (
//图片渲染
WEBSOCKET_RENDER_IMAGE = "render-image"
)
// 云渲染通知需要的签名字符串
const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d"

View File

@ -203,7 +203,8 @@ func (w *wsConnectItem) dealwithReciveData(data []byte) {
return
}
switch parseInfo.MsgType {
case constants.WEBSOCKET_RENDER_IMAGE: //图片渲染
//图片渲染
case constants.WEBSOCKET_RENDER_IMAGE:
var renderImageData []types.RenderImageReqMsg
if err := json.Unmarshal([]byte(parseInfo.Message), &renderImageData); err != nil {
logx.Error("invalid format of websocket render image message", err)

View File

@ -1,11 +1,16 @@
package handler
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"time"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
@ -16,8 +21,45 @@ func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
var req types.RenderNotifyReq
_, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "err param",
Data: nil,
})
return
}
if len(req.NotifyList) == 0 {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,notify list is empty",
Data: nil,
})
return
}
if time.Now().Unix()-120 > req.Time {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,time is expired",
Data: nil,
})
return
}
//验证签名 sha256
notifyByte, _ := json.Marshal(req.NotifyList)
h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex)
fmt.Println(sign)
if req.Sign != sign {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid sign",
Data: nil,
})
return
}
//遍历链接
mapConnPool.Range(func(key, value any) bool {
ws, ok := value.(wsConnectItem)
if !ok {
@ -40,7 +82,7 @@ func setOutRenderImage(req types.RenderNotifyReq, ws wsConnectItem) {
defer ws.mutex.Unlock()
for _, notifyItem := range req.NotifyList {
renderKey := ws.getRenderImageMapKey(notifyItem.ProductId, notifyItem.SizeId, notifyItem.TemplateId)
//加载并删除
//查询
_, ok := ws.renderImage[renderKey]
if !ok {
continue
@ -58,5 +100,7 @@ func setOutRenderImage(req types.RenderNotifyReq, ws wsConnectItem) {
case ws.outChan <- b:
logx.Info("notify send render result to out chan")
}
//删掉已经处理的渲染任务
delete(ws.renderImage, renderKey)
}
}

View File

@ -29,6 +29,8 @@ type RenderImageRspMsg struct {
}
type RenderNotifyReq struct {
Sign string `json:"sign"`
Time int64 `json:"time"`
NotifyList []NotifyItem `json:"notify_list"`
}

View File

@ -39,6 +39,8 @@ type RenderImageRspMsg { //websocket发送渲染完的数据
}
//渲染完了通知接口
type RenderNotifyReq {
Sign string `json:"sign"`
Time int64 `json:"time"`
NotifyList []NotifyItem `json:"notify_list"`
}
type NotifyItem {